Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,39 +14,33 @@ def detect_objects_image(img):
|
|
| 14 |
# 🎥 Détection sur vidéo
|
| 15 |
model = YOLO("yolov8n.pt")
|
| 16 |
|
| 17 |
-
def detect_objects_video(
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
print("Erreur : impossible d'ouvrir la vidéo.")
|
| 23 |
-
return None
|
| 24 |
|
| 25 |
-
|
| 26 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 27 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 28 |
-
fps
|
| 29 |
|
| 30 |
-
# Définir l'écriture de la vidéo de sortie
|
| 31 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 32 |
-
out = cv2.VideoWriter(
|
| 33 |
|
| 34 |
while True:
|
| 35 |
ret, frame = cap.read()
|
| 36 |
if not ret:
|
| 37 |
break
|
| 38 |
|
| 39 |
-
# Appliquer YOLOv8 sur chaque frame
|
| 40 |
results = model(frame)
|
| 41 |
annotated_frame = results[0].plot()
|
| 42 |
-
|
| 43 |
-
# Écrire le frame annoté
|
| 44 |
out.write(annotated_frame)
|
| 45 |
|
| 46 |
cap.release()
|
| 47 |
out.release()
|
| 48 |
-
|
| 49 |
-
return
|
| 50 |
|
| 51 |
# Lancer l'interface avec deux onglets
|
| 52 |
with gr.Blocks() as demo:
|
|
|
|
| 14 |
# 🎥 Détection sur vidéo
|
| 15 |
model = YOLO("yolov8n.pt")
|
| 16 |
|
| 17 |
+
def detect_objects_video(video):
|
| 18 |
+
# Si l'entrée est une chaîne, utiliser telle quelle. Sinon, utiliser .name (cas Gradio)
|
| 19 |
+
video_path = video.name if hasattr(video, 'name') else video
|
| 20 |
|
| 21 |
+
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 22 |
+
cap = cv2.VideoCapture(video_path)
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
|
|
| 25 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 26 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 27 |
|
|
|
|
| 28 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 29 |
+
out = cv2.VideoWriter(temp_output.name, fourcc, fps, (width, height))
|
| 30 |
|
| 31 |
while True:
|
| 32 |
ret, frame = cap.read()
|
| 33 |
if not ret:
|
| 34 |
break
|
| 35 |
|
|
|
|
| 36 |
results = model(frame)
|
| 37 |
annotated_frame = results[0].plot()
|
|
|
|
|
|
|
| 38 |
out.write(annotated_frame)
|
| 39 |
|
| 40 |
cap.release()
|
| 41 |
out.release()
|
| 42 |
+
|
| 43 |
+
return temp_output.name
|
| 44 |
|
| 45 |
# Lancer l'interface avec deux onglets
|
| 46 |
with gr.Blocks() as demo:
|