Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -138,40 +138,56 @@ def detect_image(image):
|
|
138 |
|
139 |
def detect_video(video_path):
|
140 |
video = cv2.VideoCapture(video_path)
|
141 |
-
frame_rate = video.get(cv2.CAP_PROP_FPS)
|
142 |
-
|
143 |
-
#
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
# Create a VideoWriter object to write the output video
|
148 |
-
video_output_path = 'output_video.mp4' # Replace with your desired output video path
|
149 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can change the codec as needed
|
150 |
-
video_writer = cv2.VideoWriter(video_output_path, fourcc, frame_rate, (width, height))
|
151 |
|
152 |
frame_count = 0
|
153 |
while True:
|
154 |
success, frame = video.read()
|
155 |
if not success:
|
156 |
break
|
157 |
-
|
158 |
-
|
159 |
-
results = model(frame)
|
160 |
-
# Access the first detection result
|
161 |
-
detection_result = results.pandas().xyxy.iloc[0]
|
162 |
-
# Extract the bounding box coordinates
|
163 |
-
xmin, ymin, xmax, ymax = detection_result["xmin"], detection_result["ymin"], detection_result["xmax"], detection_result["ymax"]
|
164 |
-
# Draw the bounding box on the frame
|
165 |
-
frame = cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
|
166 |
-
|
167 |
-
# Write the frame to the output video
|
168 |
-
video_writer.write(frame)
|
169 |
-
|
170 |
frame_count += 1
|
171 |
|
172 |
video.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
video_writer.release()
|
174 |
|
|
|
|
|
|
|
|
|
175 |
return video_output_path
|
176 |
|
177 |
|
@@ -196,4 +212,4 @@ interfaces = [img_interface, vid_interface]
|
|
196 |
tabbed_interface = gr.TabbedInterface(interfaces, ["Image", "Video"])
|
197 |
|
198 |
# Launch the tabbed interface
|
199 |
-
tabbed_interface.launch(debug=True)
|
|
|
138 |
|
139 |
def detect_video(video_path):
|
140 |
video = cv2.VideoCapture(video_path)
|
141 |
+
frame_rate = video.get(cv2.CAP_PROP_FPS)
|
142 |
+
|
143 |
+
# Create a directory to store the frames
|
144 |
+
frames_dir = 'frames'
|
145 |
+
os.makedirs(frames_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
frame_count = 0
|
148 |
while True:
|
149 |
success, frame = video.read()
|
150 |
if not success:
|
151 |
break
|
152 |
+
frame_output_path = os.path.join(frames_dir, f'frame_{frame_count:04d}.jpg')
|
153 |
+
cv2.imwrite(frame_output_path, frame)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
frame_count += 1
|
155 |
|
156 |
video.release()
|
157 |
+
cv2.destroyAllWindows()
|
158 |
+
|
159 |
+
# Process the frames with object detection and save the results
|
160 |
+
results_dir = 'results'
|
161 |
+
os.makedirs(results_dir, exist_ok=True)
|
162 |
+
|
163 |
+
for i in range(frame_count):
|
164 |
+
frame_path = os.path.join(frames_dir, f'frame_{i:04d}.jpg')
|
165 |
+
frame = cv2.imread(frame_path)
|
166 |
+
results = model(frame)
|
167 |
+
results_output_path = os.path.join(results_dir, f'results_{i:04d}.jpg')
|
168 |
+
cv2.imwrite(results_output_path, results.render()[0])
|
169 |
+
|
170 |
+
# Create the output video from the processed frames
|
171 |
+
frame_files = sorted(os.listdir(results_dir))
|
172 |
+
frame_path = os.path.join(results_dir, frame_files[0])
|
173 |
+
frame = cv2.imread(frame_path)
|
174 |
+
height, width, _ = frame.shape
|
175 |
+
|
176 |
+
video_output_path = 'output_video.mp4' # Replace with your desired output video path
|
177 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can change the codec as needed
|
178 |
+
video_writer = cv2.VideoWriter(video_output_path, fourcc, frame_rate, (width, height))
|
179 |
+
|
180 |
+
for frame_file in frame_files:
|
181 |
+
frame_path = os.path.join(results_dir, frame_file)
|
182 |
+
frame = cv2.imread(frame_path)
|
183 |
+
video_writer.write(frame)
|
184 |
+
|
185 |
video_writer.release()
|
186 |
|
187 |
+
# Clean up the temporary directories
|
188 |
+
os.rmdir(frames_dir)
|
189 |
+
os.rmdir(results_dir)
|
190 |
+
|
191 |
return video_output_path
|
192 |
|
193 |
|
|
|
212 |
tabbed_interface = gr.TabbedInterface(interfaces, ["Image", "Video"])
|
213 |
|
214 |
# Launch the tabbed interface
|
215 |
+
tabbed_interface.launch(debug=True)
|