Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Charger le modèle YOLOv8
|
| 7 |
+
model = YOLO("yolov8n.pt")
|
| 8 |
+
|
| 9 |
+
# Fonction pour la détection sur image
|
| 10 |
+
def detect_objects_image(img):
|
| 11 |
+
results = model(img)
|
| 12 |
+
annotated_frame = results[0].plot()
|
| 13 |
+
return annotated_frame
|
| 14 |
+
|
| 15 |
+
# Fonction pour la détection sur vidéo
|
| 16 |
+
def detect_objects_video(video_path):
|
| 17 |
+
# video_path est une chaîne de caractères (chemin du fichier)
|
| 18 |
+
cap = cv2.VideoCapture(video_path)
|
| 19 |
+
|
| 20 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 21 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 22 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 23 |
+
|
| 24 |
+
# Créer un fichier temporaire de sortie
|
| 25 |
+
output_path = "video_result.mp4"
|
| 26 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 27 |
+
|
| 28 |
+
while True:
|
| 29 |
+
ret, frame = cap.read()
|
| 30 |
+
if not ret:
|
| 31 |
+
break
|
| 32 |
+
results = model(frame)
|
| 33 |
+
annotated_frame = results[0].plot()
|
| 34 |
+
out.write(annotated_frame)
|
| 35 |
+
|
| 36 |
+
cap.release()
|
| 37 |
+
out.release()
|
| 38 |
+
|
| 39 |
+
return output_path # retourne le chemin de la vidéo traitée
|
| 40 |
+
|
| 41 |
+
# Interface Gradio
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("## Détection d'objets avec YOLOv8")
|
| 44 |
+
|
| 45 |
+
with gr.Tab("Image"):
|
| 46 |
+
image_input = gr.Image(type="numpy", label="Image à analyser")
|
| 47 |
+
image_output = gr.Image(type="numpy", label="Image annotée")
|
| 48 |
+
image_btn = gr.Button("Analyser l'image")
|
| 49 |
+
image_btn.click(fn=detect_objects_image, inputs=image_input, outputs=image_output)
|
| 50 |
+
|
| 51 |
+
with gr.Tab("Vidéo"):
|
| 52 |
+
# Utilise gr.Video sans le paramètre 'type', Gradio gère automatiquement
|
| 53 |
+
video_input = gr.Video(label="Vidéo à analyser")
|
| 54 |
+
video_output = gr.Video(label="Vidéo annotée")
|
| 55 |
+
video_btn = gr.Button("Analyser la vidéo")
|
| 56 |
+
video_btn.click(fn=detect_objects_video, inputs=video_input, outputs=video_output)
|
| 57 |
+
|
| 58 |
+
demo.launch()
|