Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Charger le modèle YOLOv8 pré-entraîné
|
| 7 |
+
model = YOLO("yolov8n.pt")
|
| 8 |
+
|
| 9 |
+
# Fonction pour la détection sur image
|
| 10 |
+
def detect_objects_image(img):
|
| 11 |
+
results = model(img) # Détection
|
| 12 |
+
annotated_frame = results[0].plot() # Annoter les résultats
|
| 13 |
+
return annotated_frame
|
| 14 |
+
|
| 15 |
+
# Fonction pour la détection sur vidéo (frame par frame)
|
| 16 |
+
def detect_objects_video(video):
|
| 17 |
+
cap = cv2.VideoCapture(video)
|
| 18 |
+
frames = []
|
| 19 |
+
|
| 20 |
+
while cap.isOpened():
|
| 21 |
+
ret, frame = cap.read()
|
| 22 |
+
if not ret:
|
| 23 |
+
break
|
| 24 |
+
results = model(frame)
|
| 25 |
+
annotated = results[0].plot()
|
| 26 |
+
frames.append(annotated)
|
| 27 |
+
|
| 28 |
+
cap.release()
|
| 29 |
+
|
| 30 |
+
# Convertir la liste de frames en vidéo
|
| 31 |
+
output_path = "annotated_video.mp4"
|
| 32 |
+
height, width, _ = frames[0].shape
|
| 33 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20, (width, height))
|
| 34 |
+
for frame in frames:
|
| 35 |
+
out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
|
| 36 |
+
out.release()
|
| 37 |
+
|
| 38 |
+
return output_path
|
| 39 |
+
|
| 40 |
+
# Interfaces Gradio
|
| 41 |
+
image_interface = gr.Interface(
|
| 42 |
+
fn=detect_objects_image,
|
| 43 |
+
inputs=gr.Image(type="numpy", label="Image à analyser"),
|
| 44 |
+
outputs=gr.Image(type="numpy", label="Image annotée"),
|
| 45 |
+
title="Détection sur Image"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
video_interface = gr.Interface(
|
| 49 |
+
fn=detect_objects_video,
|
| 50 |
+
inputs=gr.Video(label="Vidéo à analyser"),
|
| 51 |
+
outputs=gr.Video(label="Vidéo annotée"),
|
| 52 |
+
title="Détection sur Vidéo"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Interface avec onglets
|
| 56 |
+
demo = gr.TabbedInterface(
|
| 57 |
+
interface_list=[image_interface, video_interface],
|
| 58 |
+
tab_names=["Image", "Vidéo"],
|
| 59 |
+
theme='JohnSmith9982/small_and_pretty'
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
demo.launch()
|