Sandouno2003 commited on
Commit
49891b6
·
verified ·
1 Parent(s): 23924e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ import numpy as np
5
+ # Charger le modèle YOLOv8 pré-entraîné
6
+ model = YOLO("yolov8n.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
+
15
+ def detect_objects_video(video_path):
16
+ cap = cv2.VideoCapture(video_path)
17
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
18
+ out_path = "annotated_video.mp4"
19
+ out = cv2.VideoWriter(out_path, fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
20
+
21
+ while cap.isOpened():
22
+ ret, frame = cap.read()
23
+ if not ret:
24
+ break
25
+ result = model(frame)
26
+ annotated = result[0].plot()
27
+ out.write(annotated)
28
+
29
+ cap.release()
30
+ out.release()
31
+ return out_path
32
+
33
+ demo = gr.Blocks()
34
+
35
+ #Interface Gradio
36
+
37
+ image_input = gr.Image(type="numpy", label="Image à analyser")
38
+ video_input = gr.Image(label="Video à analyser")
39
+
40
+ image_output = gr.Image(type="numpy", label="Image annotée")
41
+ video_output = gr.Image(label="Video annotée")
42
+
43
+ interface1 = gr.Interface(fn=detect_objects_image, inputs=image_input, outputs=image_output, title="Détection sur Image")
44
+ interface2 = gr.Interface(fn=detect_objects_video, inputs=video_input, outputs=video_output, title="Détection sur Video")
45
+
46
+ with demo:
47
+ gr.TabbedInterface(
48
+ [interface1, interface2],
49
+ ['Image', 'Video']
50
+ )
51
+ demo.launch()