Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,3 @@
|
|
| 1 |
-
"""
|
| 2 |
-
🎯 Application Gradio pour YOLOv3 Object Detection - Pascal VOC
|
| 3 |
-
Déployée sur Hugging Face Spaces
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
| 8 |
import cv2
|
|
@@ -41,7 +36,6 @@ from utils import cells_to_bboxes, non_max_suppression
|
|
| 41 |
class YOLOv3Detector:
|
| 42 |
def __init__(self, checkpoint_path):
|
| 43 |
"""Initialise le détecteur YOLOv3"""
|
| 44 |
-
print(f"🔧 Device: {DEVICE}")
|
| 45 |
|
| 46 |
# Charger le modèle
|
| 47 |
self.model = YOLOv3(num_classes=NUM_CLASSES).to(DEVICE)
|
|
@@ -59,7 +53,6 @@ class YOLOv3Detector:
|
|
| 59 |
np.random.seed(42)
|
| 60 |
self.colors = np.random.randint(0, 255, size=(len(PASCAL_CLASSES), 3), dtype=np.uint8)
|
| 61 |
|
| 62 |
-
print("✅ Modèle chargé avec succès!")
|
| 63 |
|
| 64 |
def preprocess_image(self, image):
|
| 65 |
"""Prétraite l'image pour le modèle"""
|
|
@@ -162,22 +155,19 @@ class YOLOv3Detector:
|
|
| 162 |
|
| 163 |
|
| 164 |
# Télécharger le modèle depuis Hugging Face
|
| 165 |
-
print("📥 Téléchargement du modèle depuis Hugging Face...")
|
| 166 |
checkpoint_path = hf_hub_download(
|
| 167 |
repo_id="nathbns/yolov3_from_scratch",
|
| 168 |
filename="checkpoint.pth.tar"
|
| 169 |
)
|
| 170 |
-
print(f"✅ Modèle téléchargé: {checkpoint_path}")
|
| 171 |
|
| 172 |
# Initialiser le détecteur
|
| 173 |
-
print("🚀 Chargement du modèle...")
|
| 174 |
detector = YOLOv3Detector(checkpoint_path)
|
| 175 |
|
| 176 |
|
| 177 |
def predict(image, conf_threshold, iou_threshold):
|
| 178 |
"""Fonction de prédiction pour Gradio"""
|
| 179 |
if image is None:
|
| 180 |
-
return None, "
|
| 181 |
|
| 182 |
# Détecter
|
| 183 |
boxes = detector.detect(image, conf_threshold, iou_threshold)
|
|
@@ -187,9 +177,9 @@ def predict(image, conf_threshold, iou_threshold):
|
|
| 187 |
|
| 188 |
# Texte des détections
|
| 189 |
if detections:
|
| 190 |
-
detection_text = f"
|
| 191 |
else:
|
| 192 |
-
detection_text = "
|
| 193 |
|
| 194 |
return result_image, detection_text
|
| 195 |
|
|
@@ -198,23 +188,15 @@ def predict(image, conf_threshold, iou_threshold):
|
|
| 198 |
with gr.Blocks(title="YOLOv3 Object Detection", theme=gr.themes.Soft()) as demo:
|
| 199 |
gr.Markdown(
|
| 200 |
"""
|
| 201 |
-
#
|
| 202 |
-
|
| 203 |
-
Uploadez une image pour détecter des objets parmi **20 classes Pascal VOC**.
|
| 204 |
-
|
| 205 |
-
**Classes détectables:** personne, vélo, voiture, moto, avion, bus, train, camion, bateau,
|
| 206 |
-
feu de circulation, bouche d'incendie, panneau stop, parcomètre, banc, oiseau, chat, chien,
|
| 207 |
-
cheval, mouton, vache, éléphant, ours, zèbre, girafe, sac à dos, parapluie, etc.
|
| 208 |
-
|
| 209 |
-
---
|
| 210 |
"""
|
| 211 |
)
|
| 212 |
|
| 213 |
with gr.Row():
|
| 214 |
with gr.Column():
|
| 215 |
-
input_image = gr.Image(type="pil", label="
|
| 216 |
|
| 217 |
-
with gr.Accordion("
|
| 218 |
conf_slider = gr.Slider(
|
| 219 |
minimum=0.1,
|
| 220 |
maximum=1.0,
|
|
@@ -232,11 +214,11 @@ with gr.Blocks(title="YOLOv3 Object Detection", theme=gr.themes.Soft()) as demo:
|
|
| 232 |
info="Plus élevé = plus de boîtes qui se chevauchent"
|
| 233 |
)
|
| 234 |
|
| 235 |
-
detect_btn = gr.Button("
|
| 236 |
|
| 237 |
with gr.Column():
|
| 238 |
-
output_image = gr.Image(label="
|
| 239 |
-
output_text = gr.Markdown(label="
|
| 240 |
|
| 241 |
# Action
|
| 242 |
detect_btn.click(
|
|
@@ -251,33 +233,6 @@ with gr.Blocks(title="YOLOv3 Object Detection", theme=gr.themes.Soft()) as demo:
|
|
| 251 |
inputs=[input_image, conf_slider, iou_slider],
|
| 252 |
outputs=[output_image, output_text]
|
| 253 |
)
|
| 254 |
-
|
| 255 |
-
gr.Markdown(
|
| 256 |
-
"""
|
| 257 |
-
---
|
| 258 |
-
|
| 259 |
-
### 📊 Informations sur le modèle
|
| 260 |
-
|
| 261 |
-
- **Architecture:** YOLOv3 (Darknet-53 backbone)
|
| 262 |
-
- **Dataset:** Pascal VOC (16 551 images d'entraînement)
|
| 263 |
-
- **Epochs:** 100
|
| 264 |
-
- **mAP @ 0.5 IoU:** ~38.3%
|
| 265 |
-
- **Classes:** 20 objets courants
|
| 266 |
-
- **Taille d'entrée:** 416x416
|
| 267 |
-
|
| 268 |
-
---
|
| 269 |
-
|
| 270 |
-
### 💡 Astuces
|
| 271 |
-
|
| 272 |
-
- **Seuil de confiance bas (0.3):** Plus de détections, mais plus de faux positifs
|
| 273 |
-
- **Seuil de confiance élevé (0.7):** Moins de détections, mais plus précises
|
| 274 |
-
- **Seuil NMS:** Contrôle le chevauchement des boîtes de détection
|
| 275 |
-
|
| 276 |
-
---
|
| 277 |
-
|
| 278 |
-
Créé avec ❤️ par [nathbns](https://huggingface.co/nathbns)
|
| 279 |
-
"""
|
| 280 |
-
)
|
| 281 |
|
| 282 |
if __name__ == "__main__":
|
| 283 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import cv2
|
|
|
|
| 36 |
class YOLOv3Detector:
|
| 37 |
def __init__(self, checkpoint_path):
|
| 38 |
"""Initialise le détecteur YOLOv3"""
|
|
|
|
| 39 |
|
| 40 |
# Charger le modèle
|
| 41 |
self.model = YOLOv3(num_classes=NUM_CLASSES).to(DEVICE)
|
|
|
|
| 53 |
np.random.seed(42)
|
| 54 |
self.colors = np.random.randint(0, 255, size=(len(PASCAL_CLASSES), 3), dtype=np.uint8)
|
| 55 |
|
|
|
|
| 56 |
|
| 57 |
def preprocess_image(self, image):
|
| 58 |
"""Prétraite l'image pour le modèle"""
|
|
|
|
| 155 |
|
| 156 |
|
| 157 |
# Télécharger le modèle depuis Hugging Face
|
|
|
|
| 158 |
checkpoint_path = hf_hub_download(
|
| 159 |
repo_id="nathbns/yolov3_from_scratch",
|
| 160 |
filename="checkpoint.pth.tar"
|
| 161 |
)
|
|
|
|
| 162 |
|
| 163 |
# Initialiser le détecteur
|
|
|
|
| 164 |
detector = YOLOv3Detector(checkpoint_path)
|
| 165 |
|
| 166 |
|
| 167 |
def predict(image, conf_threshold, iou_threshold):
|
| 168 |
"""Fonction de prédiction pour Gradio"""
|
| 169 |
if image is None:
|
| 170 |
+
return None, "Aucune image fournie"
|
| 171 |
|
| 172 |
# Détecter
|
| 173 |
boxes = detector.detect(image, conf_threshold, iou_threshold)
|
|
|
|
| 177 |
|
| 178 |
# Texte des détections
|
| 179 |
if detections:
|
| 180 |
+
detection_text = f"**{len(detections)} objet(s) détecté(s) :**\n\n" + "\n".join(detections)
|
| 181 |
else:
|
| 182 |
+
detection_text = "Aucun objet détecté"
|
| 183 |
|
| 184 |
return result_image, detection_text
|
| 185 |
|
|
|
|
| 188 |
with gr.Blocks(title="YOLOv3 Object Detection", theme=gr.themes.Soft()) as demo:
|
| 189 |
gr.Markdown(
|
| 190 |
"""
|
| 191 |
+
# YOLOv3 Object Detection - Pascal VOC
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
"""
|
| 193 |
)
|
| 194 |
|
| 195 |
with gr.Row():
|
| 196 |
with gr.Column():
|
| 197 |
+
input_image = gr.Image(type="pil", label="Image d'entrée")
|
| 198 |
|
| 199 |
+
with gr.Accordion("Paramètres", open=True):
|
| 200 |
conf_slider = gr.Slider(
|
| 201 |
minimum=0.1,
|
| 202 |
maximum=1.0,
|
|
|
|
| 214 |
info="Plus élevé = plus de boîtes qui se chevauchent"
|
| 215 |
)
|
| 216 |
|
| 217 |
+
detect_btn = gr.Button("Détecter les objets", variant="primary", size="lg")
|
| 218 |
|
| 219 |
with gr.Column():
|
| 220 |
+
output_image = gr.Image(label="Résultat")
|
| 221 |
+
output_text = gr.Markdown(label="Détections")
|
| 222 |
|
| 223 |
# Action
|
| 224 |
detect_btn.click(
|
|
|
|
| 233 |
inputs=[input_image, conf_slider, iou_slider],
|
| 234 |
outputs=[output_image, output_text]
|
| 235 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
if __name__ == "__main__":
|
| 238 |
demo.launch()
|