oumarodd commited on
Commit
cece668
·
verified ·
1 Parent(s): cda65b3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ import tempfile
5
+
6
+ # Charger le modèle de segmentation
7
+ model = YOLO("yolov8n-seg.pt")
8
+
9
+ # 🔹 Fonction de détection par segmentation sur image
10
+ def detect_segmentation_image(img):
11
+ results = model(img)
12
+ annotated_img = results[0].plot()
13
+ return annotated_img
14
+
15
+ # 🔹 Fonction de détection par segmentation sur vidéo
16
+ def detect_segmentation_video(video_path):
17
+ cap = cv2.VideoCapture(video_path)
18
+ fps = cap.get(cv2.CAP_PROP_FPS)
19
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
20
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
21
+
22
+ # Créer un fichier vidéo temporaire pour la sortie
23
+ temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
24
+ output_path = temp_output.name
25
+ temp_output.close()
26
+
27
+ # Définir le codec et le writer
28
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
29
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
30
+
31
+ while True:
32
+ ret, frame = cap.read()
33
+ if not ret:
34
+ break
35
+
36
+ results = model(frame)
37
+ annotated_frame = results[0].plot()
38
+ out.write(annotated_frame)
39
+
40
+ cap.release()
41
+ out.release()
42
+
43
+ return output_path
44
+
45
+ # 🔧 Interface Gradio combinée avec onglets
46
+ with gr.Blocks(title="YOLOv8n-seg - Détection par Segmentation") as app:
47
+ gr.Markdown("## 🧠 YOLOv8n Segmentation - Image & Vidéo")
48
+ gr.Markdown("Détection par segmentation automatique avec le modèle **YOLOv8n-seg**.")
49
+
50
+ with gr.Tab("📷 Segmentation sur Image"):
51
+ image_input = gr.Image(type="numpy", label="Importer une image")
52
+ image_output = gr.Image(type="numpy", label="Image segmentée")
53
+ image_button = gr.Button("Lancer la détection")
54
+ image_button.click(fn=detect_segmentation_image, inputs=image_input, outputs=image_output)
55
+
56
+ with gr.Tab("🎥 Segmentation sur Vidéo"):
57
+ video_input = gr.Video(label="Importer une vidéo")
58
+ video_output = gr.Video(label="Vidéo segmentée")
59
+ video_button = gr.Button("Lancer la détection")
60
+ video_button.click(fn=detect_segmentation_video, inputs=video_input, outputs=video_output)
61
+
62
+ # 🚀 Lancement de l'application Gradio
63
+ app.launch()