Mirko Trasciatti commited on
Commit ·
cd7c867
1
Parent(s): cf42079
Add Remove Background checkbox to Multi-Object tab with overlay option
Browse files385 unmodified lines3863873883893903913924 unmodified lines397398399400401402134 unmodified lines537538539540541542543544545546547192 unmodified lines7407417427437447453 unmodified lines749750751752753754755385 unmodified lines@spaces.GPUdef segment_video_multi(video_file, objects_json):"""Multi-object video segmentation.4 unmodified lines{"id": "ball", "init_frame": 0, "point_x": 360, "point_y": 640},{"id": "player", "init_frame": 0, "point_x": 320, "point_y": 500}]Returns:Tuple: (video1_path, video2_path, status_json)134 unmodified linesmask = cv2.resize(mask, (width, height), interpolation=cv2.INTER_NEAREST)mask_binary = (mask > 0.5).astype(np.uint8)# Remove backgroundbackground = np.zeros_like(frame)mask_3d = np.repeat(mask_binary[:, :, np.newaxis], 3, axis=2)frame = frame * mask_3d + background * (1 - mask_3d)frame_bgr = cv2.cvtColor(frame.astype(np.uint8), cv2.COLOR_RGB2BGR)out.write(frame_bgr)192 unmodified linesplaceholder='[{"id": "player", "init_frame": 165, "point_x": 180, "point_y": 320}]')process_btn_multi = gr.Button("🎬 Process Multi-Object", variant="primary")with gr.Column():3 unmodified linesprocess_btn_multi.click(fn=segment_video_multi,inputs=[video_input_multi, objects_json],outputs=[output_video_multi_1, output_video_multi_2, status_text_multi])385 unmodified lines3863873883893903913924 unmodified lines397398399400401402403134 unmodified lines538539540541542543544545546547548549550551552553554555556192 unmodified lines7497507517527537547557567577587597603 unmodified lines764765766767768769770385 unmodified lines@spaces.GPUdef segment_video_multi(video_file, objects_json, remove_bg=True):"""Multi-object video segmentation.4 unmodified lines{"id": "ball", "init_frame": 0, "point_x": 360, "point_y": 640},{"id": "player", "init_frame": 0, "point_x": 320, "point_y": 500}]remove_bg: If True, remove background. If False, overlay masks on original video.Returns:Tuple: (video1_path, video2_path, status_json)134 unmodified linesmask = cv2.resize(mask, (width, height), interpolation=cv2.INTER_NEAREST)mask_binary = (mask > 0.5).astype(np.uint8)mask_3d = np.repeat(mask_binary[:, :, np.newaxis], 3, axis=2)if remove_bg:# Remove background - show only tracked objectbackground = np.zeros_like(frame)frame = frame * mask_3d + background * (1 - mask_3d)else:# Overlay colored mask on original video# Create a colored overlay (e.g., semi-transparent green)overlay_color = np.array([0, 255, 0], dtype=np.uint8) # Greenoverlay = np.ones_like(frame) * overlay_coloralpha = 0.5 # Transparencyframe = (frame * (1 - alpha * mask_3d) + overlay * alpha * mask_3d).astype(np.uint8)frame_bgr = cv2.cvtColor(frame.astype(np.uint8), cv2.COLOR_RGB2BGR)out.write(frame_bgr)192 unmodified linesplaceholder='[{"id": "player", "init_frame": 165, "point_x": 180, "point_y": 320}]')remove_bg_multi = gr.Checkbox(label="Remove Background",value=True,info="If checked, shows only tracked objects. If unchecked, overlays masks on original video.")process_btn_multi = gr.Button("🎬 Process Multi-Object", variant="primary")with gr.Column():3 unmodified linesprocess_btn_multi.click(fn=segment_video_multi,inputs=[video_input_multi, objects_json, remove_bg_multi],outputs=[output_video_multi_1, output_video_multi_2, status_text_multi])
app.py+21-6