Iralion commited on
Commit
1b56f1c
·
verified ·
1 Parent(s): 099dfb4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import shutil
4
+ import os
5
+
6
+ # Charger le modèle YOLOv8
7
+ model = YOLO("yolov8n.pt")
8
+
9
+ # 🔍 Détection sur image
10
+ def detect_objects_image(img):
11
+ results = model(img)
12
+ return results[0].plot()
13
+
14
+ # 🎥 Détection sur vidéo
15
+ import cv2
16
+ from ultralytics import YOLO
17
+ import os
18
+
19
+ # Charger modèle YOLOv8
20
+ model = YOLO("yolov8n.pt")
21
+
22
+ def detect_objects_video(video_path, output_path="video_sortie.mp4"):
23
+ cap = cv2.VideoCapture(video_path)
24
+
25
+ # Vérifier ouverture
26
+ if not cap.isOpened():
27
+ print("Erreur : impossible d'ouvrir la vidéo.")
28
+ return None
29
+
30
+ # Propriétés vidéo
31
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
32
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
33
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
34
+
35
+ # Définir l'écriture de la vidéo de sortie
36
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
37
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
38
+
39
+ while True:
40
+ ret, frame = cap.read()
41
+ if not ret:
42
+ break
43
+
44
+ # Appliquer YOLOv8 sur chaque frame
45
+ results = model(frame)
46
+ annotated_frame = results[0].plot()
47
+
48
+ # Écrire le frame annoté
49
+ out.write(annotated_frame)
50
+
51
+ cap.release()
52
+ out.release()
53
+ print(f"Vidéo enregistrée : {output_path}")
54
+ return output_path
55
+
56
+ # Lancer l'interface avec deux onglets
57
+ with gr.Blocks() as demo:
58
+ gr.Markdown("## 📸 Détection d’objets avec YOLOv8 - Image et Vidéo")
59
+
60
+ with gr.Tab("🖼️ Image"):
61
+ image_input = gr.Image(type="numpy", label="Importer une image")
62
+ image_output = gr.Image(type="numpy", label="Image avec détection")
63
+ image_btn = gr.Button("Analyser l'image")
64
+ image_btn.click(fn=detect_objects_image, inputs=image_input, outputs=image_output)
65
+
66
+ with gr.Tab("🎞️ Vidéo"):
67
+ video_input = gr.Video(label="Importer une vidéo (.mp4)")
68
+ video_output = gr.Video(label="Vidéo traitée")
69
+ video_btn = gr.Button("Analyser la vidéo")
70
+ video_btn.click(fn=detect_objects_video, inputs=video_input, outputs=video_output)
71
+
72
+ demo.launch()