greykingreys commited on
Commit
a9e441b
·
verified ·
1 Parent(s): 0c1b464

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ import os
5
+ import torch
6
+ import numpy as np
7
+ # Charger le modèle YOLOv8 pré-entraîné
8
+ model = YOLO("yolov8n.pt")
9
+
10
+ # Fonction pour la détection sur image
11
+ def detect_objects_image(img):
12
+ results = model(img) # Détection
13
+ annotated_frame = results[0].plot() # Annoter les résultats
14
+ return annotated_frame
15
+
16
+ import tempfile
17
+ # Fonction pour la détection sur vidéo
18
+ def detect_objects_video(video):
19
+ # Si l'entrée est une chaîne, utiliser telle quelle. Sinon, utiliser .name (cas Gradio)
20
+ video_path = video.name if hasattr(video, 'name') else video
21
+
22
+ temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
23
+ cap = cv2.VideoCapture(video_path)
24
+
25
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
26
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
27
+ fps = cap.get(cv2.CAP_PROP_FPS)
28
+
29
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
30
+ out = cv2.VideoWriter(temp_output.name, fourcc, fps, (width, height))
31
+
32
+ while True:
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 temp_output.name
45
+
46
+
47
+
48
+ demo = gr.Blocks(theme='NoCrypt/miku')
49
+
50
+ #Interface Gradio
51
+ image_input = gr.Image(type='numpy',label="Image à analyser")
52
+ image_output = gr.Image(type = 'numpy', label="Image annotée")
53
+
54
+ video_input = gr.Video(label="Video à analyser")
55
+ video_output = gr.Video(label="Video annotée")
56
+
57
+ interface1 = gr.Interface(fn=detect_objects_image, inputs=image_input, outputs=image_output, title="Détection sur Image")
58
+
59
+ interface2 = gr.Interface(fn=detect_objects_video, inputs=video_input, outputs=video_output, title="Détection sur Video")
60
+
61
+ with demo:
62
+ gr.TabbedInterface([interface1, interface2], ['image detection', 'video detection'])
63
+
64
+ demo.launch()