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 |
+
# charger le modèle
|
6 |
+
model = YOLO("yolov8n-seg.pt")
|
7 |
+
|
8 |
+
# Fonction pour la détection sur image
|
9 |
+
def detect_objects_image(img):
|
10 |
+
results = model(img) # Détection
|
11 |
+
annotated_frame = results[0].plot() # Annoter les résultats
|
12 |
+
return annotated_frame
|
13 |
+
|
14 |
+
import tempfile
|
15 |
+
# Fonction pour la détection sur vidéo
|
16 |
+
def detect_objects_video(video):
|
17 |
+
# Si l'entrée est une chaîne, utiliser telle quelle. Sinon, utiliser .name (cas Gradio)
|
18 |
+
video_path = video.name if hasattr(video, 'name') else video
|
19 |
+
|
20 |
+
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
21 |
+
cap = cv2.VideoCapture(video_path)
|
22 |
+
|
23 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
24 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
25 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
26 |
+
|
27 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
28 |
+
out = cv2.VideoWriter(temp_output.name, fourcc, fps, (width, height))
|
29 |
+
|
30 |
+
while True:
|
31 |
+
ret, frame = cap.read()
|
32 |
+
if not ret:
|
33 |
+
break
|
34 |
+
|
35 |
+
results = model(frame)
|
36 |
+
annotated_frame = results[0].plot()
|
37 |
+
out.write(annotated_frame)
|
38 |
+
|
39 |
+
cap.release()
|
40 |
+
out.release()
|
41 |
+
|
42 |
+
return temp_output.name
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
demo = gr.Blocks(theme='NoCrypt/miku')
|
47 |
+
|
48 |
+
#Interface Gradio
|
49 |
+
image_input = gr.Image(type='numpy',label="Image à analyser")
|
50 |
+
image_output = gr.Image(type = 'numpy', label="Image annotée")
|
51 |
+
|
52 |
+
video_input = gr.Video(label="Video à analyser")
|
53 |
+
video_output = gr.Video(label="Video annotée")
|
54 |
+
|
55 |
+
interface1 = gr.Interface(fn=detect_objects_image, inputs=image_input, outputs=image_output, title="Détection sur Image")
|
56 |
+
|
57 |
+
interface2 = gr.Interface(fn=detect_objects_video, inputs=video_input, outputs=video_output, title="Détection sur Video")
|
58 |
+
|
59 |
+
with demo:
|
60 |
+
gr.TabbedInterface([interface1, interface2], ['image detection', 'video detection'])
|
61 |
+
|
62 |
+
demo.launch()
|