Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from PIL import Image
|
|
3 |
import gradio as gr
|
4 |
import cv2
|
5 |
import tempfile
|
|
|
6 |
|
7 |
# Load YOLOv8 model
|
8 |
model = YOLO("best.pt") # Ensure best.pt is in the same directory
|
@@ -17,7 +18,7 @@ def predict_image(image):
|
|
17 |
def predict_video(video):
|
18 |
try:
|
19 |
# Save video to a temporary file
|
20 |
-
temp_video_path = tempfile.NamedTemporaryFile(suffix=".mp4").name
|
21 |
with open(temp_video_path, "wb") as f:
|
22 |
f.write(video.read())
|
23 |
|
@@ -32,8 +33,8 @@ def predict_video(video):
|
|
32 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
33 |
|
34 |
# Define codec and create a video writer
|
35 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
36 |
-
output_path = tempfile.NamedTemporaryFile(suffix=".mp4").name
|
37 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
38 |
|
39 |
while cap.isOpened():
|
@@ -52,7 +53,11 @@ def predict_video(video):
|
|
52 |
cap.release()
|
53 |
out.release()
|
54 |
|
55 |
-
|
|
|
|
|
|
|
|
|
56 |
|
57 |
except Exception as e:
|
58 |
return f"An error occurred while processing the video: {str(e)}"
|
|
|
3 |
import gradio as gr
|
4 |
import cv2
|
5 |
import tempfile
|
6 |
+
import os
|
7 |
|
8 |
# Load YOLOv8 model
|
9 |
model = YOLO("best.pt") # Ensure best.pt is in the same directory
|
|
|
18 |
def predict_video(video):
|
19 |
try:
|
20 |
# Save video to a temporary file
|
21 |
+
temp_video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
22 |
with open(temp_video_path, "wb") as f:
|
23 |
f.write(video.read())
|
24 |
|
|
|
33 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
34 |
|
35 |
# Define codec and create a video writer
|
36 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use 'mp4v' for compatibility
|
37 |
+
output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
38 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
39 |
|
40 |
while cap.isOpened():
|
|
|
53 |
cap.release()
|
54 |
out.release()
|
55 |
|
56 |
+
# Ensure the output file exists and is playable
|
57 |
+
if os.path.exists(output_path):
|
58 |
+
return output_path
|
59 |
+
else:
|
60 |
+
return "Error: Annotated video could not be created."
|
61 |
|
62 |
except Exception as e:
|
63 |
return f"An error occurred while processing the video: {str(e)}"
|