reab5555 commited on
Commit
7f942f1
·
verified ·
1 Parent(s): f9be460

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -31
app.py CHANGED
@@ -57,38 +57,19 @@ def process_video(video_path, target, progress=gr.Progress()):
57
  return None, f"Error: Unable to open video file at {video_path}"
58
 
59
  frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
60
- original_fps = int(cap.get(cv2.CAP_PROP_FPS))
61
- output_fps = 3
62
-
63
- output_path = "output_video.mp4"
64
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
65
- out = cv2.VideoWriter(output_path, fourcc, output_fps, (int(cap.get(3)), int(cap.get(4))))
66
-
67
- batch_size = 8
68
- frames = []
69
 
70
  for frame in progress.tqdm(range(frame_count)):
71
  ret, img = cap.read()
72
  if not ret:
73
  break
74
 
75
- if frame % (original_fps // output_fps) != 0:
76
- continue
77
-
78
  pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
79
- frames.append(pil_img)
80
-
81
- if len(frames) == batch_size or frame == frame_count - 1:
82
- annotated_frames = [detect_objects_in_frame(frame, target) for frame in frames]
83
- for annotated_img in annotated_frames:
84
- annotated_frame = cv2.cvtColor(np.array(annotated_img), cv2.COLOR_RGB2BGR)
85
- out.write(annotated_frame)
86
- frames = []
87
 
88
  cap.release()
89
- out.release()
90
-
91
- return output_path, None
92
 
93
  def load_sample_frame(video_path):
94
  cap = cv2.VideoCapture(video_path)
@@ -107,31 +88,47 @@ def gradio_app():
107
 
108
  video_input = gr.Video(label="Upload Video")
109
  target_input = gr.Textbox(label="Target Object", value="Elephant")
110
- output_video = gr.Video(label="Output Video")
 
111
  error_output = gr.Textbox(label="Error Messages", visible=False)
112
  sample_video_frame = gr.Image(value=load_sample_frame("Drone Video of African Wildlife Wild Botswan.mp4"), label="Sample Video Frame")
113
  use_sample_button = gr.Button("Use Sample Video")
114
  progress_bar = gr.Progress()
115
 
 
 
116
  def process_and_update(video, target):
117
- output_video_path, error = process_video(video, target, progress_bar)
118
- return output_video_path, error
 
 
 
 
 
 
 
 
 
119
 
120
  video_input.upload(process_and_update,
121
  inputs=[video_input, target_input],
122
- outputs=[output_video, error_output])
 
 
 
 
123
 
124
  def use_sample_video():
125
  sample_video_path = "Drone Video of African Wildlife Wild Botswan.mp4"
126
- output_video_path, error = process_and_update(sample_video_path, "Elephant")
127
- return output_video_path, error
128
 
129
  use_sample_button.click(use_sample_video,
130
  inputs=None,
131
- outputs=[output_video, error_output])
132
 
133
  return app
134
 
135
  if __name__ == "__main__":
136
  app = gradio_app()
137
- app.launch(share=True)
 
57
  return None, f"Error: Unable to open video file at {video_path}"
58
 
59
  frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
60
+ processed_frames = []
 
 
 
 
 
 
 
 
61
 
62
  for frame in progress.tqdm(range(frame_count)):
63
  ret, img = cap.read()
64
  if not ret:
65
  break
66
 
 
 
 
67
  pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
68
+ annotated_img = detect_objects_in_frame(pil_img, target)
69
+ processed_frames.append(np.array(annotated_img))
 
 
 
 
 
 
70
 
71
  cap.release()
72
+ return processed_frames, None
 
 
73
 
74
  def load_sample_frame(video_path):
75
  cap = cv2.VideoCapture(video_path)
 
88
 
89
  video_input = gr.Video(label="Upload Video")
90
  target_input = gr.Textbox(label="Target Object", value="Elephant")
91
+ frame_slider = gr.Slider(minimum=0, maximum=100, step=1, label="Frame", value=0)
92
+ output_image = gr.Image(label="Processed Frame")
93
  error_output = gr.Textbox(label="Error Messages", visible=False)
94
  sample_video_frame = gr.Image(value=load_sample_frame("Drone Video of African Wildlife Wild Botswan.mp4"), label="Sample Video Frame")
95
  use_sample_button = gr.Button("Use Sample Video")
96
  progress_bar = gr.Progress()
97
 
98
+ processed_frames = gr.State([])
99
+
100
  def process_and_update(video, target):
101
+ frames, error = process_video(video, target, progress_bar)
102
+ if frames is not None:
103
+ frame_slider.maximum = len(frames) - 1
104
+ frame_slider.value = 0
105
+ return frames, frames[0], error, gr.Slider.update(maximum=len(frames) - 1, value=0)
106
+ return None, None, error, gr.Slider.update(maximum=100, value=0)
107
+
108
+ def update_frame(frame_index, frames):
109
+ if frames and 0 <= frame_index < len(frames):
110
+ return frames[frame_index]
111
+ return None
112
 
113
  video_input.upload(process_and_update,
114
  inputs=[video_input, target_input],
115
+ outputs=[processed_frames, output_image, error_output, frame_slider])
116
+
117
+ frame_slider.change(update_frame,
118
+ inputs=[frame_slider, processed_frames],
119
+ outputs=[output_image])
120
 
121
  def use_sample_video():
122
  sample_video_path = "Drone Video of African Wildlife Wild Botswan.mp4"
123
+ frames, output_image, error, slider_update = process_and_update(sample_video_path, "Elephant")
124
+ return frames, output_image, error, slider_update
125
 
126
  use_sample_button.click(use_sample_video,
127
  inputs=None,
128
+ outputs=[processed_frames, output_image, error_output, frame_slider])
129
 
130
  return app
131
 
132
  if __name__ == "__main__":
133
  app = gradio_app()
134
+ app.launch(share=True)