mlbench123 commited on
Commit
31bfeff
·
verified ·
1 Parent(s): 54861e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -37,36 +37,47 @@ def extract_mask_from_drawn(composite_image, background_image):
37
  return mask * 255 # return as binary mask image (255 inside mask)
38
 
39
  def process_video(video_file, mask_image, drawn_editor, progress=gr.Progress()):
40
- # video_file: path to uploaded video
41
- # mask_image: numpy array (HxW or HxWx3) if uploaded, or None
42
- # drawn_editor: dict with 'background', 'composite' from ImageEditor, or None
43
-
44
- # Decide mask source
45
- mask = None
 
 
 
 
 
 
 
46
  if mask_image is not None:
47
- # Ensure mask is binary (if user uploaded a colored mask, convert to gray)
48
  mask = mask_image
49
  if mask.ndim == 3:
50
  mask = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
51
  _, mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
52
- elif drawn_editor is not None:
53
- comp = drawn_editor["composite"]
54
- bg = drawn_editor["background"]
55
- mask = extract_mask_from_drawn(comp, bg)
56
  else:
57
- raise gr.Error("Please provide a mask (upload or draw).")
58
-
59
  progress(0, desc="Extracting keyframes...")
60
- frames = video_to_keyframes(video_file)
 
61
  progress(0.3, desc="Applying mask and cropping...")
62
- cropped_frames = apply_mask_and_crop(frames, mask)
 
63
  progress(0.6, desc="Running inference on frames...")
64
- output_frames = run_gmm_inference(cropped_frames)
 
65
  progress(0.85, desc="Composing final video...")
66
- result_path = compose_final_video(output_frames, "heatmap_output.mp4")
67
- progress(1.0, desc="Done")
68
-
69
- return "✅ Heatmap video generated!", result_path
 
 
 
 
70
 
71
  # Define the Gradio app layout
72
  custom_css = """
 
37
  return mask * 255 # return as binary mask image (255 inside mask)
38
 
39
  def process_video(video_file, mask_image, drawn_editor, progress=gr.Progress()):
40
+ import os
41
+ import uuid
42
+
43
+ # Prepare all output folders
44
+ base_dir = "video_outputs"
45
+ extracted_dir = os.path.join(base_dir, "extracted_frames")
46
+ masked_dir = os.path.join(base_dir, "masked_frames")
47
+ heatmap_dir = os.path.join(base_dir, "output_heatmap")
48
+ os.makedirs(extracted_dir, exist_ok=True)
49
+ os.makedirs(masked_dir, exist_ok=True)
50
+ os.makedirs(heatmap_dir, exist_ok=True)
51
+
52
+ # Choose mask: from upload or drawing
53
  if mask_image is not None:
 
54
  mask = mask_image
55
  if mask.ndim == 3:
56
  mask = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
57
  _, mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
58
+ elif drawn_editor and "composite" in drawn_editor and "background" in drawn_editor:
59
+ mask = extract_mask_from_drawn(drawn_editor)
 
 
60
  else:
61
+ raise gr.Error("Please provide a valid mask (uploaded or drawn).")
62
+
63
  progress(0, desc="Extracting keyframes...")
64
+ video_to_keyframes(video_file, extracted_dir)
65
+
66
  progress(0.3, desc="Applying mask and cropping...")
67
+ apply_mask_and_crop(extracted_dir, mask, masked_dir)
68
+
69
  progress(0.6, desc="Running inference on frames...")
70
+ run_gmm_inference(masked_dir, heatmap_dir)
71
+
72
  progress(0.85, desc="Composing final video...")
73
+ video_name = f"heatmap_output_{uuid.uuid4().hex[:6]}.mp4"
74
+ result_path = os.path.join(base_dir, video_name)
75
+ compose_final_video(mask, heatmap_dir, extracted_dir, result_path)
76
+
77
+ progress(1.0, desc="Done!")
78
+
79
+ return "✅ Heatmap video generated!", result_path, result_path
80
+
81
 
82
  # Define the Gradio app layout
83
  custom_css = """