abdoulayee commited on
Commit
a2b6456
·
verified ·
1 Parent(s): f4ff5f6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
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
+ import tempfile
6
+ import os
7
+
8
+ # Charger le modèle YOLOv8 pré-entraîné
9
+ model = YOLO("yolov8n.pt")
10
+
11
+ # Fonction pour la détection sur image
12
+ def detect_objects_image(img):
13
+ results = model(img) # Détection
14
+ annotated_frame = results[0].plot() # Annoter les résultats
15
+ return annotated_frame
16
+
17
+ # Fonction pour la détection sur vidéo
18
+ def detect_objects_video(video):
19
+ # Lire la vidéo uploadée
20
+ cap = cv2.VideoCapture(video)
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
+ # Créer un fichier temporaire pour enregistrer la sortie
26
+ temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
27
+ output_path = temp_output.name
28
+
29
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
30
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
31
+
32
+ while cap.isOpened():
33
+ ret, frame = cap.read()
34
+ if not ret:
35
+ break
36
+
37
+ results = model(frame)
38
+ annotated_frame = results[0].plot()
39
+ out.write(annotated_frame)
40
+
41
+ cap.release()
42
+ out.release()
43
+
44
+ return output_path
45
+
46
+ # Interface Gradio
47
+ with gr.Blocks() as demo:
48
+ gr.Markdown("## Détection d'objets avec YOLOv8")
49
+
50
+ with gr.Tab("Image"):
51
+ image_input = gr.Image(type="numpy", label="Image à analyser")
52
+ image_output = gr.Image(type="numpy", label="Image annotée")
53
+ image_btn = gr.Button("Analyser l'image")
54
+ image_btn.click(fn=detect_objects_image, inputs=image_input, outputs=image_output)
55
+
56
+ with gr.Tab("Vidéo"):
57
+ video_input = gr.Video(label="Vidéo à analyser")
58
+ video_output = gr.Video(label="Vidéo annotée")
59
+ video_btn = gr.Button("Analyser la vidéo")
60
+ video_btn.click(fn=detect_objects_video, inputs=video_input, outputs=video_output)
61
+
62
+ demo.launch()