oumarodd commited on
Commit
1bff35a
·
verified ·
1 Parent(s): 333e82c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -46
app.py CHANGED
@@ -2,49 +2,28 @@ 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
- demo = gr.Blocks()
15
-
16
- #Interface Gradio
17
- image_input = gr.Image(type="numpy", label="Image à analyser")
18
-
19
- image_output = gr.Image(type="numpy", label="Image annotée")
20
-
21
- interface = gr.Interface(fn=detect_objects_image, inputs=image_input, outputs=image_output, title="Détection sur Image")
22
-
23
- interface.launch()
24
-
25
- import gradio as gr
26
- from ultralytics import YOLO
27
- import cv2
28
- import tempfile
29
- import os
30
-
31
- # Charger le modèle YOLOv8 pré-entraîné
32
- model = YOLO("yolov8n.pt")
33
-
34
- # Fonction pour la détection sur vidéo
35
  def detect_objects_video(video_input):
36
- # Lire la vidéo d'entrée
37
  cap = cv2.VideoCapture(video_input)
38
  fps = cap.get(cv2.CAP_PROP_FPS)
39
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
40
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
41
 
42
- # Créer un fichier temporaire pour enregistrer la sortie
43
  temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
44
  output_path = temp_output.name
45
  temp_output.close()
46
 
47
- # Définir le writer vidéo
48
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
49
  out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
50
 
@@ -52,31 +31,14 @@ def detect_objects_video(video_input):
52
  ret, frame = cap.read()
53
  if not ret:
54
  break
55
-
56
- # Détection avec YOLOv8
57
  results = model(frame)
58
- annotated_frame = results[0].plot() # Annoter les résultats
59
-
60
  out.write(annotated_frame)
61
 
62
  cap.release()
63
  out.release()
64
-
65
  return output_path
66
 
67
- # Interface Gradio pour la vidéo
68
- video_input = gr.Video(label="Vidéo à analyser")
69
- video_output = gr.Video(label="Vidéo annotée")
70
-
71
- interface = gr.Interface(
72
- fn=detect_objects_video,
73
- inputs=video_input,
74
- outputs=video_output,
75
- title="Détection d'objets sur Vidéo"
76
- )
77
-
78
- interface.launch()
79
-
80
  # Créer l'application avec Gradio Blocks
81
  with gr.Blocks(title="YOLOv8 - Détection d'objets sur Image et Vidéo") as app:
82
  gr.Markdown("## 🧠 Détection d'objets avec YOLOv8 (Image & Vidéo)")
@@ -93,3 +55,6 @@ with gr.Blocks(title="YOLOv8 - Détection d'objets sur Image et Vidéo") as app:
93
  vid_output = gr.Video(label="Vidéo annotée")
94
  vid_button = gr.Button("Lancer la détection")
95
  vid_button.click(fn=detect_objects_video, inputs=vid_input, outputs=vid_output)
 
 
 
 
2
  from ultralytics import YOLO
3
  import cv2
4
  import numpy as np
5
+ import tempfile
6
+
7
+ # Charger le modèle YOLOv8
8
  model = YOLO("yolov8n.pt")
9
 
10
+ # 🔹 Fonction pour détecter sur image
11
  def detect_objects_image(img):
12
+ results = model(img)
13
+ annotated_frame = results[0].plot()
14
  return annotated_frame
15
 
16
+ # 🔹 Fonction pour détecter sur vidéo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def detect_objects_video(video_input):
 
18
  cap = cv2.VideoCapture(video_input)
19
  fps = cap.get(cv2.CAP_PROP_FPS)
20
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
21
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
22
 
 
23
  temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
24
  output_path = temp_output.name
25
  temp_output.close()
26
 
 
27
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
28
  out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
29
 
 
31
  ret, frame = cap.read()
32
  if not ret:
33
  break
 
 
34
  results = model(frame)
35
+ annotated_frame = results[0].plot()
 
36
  out.write(annotated_frame)
37
 
38
  cap.release()
39
  out.release()
 
40
  return output_path
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  # Créer l'application avec Gradio Blocks
43
  with gr.Blocks(title="YOLOv8 - Détection d'objets sur Image et Vidéo") as app:
44
  gr.Markdown("## 🧠 Détection d'objets avec YOLOv8 (Image & Vidéo)")
 
55
  vid_output = gr.Video(label="Vidéo annotée")
56
  vid_button = gr.Button("Lancer la détection")
57
  vid_button.click(fn=detect_objects_video, inputs=vid_input, outputs=vid_output)
58
+
59
+ # Lancer l'application
60
+ app.launch()