Spaces:
Sleeping
Sleeping
File size: 2,061 Bytes
8c07326 1bff35a 8c07326 1bff35a 8c07326 1bff35a 8c07326 1bff35a 8c07326 1bff35a 8c07326 537946f 1bff35a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import gradio as gr
from ultralytics import YOLO
import cv2
import numpy as np
import tempfile
# Charger le modèle YOLOv8
model = YOLO("yolov8n.pt")
# 🔹 Fonction pour détecter sur image
def detect_objects_image(img):
results = model(img)
annotated_frame = results[0].plot()
return annotated_frame
# 🔹 Fonction pour détecter sur vidéo
def detect_objects_video(video_input):
cap = cv2.VideoCapture(video_input)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
output_path = temp_output.name
temp_output.close()
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame)
annotated_frame = results[0].plot()
out.write(annotated_frame)
cap.release()
out.release()
return output_path
# Créer l'application avec Gradio Blocks
with gr.Blocks(title="YOLOv8 - Détection d'objets sur Image et Vidéo") as app:
gr.Markdown("## 🧠 Détection d'objets avec YOLOv8 (Image & Vidéo)")
gr.Markdown("Choisissez une option ci-dessous pour détecter les objets dans une image ou une vidéo.")
with gr.Tab("📷 Détection sur Image"):
img_input = gr.Image(type="numpy", label="Image à analyser")
img_output = gr.Image(type="numpy", label="Image annotée")
img_button = gr.Button("Lancer la détection")
img_button.click(fn=detect_objects_image, inputs=img_input, outputs=img_output)
with gr.Tab("🎥 Détection sur Vidéo"):
vid_input = gr.Video(label="Vidéo à analyser")
vid_output = gr.Video(label="Vidéo annotée")
vid_button = gr.Button("Lancer la détection")
vid_button.click(fn=detect_objects_video, inputs=vid_input, outputs=vid_output)
# Lancer l'application
app.launch() |