Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO # Importer YOLOv8
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
# Charger le modèle YOLOv8
|
| 6 |
model = YOLO('best.pt') # Remplace 'best.pt' par le chemin de ton modèle
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Fonction d'inférence
|
| 9 |
-
def detect(img):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Faire une prédiction avec YOLOv8
|
| 11 |
-
results = model(
|
| 12 |
-
|
| 13 |
-
# Si des objets sont détectés,
|
| 14 |
if results:
|
| 15 |
detections = results[0].boxes # Les résultats des détections
|
| 16 |
if len(detections) > 0:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
class_names = ['AMAZONE', 'BIOGUERRA','PORTE DU NON RETOUR', 'REVENANT', 'ZANGBETO']
|
| 21 |
-
class_name = class_names[class_index] # Nom de la classe prédite
|
| 22 |
-
confidence = format(detection.conf[0], ".2f") # Confiance de la détection
|
| 23 |
-
return f"Classe : {class_name}, Confiance : {confidence}"
|
| 24 |
else:
|
| 25 |
-
return
|
| 26 |
else:
|
| 27 |
-
return
|
| 28 |
|
| 29 |
# Interface utilisateur avec Gradio
|
| 30 |
title = "Orisha YOLOv8"
|
| 31 |
|
| 32 |
iface = gr.Interface(
|
| 33 |
fn=detect,
|
| 34 |
-
inputs=
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
title=title
|
| 37 |
)
|
| 38 |
|
|
@@ -40,6 +65,48 @@ iface = gr.Interface(
|
|
| 40 |
iface.launch(inline=False)
|
| 41 |
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# import gradio as gr
|
| 44 |
# import tensorflow as tf
|
| 45 |
# import numpy as np
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO # Importer YOLOv8
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
|
| 6 |
# Charger le modèle YOLOv8
|
| 7 |
model = YOLO('best.pt') # Remplace 'best.pt' par le chemin de ton modèle
|
| 8 |
|
| 9 |
+
# Fonction pour dessiner les boîtes de détection sur l'image
|
| 10 |
+
def draw_boxes(image, detections, class_names, confidence_threshold):
|
| 11 |
+
for detection in detections:
|
| 12 |
+
if detection.conf[0] >= confidence_threshold:
|
| 13 |
+
class_index = int(detection.cls[0]) # Obtenir l'indice de la classe
|
| 14 |
+
class_name = class_names[class_index] # Nom de la classe
|
| 15 |
+
confidence = format(detection.conf[0], ".2f") # Confiance
|
| 16 |
+
|
| 17 |
+
# Obtenir les coordonnées des boîtes
|
| 18 |
+
xyxy = detection.xyxy[0].cpu().numpy().astype(int)
|
| 19 |
+
(x1, y1, x2, y2) = xyxy
|
| 20 |
+
|
| 21 |
+
# Dessiner la boîte
|
| 22 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 23 |
+
label = f"{class_name} ({confidence})"
|
| 24 |
+
|
| 25 |
+
# Ajouter le texte (classe et confiance) au-dessus de la boîte
|
| 26 |
+
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 27 |
+
|
| 28 |
+
return image
|
| 29 |
+
|
| 30 |
# Fonction d'inférence
|
| 31 |
+
def detect(img, confidence_threshold):
|
| 32 |
+
# Convertir l'image PIL en format approprié pour OpenCV
|
| 33 |
+
img_cv = np.array(img)
|
| 34 |
+
img_cv = cv2.cvtColor(img_cv, cv2.COLOR_RGB2BGR)
|
| 35 |
+
|
| 36 |
# Faire une prédiction avec YOLOv8
|
| 37 |
+
results = model(img_cv) # YOLOv8 prend directement l'image en entrée
|
| 38 |
+
|
| 39 |
+
# Si des objets sont détectés, dessiner les boîtes
|
| 40 |
if results:
|
| 41 |
detections = results[0].boxes # Les résultats des détections
|
| 42 |
if len(detections) > 0:
|
| 43 |
+
class_names = ['AMAZONE', 'BIOGUERRA', 'PORTE DU NON RETOUR', 'REVENANT', 'ZANGBETO']
|
| 44 |
+
img_with_boxes = draw_boxes(img_cv, detections, class_names, confidence_threshold)
|
| 45 |
+
return cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB) # Convertir l'image en format RGB pour Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
else:
|
| 47 |
+
return img # Si aucune détection, retourner l'image originale
|
| 48 |
else:
|
| 49 |
+
return img # Si aucune détection, retourner l'image originale
|
| 50 |
|
| 51 |
# Interface utilisateur avec Gradio
|
| 52 |
title = "Orisha YOLOv8"
|
| 53 |
|
| 54 |
iface = gr.Interface(
|
| 55 |
fn=detect,
|
| 56 |
+
inputs=[
|
| 57 |
+
gr.Image(type="pil", image_mode='RGB', label="Image à détecter"), # Charger une image
|
| 58 |
+
gr.Slider(0.0, 1.0, value=0.25, step=0.01, label="Confiance minimale"), # Paramétrer la confiance
|
| 59 |
+
],
|
| 60 |
+
outputs=gr.Image(type="pil", label="Résultat avec détections"), # Afficher le résultat avec boîtes
|
| 61 |
title=title
|
| 62 |
)
|
| 63 |
|
|
|
|
| 65 |
iface.launch(inline=False)
|
| 66 |
|
| 67 |
|
| 68 |
+
# import gradio as gr
|
| 69 |
+
# from ultralytics import YOLO # Importer YOLOv8
|
| 70 |
+
# import numpy as np
|
| 71 |
+
|
| 72 |
+
# # Charger le modèle YOLOv8
|
| 73 |
+
# model = YOLO('best.pt') # Remplace 'best.pt' par le chemin de ton modèle
|
| 74 |
+
|
| 75 |
+
# # Fonction d'inférence
|
| 76 |
+
# def detect(img):
|
| 77 |
+
# # Faire une prédiction avec YOLOv8
|
| 78 |
+
# results = model(img) # YOLOv8 prend directement l'image en entrée
|
| 79 |
+
|
| 80 |
+
# # Si des objets sont détectés, renvoyer la classe de l'objet
|
| 81 |
+
# if results:
|
| 82 |
+
# detections = results[0].boxes # Les résultats des détections
|
| 83 |
+
# if len(detections) > 0:
|
| 84 |
+
# # Obtenir la classe de la détection avec la probabilité la plus élevée
|
| 85 |
+
# detection = detections[0] # On prend la première détection (peut être ajusté)
|
| 86 |
+
# class_index = int(detection.cls[0]) # Obtenir l'indice de la classe
|
| 87 |
+
# class_names = ['AMAZONE', 'BIOGUERRA','PORTE DU NON RETOUR', 'REVENANT', 'ZANGBETO']
|
| 88 |
+
# class_name = class_names[class_index] # Nom de la classe prédite
|
| 89 |
+
# confidence = format(detection.conf[0], ".2f") # Confiance de la détection
|
| 90 |
+
# return f"Classe : {class_name}, Confiance : {confidence}"
|
| 91 |
+
# else:
|
| 92 |
+
# return "Aucune détection"
|
| 93 |
+
# else:
|
| 94 |
+
# return "Aucune détection"
|
| 95 |
+
|
| 96 |
+
# # Interface utilisateur avec Gradio
|
| 97 |
+
# title = "Orisha YOLOv8"
|
| 98 |
+
|
| 99 |
+
# iface = gr.Interface(
|
| 100 |
+
# fn=detect,
|
| 101 |
+
# inputs=gr.Image(type="pil", image_mode='RGB'), # Charger une image
|
| 102 |
+
# outputs=gr.Textbox(label="Résultat", lines=2), # Afficher le résultat
|
| 103 |
+
# title=title
|
| 104 |
+
# )
|
| 105 |
+
|
| 106 |
+
# # Lancer l'application
|
| 107 |
+
# iface.launch(inline=False)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
# import gradio as gr
|
| 111 |
# import tensorflow as tf
|
| 112 |
# import numpy as np
|