Mirko Trasciatti
commited on
Commit
·
cd7c867
1
Parent(s):
cf42079
Add Remove Background checkbox to Multi-Object tab with overlay option
Browse files
app.py
CHANGED
|
@@ -386,7 +386,7 @@ def segment_video_simple(video_file, point_x, point_y, frame_idx, remove_bg):
|
|
| 386 |
|
| 387 |
|
| 388 |
@spaces.GPU
|
| 389 |
-
def segment_video_multi(video_file, objects_json):
|
| 390 |
"""
|
| 391 |
Multi-object video segmentation.
|
| 392 |
|
|
@@ -397,6 +397,7 @@ def segment_video_multi(video_file, objects_json):
|
|
| 397 |
{"id": "ball", "init_frame": 0, "point_x": 360, "point_y": 640},
|
| 398 |
{"id": "player", "init_frame": 0, "point_x": 320, "point_y": 500}
|
| 399 |
]
|
|
|
|
| 400 |
|
| 401 |
Returns:
|
| 402 |
Tuple: (video1_path, video2_path, status_json)
|
|
@@ -537,11 +538,19 @@ def segment_video_multi(video_file, objects_json):
|
|
| 537 |
mask = cv2.resize(mask, (width, height), interpolation=cv2.INTER_NEAREST)
|
| 538 |
|
| 539 |
mask_binary = (mask > 0.5).astype(np.uint8)
|
| 540 |
-
|
| 541 |
-
# Remove background
|
| 542 |
-
background = np.zeros_like(frame)
|
| 543 |
mask_3d = np.repeat(mask_binary[:, :, np.newaxis], 3, axis=2)
|
| 544 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
|
| 546 |
frame_bgr = cv2.cvtColor(frame.astype(np.uint8), cv2.COLOR_RGB2BGR)
|
| 547 |
out.write(frame_bgr)
|
|
@@ -740,6 +749,12 @@ def create_app():
|
|
| 740 |
placeholder='[{"id": "player", "init_frame": 165, "point_x": 180, "point_y": 320}]'
|
| 741 |
)
|
| 742 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 743 |
process_btn_multi = gr.Button("🎬 Process Multi-Object", variant="primary")
|
| 744 |
|
| 745 |
with gr.Column():
|
|
@@ -749,7 +764,7 @@ def create_app():
|
|
| 749 |
|
| 750 |
process_btn_multi.click(
|
| 751 |
fn=segment_video_multi,
|
| 752 |
-
inputs=[video_input_multi, objects_json],
|
| 753 |
outputs=[output_video_multi_1, output_video_multi_2, status_text_multi]
|
| 754 |
)
|
| 755 |
|
|
|
|
| 386 |
|
| 387 |
|
| 388 |
@spaces.GPU
|
| 389 |
+
def segment_video_multi(video_file, objects_json, remove_bg=True):
|
| 390 |
"""
|
| 391 |
Multi-object video segmentation.
|
| 392 |
|
|
|
|
| 397 |
{"id": "ball", "init_frame": 0, "point_x": 360, "point_y": 640},
|
| 398 |
{"id": "player", "init_frame": 0, "point_x": 320, "point_y": 500}
|
| 399 |
]
|
| 400 |
+
remove_bg: If True, remove background. If False, overlay masks on original video.
|
| 401 |
|
| 402 |
Returns:
|
| 403 |
Tuple: (video1_path, video2_path, status_json)
|
|
|
|
| 538 |
mask = cv2.resize(mask, (width, height), interpolation=cv2.INTER_NEAREST)
|
| 539 |
|
| 540 |
mask_binary = (mask > 0.5).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
| 541 |
mask_3d = np.repeat(mask_binary[:, :, np.newaxis], 3, axis=2)
|
| 542 |
+
|
| 543 |
+
if remove_bg:
|
| 544 |
+
# Remove background - show only tracked object
|
| 545 |
+
background = np.zeros_like(frame)
|
| 546 |
+
frame = frame * mask_3d + background * (1 - mask_3d)
|
| 547 |
+
else:
|
| 548 |
+
# Overlay colored mask on original video
|
| 549 |
+
# Create a colored overlay (e.g., semi-transparent green)
|
| 550 |
+
overlay_color = np.array([0, 255, 0], dtype=np.uint8) # Green
|
| 551 |
+
overlay = np.ones_like(frame) * overlay_color
|
| 552 |
+
alpha = 0.5 # Transparency
|
| 553 |
+
frame = (frame * (1 - alpha * mask_3d) + overlay * alpha * mask_3d).astype(np.uint8)
|
| 554 |
|
| 555 |
frame_bgr = cv2.cvtColor(frame.astype(np.uint8), cv2.COLOR_RGB2BGR)
|
| 556 |
out.write(frame_bgr)
|
|
|
|
| 749 |
placeholder='[{"id": "player", "init_frame": 165, "point_x": 180, "point_y": 320}]'
|
| 750 |
)
|
| 751 |
|
| 752 |
+
remove_bg_multi = gr.Checkbox(
|
| 753 |
+
label="Remove Background",
|
| 754 |
+
value=True,
|
| 755 |
+
info="If checked, shows only tracked objects. If unchecked, overlays masks on original video."
|
| 756 |
+
)
|
| 757 |
+
|
| 758 |
process_btn_multi = gr.Button("🎬 Process Multi-Object", variant="primary")
|
| 759 |
|
| 760 |
with gr.Column():
|
|
|
|
| 764 |
|
| 765 |
process_btn_multi.click(
|
| 766 |
fn=segment_video_multi,
|
| 767 |
+
inputs=[video_input_multi, objects_json, remove_bg_multi],
|
| 768 |
outputs=[output_video_multi_1, output_video_multi_2, status_text_multi]
|
| 769 |
)
|
| 770 |
|