Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ from transformers import AutoModelForImageSegmentation
|
|
13 |
import cv2
|
14 |
import ezdxf
|
15 |
import gradio as gr
|
|
|
16 |
import gc
|
17 |
# from scalingtestupdated import calculate_scaling_factor
|
18 |
from scalingtestupdated import calculate_scaling_factor_with_units, calculate_paper_scaling_factor, convert_units
|
@@ -42,6 +43,16 @@ PAPER_SIZES = {
|
|
42 |
"US Letter": {"width": 215.9, "height": 279.4}
|
43 |
}
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
def get_yolo_world():
|
46 |
"""Lazy load YOLO-World model"""
|
47 |
yolo_world = YOLOWorld('yolov8s-world.pt') # Smaller model for efficiency
|
@@ -525,6 +536,35 @@ def remove_bg_u2netp(image: np.ndarray) -> np.ndarray:
|
|
525 |
logger.error(f"Error in U2NETP background removal: {e}")
|
526 |
raise
|
527 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
528 |
def remove_bg(image: np.ndarray) -> np.ndarray:
|
529 |
"""Remove background using BiRefNet model for main objects"""
|
530 |
try:
|
@@ -1245,7 +1285,8 @@ def predict_with_paper(image, paper_size, offset, offset_unit, finger_clearance=
|
|
1245 |
# Remove background from main objects
|
1246 |
orig_size = image.shape[:2]
|
1247 |
# objects_mask = remove_bg(image)
|
1248 |
-
objects_mask = remove_bg(image)
|
|
|
1249 |
processed_size = objects_mask.shape[:2]
|
1250 |
|
1251 |
# Resize mask to match original image
|
|
|
13 |
import cv2
|
14 |
import ezdxf
|
15 |
import gradio as gr
|
16 |
+
from modnet import MODNet
|
17 |
import gc
|
18 |
# from scalingtestupdated import calculate_scaling_factor
|
19 |
from scalingtestupdated import calculate_scaling_factor_with_units, calculate_paper_scaling_factor, convert_units
|
|
|
43 |
"US Letter": {"width": 215.9, "height": 279.4}
|
44 |
}
|
45 |
|
46 |
+
def get_modnet():
|
47 |
+
"""Lazy load MODNet model"""
|
48 |
+
global modnet_global
|
49 |
+
if modnet_global is None:
|
50 |
+
logger.info("Loading MODNet model...")
|
51 |
+
modnet_global = MODNet(backbone_pretrained=False)
|
52 |
+
modnet_global.load_state_dict(torch.load('modnet_photographic_portrait_matting.ckpt', map_location='cpu'))
|
53 |
+
modnet_global.to(device).eval()
|
54 |
+
return modnet_global
|
55 |
+
|
56 |
def get_yolo_world():
|
57 |
"""Lazy load YOLO-World model"""
|
58 |
yolo_world = YOLOWorld('yolov8s-world.pt') # Smaller model for efficiency
|
|
|
536 |
logger.error(f"Error in U2NETP background removal: {e}")
|
537 |
raise
|
538 |
|
539 |
+
def remove_bg_modnet(image: np.ndarray) -> np.ndarray:
|
540 |
+
"""Remove background using MODNet"""
|
541 |
+
try:
|
542 |
+
model = get_modnet()
|
543 |
+
|
544 |
+
# Preprocess
|
545 |
+
transform = transforms.Compose([
|
546 |
+
transforms.ToTensor(),
|
547 |
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
548 |
+
])
|
549 |
+
|
550 |
+
image_pil = Image.fromarray(image)
|
551 |
+
image_tensor = transform(image_pil).unsqueeze(0).to(device)
|
552 |
+
|
553 |
+
# Predict
|
554 |
+
with torch.no_grad():
|
555 |
+
_, _, matte = model(image_tensor, True)
|
556 |
+
|
557 |
+
# Post-process
|
558 |
+
matte = matte.squeeze().cpu().numpy()
|
559 |
+
matte = (matte * 255).astype(np.uint8)
|
560 |
+
matte = cv2.resize(matte, (image.shape[1], image.shape[0]))
|
561 |
+
|
562 |
+
return matte
|
563 |
+
|
564 |
+
except Exception as e:
|
565 |
+
logger.error(f"MODNet failed: {e}")
|
566 |
+
return remove_bg_u2netp(image) # Fallback to U2Net
|
567 |
+
|
568 |
def remove_bg(image: np.ndarray) -> np.ndarray:
|
569 |
"""Remove background using BiRefNet model for main objects"""
|
570 |
try:
|
|
|
1285 |
# Remove background from main objects
|
1286 |
orig_size = image.shape[:2]
|
1287 |
# objects_mask = remove_bg(image)
|
1288 |
+
# objects_mask = remove_bg(image)
|
1289 |
+
objects_mask = remove_bg_modnet(image)
|
1290 |
processed_size = objects_mask.shape[:2]
|
1291 |
|
1292 |
# Resize mask to match original image
|