Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -132,37 +132,55 @@ def video_vision(video_input_path, prompt, video_interval):
|
|
| 132 |
_seg_idx = 0
|
| 133 |
pred_masks = result['prediction_masks'][_seg_idx]
|
| 134 |
seg_frames = []
|
|
|
|
|
|
|
| 135 |
for frame_idx in range(len(vid_frames)):
|
| 136 |
pred_mask = pred_masks[frame_idx]
|
| 137 |
temp_dir = tempfile.mkdtemp()
|
| 138 |
os.makedirs(temp_dir, exist_ok=True)
|
|
|
|
|
|
|
| 139 |
seg_frame = visualize(pred_mask, image_paths[frame_idx], temp_dir)
|
| 140 |
seg_frames.append(seg_frame)
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
output_video = "output_video.mp4"
|
|
|
|
| 143 |
|
| 144 |
# Read the first image to get the size (resolution)
|
| 145 |
frame = cv2.imread(seg_frames[0])
|
| 146 |
height, width, layers = frame.shape
|
| 147 |
|
| 148 |
-
# Define the video codec and create VideoWriter
|
| 149 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4
|
| 150 |
video = cv2.VideoWriter(output_video, fourcc, new_fps, (width, height))
|
|
|
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
for
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
video.release()
|
|
|
|
| 159 |
|
| 160 |
print(f"Video created successfully at {output_video}")
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
return result['prediction'], output_video
|
| 163 |
|
| 164 |
else:
|
| 165 |
-
return result['prediction'], None
|
| 166 |
|
| 167 |
|
| 168 |
|
|
@@ -214,11 +232,12 @@ with gr.Blocks(analytics_enabled=False) as demo:
|
|
| 214 |
with gr.Column():
|
| 215 |
vid_output_res = gr.Textbox(label="Response")
|
| 216 |
output_video = gr.Video(label="Segmentation")
|
|
|
|
| 217 |
|
| 218 |
submit_video_btn.click(
|
| 219 |
fn = video_vision,
|
| 220 |
inputs = [video_input, vid_instruction, frame_interval],
|
| 221 |
-
outputs = [vid_output_res, output_video]
|
| 222 |
)
|
| 223 |
|
| 224 |
demo.queue().launch(show_api=False, show_error=True)
|
|
|
|
| 132 |
_seg_idx = 0
|
| 133 |
pred_masks = result['prediction_masks'][_seg_idx]
|
| 134 |
seg_frames = []
|
| 135 |
+
masked_only_frames = [] # New list for masked-only frames
|
| 136 |
+
|
| 137 |
for frame_idx in range(len(vid_frames)):
|
| 138 |
pred_mask = pred_masks[frame_idx]
|
| 139 |
temp_dir = tempfile.mkdtemp()
|
| 140 |
os.makedirs(temp_dir, exist_ok=True)
|
| 141 |
+
|
| 142 |
+
# Create visualized frame with segmentation overlay
|
| 143 |
seg_frame = visualize(pred_mask, image_paths[frame_idx], temp_dir)
|
| 144 |
seg_frames.append(seg_frame)
|
| 145 |
|
| 146 |
+
# Create a binary mask image (white mask on black background)
|
| 147 |
+
binary_mask = (pred_mask.astype('uint8') * 255) # Convert mask to 0/255
|
| 148 |
+
binary_mask_path = os.path.join(temp_dir, f"binary_mask_{frame_idx}.png")
|
| 149 |
+
cv2.imwrite(binary_mask_path, binary_mask)
|
| 150 |
+
masked_only_frames.append(binary_mask_path)
|
| 151 |
+
|
| 152 |
output_video = "output_video.mp4"
|
| 153 |
+
masked_video = "masked_only_video.mp4" # New video file for masked areas only
|
| 154 |
|
| 155 |
# Read the first image to get the size (resolution)
|
| 156 |
frame = cv2.imread(seg_frames[0])
|
| 157 |
height, width, layers = frame.shape
|
| 158 |
|
| 159 |
+
# Define the video codec and create VideoWriter objects
|
| 160 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4
|
| 161 |
video = cv2.VideoWriter(output_video, fourcc, new_fps, (width, height))
|
| 162 |
+
masked_video_writer = cv2.VideoWriter(masked_video, fourcc, new_fps, (width, height), isColor=False)
|
| 163 |
|
| 164 |
+
# Write frames to the videos
|
| 165 |
+
for idx, (seg_frame_path, mask_frame_path) in enumerate(zip(seg_frames, masked_only_frames)):
|
| 166 |
+
seg_frame = cv2.imread(seg_frame_path)
|
| 167 |
+
mask_frame = cv2.imread(mask_frame_path, cv2.IMREAD_GRAYSCALE) # Read the binary mask in grayscale
|
| 168 |
|
| 169 |
+
video.write(seg_frame)
|
| 170 |
+
masked_video_writer.write(mask_frame)
|
| 171 |
+
|
| 172 |
+
# Release the video writers
|
| 173 |
video.release()
|
| 174 |
+
masked_video_writer.release()
|
| 175 |
|
| 176 |
print(f"Video created successfully at {output_video}")
|
| 177 |
+
print(f"Masked-only video created successfully at {masked_video}")
|
| 178 |
+
|
| 179 |
+
return result['prediction'], output_video, masked_video
|
| 180 |
|
|
|
|
| 181 |
|
| 182 |
else:
|
| 183 |
+
return result['prediction'], None, None
|
| 184 |
|
| 185 |
|
| 186 |
|
|
|
|
| 232 |
with gr.Column():
|
| 233 |
vid_output_res = gr.Textbox(label="Response")
|
| 234 |
output_video = gr.Video(label="Segmentation")
|
| 235 |
+
masked_output = gr.Video(label="Masked video")
|
| 236 |
|
| 237 |
submit_video_btn.click(
|
| 238 |
fn = video_vision,
|
| 239 |
inputs = [video_input, vid_instruction, frame_interval],
|
| 240 |
+
outputs = [vid_output_res, output_video, masked_output]
|
| 241 |
)
|
| 242 |
|
| 243 |
demo.queue().launch(show_api=False, show_error=True)
|