Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -138,38 +138,42 @@ def detect_image(image):
|
|
| 138 |
|
| 139 |
def detect_video(video_path):
|
| 140 |
video = cv2.VideoCapture(video_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
frame_count = 0
|
| 142 |
while True:
|
| 143 |
success, frame = video.read()
|
| 144 |
if not success:
|
| 145 |
break
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
frame_count += 1
|
| 150 |
-
video.release()
|
| 151 |
-
cv2.destroyAllWindows()
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
image_dir = '.' # Replace with the directory containing the image files (frames)
|
| 156 |
-
image_files = sorted(os.listdir(image_dir))
|
| 157 |
-
|
| 158 |
-
image_path = os.path.join(image_dir, image_files[0])
|
| 159 |
-
frame = cv2.imread(image_path)
|
| 160 |
-
height, width, _ = frame.shape
|
| 161 |
-
|
| 162 |
-
video_output_path = 'output_video.mp4' # Replace with your desired output video path
|
| 163 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can change the codec as needed
|
| 164 |
-
video_writer = cv2.VideoWriter(video_output_path, fourcc, frame_rate, (width, height))
|
| 165 |
-
|
| 166 |
-
for image_file in image_files:
|
| 167 |
-
image_path = os.path.join(image_dir, image_file)
|
| 168 |
-
frame = cv2.imread(image_path)
|
| 169 |
-
video_writer.write(frame)
|
| 170 |
-
|
| 171 |
video_writer.release()
|
| 172 |
-
|
|
|
|
|
|
|
| 173 |
|
| 174 |
# Create Gradio interfaces for different modes
|
| 175 |
img_interface = gr.Interface(
|
|
|
|
| 138 |
|
| 139 |
def detect_video(video_path):
|
| 140 |
video = cv2.VideoCapture(video_path)
|
| 141 |
+
frame_rate = video.get(cv2.CAP_PROP_FPS) # Get the frame rate of the input video
|
| 142 |
+
|
| 143 |
+
# Get the dimensions of the video frames
|
| 144 |
+
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 145 |
+
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 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 |
+
# Process the frame using your model
|
| 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 |
|
| 178 |
# Create Gradio interfaces for different modes
|
| 179 |
img_interface = gr.Interface(
|