John Ho commited on
Commit
ec6dcd6
·
1 Parent(s): 61b7311

added new function frames_to_vid

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -1,6 +1,7 @@
1
  # Import helpers for mask encoding and bbox extraction
2
  import sys
3
  import tempfile
 
4
 
5
  import cv2
6
  import gradio as gr
@@ -117,6 +118,18 @@ def apply_mask_overlay(base_image, mask_data, object_ids=None, opacity=0.5):
117
  return Image.alpha_composite(base_image, composite_layer).convert("RGB")
118
 
119
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  # Our Inference Function
121
  @spaces.GPU(duration=120)
122
  def video_inference(input_video, prompt: str):
@@ -182,11 +195,12 @@ def video_inference(input_video, prompt: str):
182
  )
183
  session = VID_PROCESSOR.add_text_prompt(inference_session=session, text=prompt)
184
  temp_out_path = tempfile.mktemp(suffix=".mp4")
185
- video_writer = cv2.VideoWriter(
186
- temp_out_path, cv2.VideoWriter_fourcc(*"mp4v"), vid_fps, (vid_w, vid_h)
187
- )
188
 
189
  detections = []
 
190
  for model_out in VID_MODEL.propagate_in_video_iterator(
191
  inference_session=session, max_frame_num_to_track=len(video_frames)
192
  ):
@@ -223,10 +237,18 @@ def video_inference(input_video, prompt: str):
223
  )
224
  else:
225
  final_frame = original_pil
226
- video_writer.write(cv2.cvtColor(np.array(final_frame), cv2.COLOR_RGB2BGR))
227
- video_writer.release()
 
 
228
  return {
229
- "output_video": temp_out_path,
 
 
 
 
 
 
230
  "detections": detections,
231
  "status": "Video processing completed successfully.✅",
232
  }
 
1
  # Import helpers for mask encoding and bbox extraction
2
  import sys
3
  import tempfile
4
+ from ast import Return
5
 
6
  import cv2
7
  import gradio as gr
 
118
  return Image.alpha_composite(base_image, composite_layer).convert("RGB")
119
 
120
 
121
+ def frames_to_vid(pil_frames, output_path: str, vid_fps: int, vid_w: int, vid_h: int):
122
+ assert len(pil_frames) > 0, f"Number of frames must be greater than 0"
123
+ assert isinstance(pil_frames, list), f"pil_frames must be a list"
124
+ video_writer = cv2.VideoWriter(
125
+ output_path, cv2.VideoWriter_fourcc(*"mp4v"), vid_fps, (vid_w, vid_h)
126
+ )
127
+ for f in pil_frames:
128
+ video_writer.write(cv2.cvtColor(np.array(f), cv2.COLOR_RGB2BGR))
129
+ video_writer.release()
130
+ return output_path
131
+
132
+
133
  # Our Inference Function
134
  @spaces.GPU(duration=120)
135
  def video_inference(input_video, prompt: str):
 
195
  )
196
  session = VID_PROCESSOR.add_text_prompt(inference_session=session, text=prompt)
197
  temp_out_path = tempfile.mktemp(suffix=".mp4")
198
+ # video_writer = cv2.VideoWriter(
199
+ # temp_out_path, cv2.VideoWriter_fourcc(*"mp4v"), vid_fps, (vid_w, vid_h)
200
+ # )
201
 
202
  detections = []
203
+ annotated_frames = []
204
  for model_out in VID_MODEL.propagate_in_video_iterator(
205
  inference_session=session, max_frame_num_to_track=len(video_frames)
206
  ):
 
237
  )
238
  else:
239
  final_frame = original_pil
240
+ # video_writer.write(cv2.cvtColor(np.array(final_frame), cv2.COLOR_RGB2BGR))
241
+ annotated_frames.append(final_frame)
242
+
243
+ # video_writer.release()
244
  return {
245
+ "output_video": frames_to_vid(
246
+ annotated_frames,
247
+ output_path=temp_out_path,
248
+ vid_fps=vid_fps,
249
+ vid_h=vid_h,
250
+ vid_w=vid_w,
251
+ ),
252
  "detections": detections,
253
  "status": "Video processing completed successfully.✅",
254
  }