Muhammad Anas Akhtar
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -80,6 +80,11 @@ def create_output_writer(cap, output_path):
|
|
80 |
|
81 |
raise ValueError("Could not initialize any video codec")
|
82 |
|
|
|
|
|
|
|
|
|
|
|
83 |
def process_video(video_path, progress=gr.Progress()):
|
84 |
"""
|
85 |
Process the video file and return the path to the processed video
|
@@ -116,14 +121,19 @@ def process_video(video_path, progress=gr.Progress()):
|
|
116 |
|
117 |
# Process frame
|
118 |
if frame_count % process_every_n_frames == 0:
|
119 |
-
# Convert frame to
|
120 |
-
|
121 |
-
|
122 |
-
# Detect objects
|
123 |
-
detections = object_detector(frame_rgb)
|
124 |
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
# Write the frame
|
129 |
out.write(frame)
|
@@ -181,4 +191,4 @@ demo = gr.Interface(
|
|
181 |
)
|
182 |
|
183 |
if __name__ == "__main__":
|
184 |
-
demo.launch()
|
|
|
80 |
|
81 |
raise ValueError("Could not initialize any video codec")
|
82 |
|
83 |
+
def frame_to_pil(frame):
|
84 |
+
"""Convert OpenCV frame to PIL Image"""
|
85 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
86 |
+
return Image.fromarray(rgb_frame)
|
87 |
+
|
88 |
def process_video(video_path, progress=gr.Progress()):
|
89 |
"""
|
90 |
Process the video file and return the path to the processed video
|
|
|
121 |
|
122 |
# Process frame
|
123 |
if frame_count % process_every_n_frames == 0:
|
124 |
+
# Convert frame to PIL Image for the model
|
125 |
+
pil_frame = frame_to_pil(frame)
|
|
|
|
|
|
|
126 |
|
127 |
+
try:
|
128 |
+
# Detect objects
|
129 |
+
detections = object_detector(pil_frame)
|
130 |
+
|
131 |
+
# Draw bounding boxes
|
132 |
+
frame = draw_bounding_boxes(frame, detections)
|
133 |
+
except Exception as e:
|
134 |
+
print(f"Error processing frame {frame_count}: {str(e)}")
|
135 |
+
# Continue with the original frame if detection fails
|
136 |
+
pass
|
137 |
|
138 |
# Write the frame
|
139 |
out.write(frame)
|
|
|
191 |
)
|
192 |
|
193 |
if __name__ == "__main__":
|
194 |
+
demo.launch()
|