|
|
import gradio as gr |
|
|
from ultralytics import YOLO |
|
|
import cv2 |
|
|
import numpy as np |
|
|
|
|
|
model = YOLO("yolov8n-seg.pt") |
|
|
|
|
|
|
|
|
def detect_objects_image(img): |
|
|
results = model(img) |
|
|
annotated_frame = results[0].plot() |
|
|
return annotated_frame |
|
|
|
|
|
import tempfile |
|
|
|
|
|
def detect_objects_video(video): |
|
|
|
|
|
video_path = video.name if hasattr(video, 'name') else video |
|
|
|
|
|
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) |
|
|
cap = cv2.VideoCapture(video_path) |
|
|
|
|
|
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
|
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
|
fps = cap.get(cv2.CAP_PROP_FPS) |
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
|
out = cv2.VideoWriter(temp_output.name, 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 temp_output.name |
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Blocks(theme='earneleh/paris') |
|
|
|
|
|
|
|
|
image_input = gr.Image(type="numpy", label="Image à analyser") |
|
|
video_input = gr.Video(label="Vidéo à analyser") |
|
|
image_output = gr.Image(type="numpy", label="Image annotée") |
|
|
video_output = gr.Video(label="Vidéo annotée") |
|
|
|
|
|
interface1 = gr.Interface(fn=detect_objects_image, |
|
|
inputs=image_input, |
|
|
outputs=image_output, |
|
|
title="Détection sur Image", description = """Analyse et détection d’objets sur une seule image à l’aide du modèle YOLOv8. |
|
|
|
|
|
Téléversez une image, et le modèle détectera les objets présents, les annotera directement sur l’image affichée.""" |
|
|
) |
|
|
interface2 = gr.Interface(fn=detect_objects_video, |
|
|
inputs=video_input, |
|
|
outputs=video_output, |
|
|
title="Détection sur Vidéo",description = """Détection et annotation des objets présents dans une vidéo. |
|
|
|
|
|
Téléversez une vidéo, chaque image est analysée en temps réel, annotée avec les objets détectés, et une vidéo annotée est renvoyée.""" |
|
|
) |
|
|
with demo: |
|
|
gr.TabbedInterface( |
|
|
[interface1 ,interface2], |
|
|
['Image', 'Video'] |
|
|
) |
|
|
|
|
|
demo.launch() |