Iralion commited on
Commit
99371c4
·
verified ·
1 Parent(s): 246990d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ import numpy as np
5
+ import tempfile
6
+ # charger le modèle
7
+ model = YOLO("yolov8n-seg.pt")
8
+
9
+ # Fonction pour la détection sur image
10
+ def detect_objects_image(img):
11
+ results = model(img) # Détection
12
+ annotated_frame = results[0].plot() # Annoter les résultats
13
+ return annotated_frame
14
+ def detect_objects_video(video):
15
+ # Si l'entrée est une chaîne, utiliser telle quelle. Sinon, utiliser .name (cas Gradio)
16
+ video_path = video.name if hasattr(video, 'name') else video
17
+
18
+ temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
19
+ cap = cv2.VideoCapture(video_path)
20
+
21
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
22
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
23
+ fps = cap.get(cv2.CAP_PROP_FPS)
24
+
25
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
26
+ out = cv2.VideoWriter(temp_output.name, fourcc, fps, (width, height))
27
+
28
+ while True:
29
+ ret, frame = cap.read()
30
+ if not ret:
31
+ break
32
+
33
+ results = model(frame)
34
+ annotated_frame = results[0].plot()
35
+ out.write(annotated_frame)
36
+
37
+ cap.release()
38
+ out.release()
39
+
40
+ return temp_output.name
41
+
42
+
43
+ demo = gr.Blocks()
44
+
45
+ #Interface Gradio
46
+ image_input = gr.Image(type="numpy", label="Image à analyser")
47
+
48
+ image_output = gr.Image(type="numpy", label="Image annotée")
49
+
50
+
51
+ interface = gr.Interface(fn=detect_objects_image, inputs=image_input, outputs=image_output, title="Détection sur Image")
52
+
53
+ interface.launch()