Spaces:
Running
on
Zero
Running
on
Zero
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,272 +1,217 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
|
| 8 |
-
# Modèles
|
| 9 |
-
|
|
|
|
| 10 |
"DETR ResNet-50": "facebook/detr-resnet-50",
|
| 11 |
-
"DETR ResNet-101": "facebook/detr-resnet-101",
|
| 12 |
-
"Conditional DETR": "microsoft/conditional-detr-resnet-50",
|
| 13 |
-
"Table Transformer": "microsoft/table-transformer-detection",
|
| 14 |
-
"YOLOS Tiny": "hustvl/yolos-tiny",
|
| 15 |
"YOLOS Small": "hustvl/yolos-small",
|
| 16 |
-
"
|
| 17 |
-
"RT-DETR": "PekingU/rtdetr_r50vd_coco_o365",
|
| 18 |
-
"OWL-ViT": "google/owlvit-base-patch32"
|
| 19 |
}
|
| 20 |
|
| 21 |
-
# Cache pour
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
"""Charge
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
model=model_name,
|
| 41 |
-
device=0 if torch.cuda.is_available() else -1
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
return model_cache[model_name]
|
| 45 |
|
| 46 |
@spaces.GPU
|
| 47 |
-
def
|
| 48 |
-
"""
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
try:
|
| 54 |
-
# Charger le
|
| 55 |
-
|
| 56 |
-
detector = load_model(model_id)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
if
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
class_list = [cls.strip() for cls in custom_classes.split(",")]
|
| 64 |
-
results = detector(image, candidate_labels=class_list)
|
| 65 |
else:
|
| 66 |
-
|
| 67 |
-
results = detector(image)
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
]
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
def
|
| 87 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 88 |
draw = ImageDraw.Draw(image)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
try:
|
| 92 |
-
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 16)
|
| 93 |
-
except:
|
| 94 |
font = ImageFont.load_default()
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
colors = [
|
| 97 |
-
"#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4", "#FECA57",
|
| 98 |
-
"#FF9FF3", "#54A0FF", "#5F27CD", "#00D2D3", "#FF9F43"
|
| 99 |
-
]
|
| 100 |
|
| 101 |
for i, detection in enumerate(detections):
|
| 102 |
box = detection['box']
|
| 103 |
label = detection['label']
|
| 104 |
score = detection['score']
|
| 105 |
|
| 106 |
-
# Coordonnées
|
| 107 |
x1, y1 = box['xmin'], box['ymin']
|
| 108 |
x2, y2 = box['xmax'], box['ymax']
|
| 109 |
|
| 110 |
-
# Couleur
|
| 111 |
color = colors[i % len(colors)]
|
| 112 |
|
| 113 |
-
#
|
| 114 |
-
draw.rectangle([x1, y1, x2, y2], outline=color, width=
|
| 115 |
-
|
| 116 |
-
# Texte du label
|
| 117 |
-
text = f"{label} ({score:.2f})"
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
draw.rectangle(bbox, fill=color)
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
return image
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
if not detections:
|
| 131 |
-
return "🔍 Aucun objet détecté"
|
| 132 |
-
|
| 133 |
-
summary = f"🎯 **{len(detections)} objets détectés** avec {model_name}\n\n"
|
| 134 |
-
|
| 135 |
-
# Grouper par classe
|
| 136 |
-
class_counts = {}
|
| 137 |
-
for det in detections:
|
| 138 |
-
label = det['label']
|
| 139 |
-
score = det['score']
|
| 140 |
-
|
| 141 |
-
if label not in class_counts:
|
| 142 |
-
class_counts[label] = []
|
| 143 |
-
class_counts[label].append(score)
|
| 144 |
-
|
| 145 |
-
# Afficher le résumé
|
| 146 |
-
for label, scores in class_counts.items():
|
| 147 |
-
count = len(scores)
|
| 148 |
-
avg_score = sum(scores) / len(scores)
|
| 149 |
-
max_score = max(scores)
|
| 150 |
-
|
| 151 |
-
summary += f"**{label}**: {count}x (confiance: {avg_score:.2f} avg, {max_score:.2f} max)\n"
|
| 152 |
-
|
| 153 |
-
return summary
|
| 154 |
-
|
| 155 |
-
# Interface Gradio
|
| 156 |
-
with gr.Blocks(title="🤖 Object Detection avec Transformers", theme=gr.themes.Soft()) as demo:
|
| 157 |
|
| 158 |
gr.Markdown("""
|
| 159 |
-
#
|
| 160 |
|
| 161 |
-
|
| 162 |
|
| 163 |
-
|
| 164 |
-
- 🔄 Changement de modèle en temps réel
|
| 165 |
-
- 🎯 Seuil de confiance ajustable
|
| 166 |
-
- 🏷️ Classes personnalisées (OWL-ViT)
|
| 167 |
-
- 📊 Résumé détaillé des détections
|
| 168 |
""")
|
| 169 |
|
| 170 |
with gr.Row():
|
| 171 |
-
with gr.Column(scale=
|
| 172 |
-
#
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
label="🎯 Seuil de confiance minimum"
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
# Classes personnalisées pour OWL-ViT
|
| 197 |
-
custom_classes_input = gr.Textbox(
|
| 198 |
-
label="🏷️ Classes personnalisées (pour OWL-ViT)",
|
| 199 |
-
placeholder="person, car, dog, bottle, phone",
|
| 200 |
-
info="Séparées par des virgules. Uniquement pour OWL-ViT."
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
# Bouton de détection
|
| 204 |
-
detect_btn = gr.Button(
|
| 205 |
-
"🔍 Détecter les objets",
|
| 206 |
-
variant="primary",
|
| 207 |
-
size="lg"
|
| 208 |
)
|
| 209 |
|
| 210 |
with gr.Column(scale=1):
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
label="📊 Résultats de détection",
|
| 214 |
-
height=400
|
| 215 |
-
)
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
### **YOLOS (You Only Look Once Transformer)**
|
| 247 |
-
- **Tiny**: Ultra-rapide ⚡
|
| 248 |
-
- **Small**: Bon compromis 🎯
|
| 249 |
-
- **Base**: Maximum de précision 🔍
|
| 250 |
-
|
| 251 |
-
### **OWL-ViT (Zero-shot Detection)**
|
| 252 |
-
- Détecte **n'importe quoi** que vous décrivez ! 🎨
|
| 253 |
-
- Tapez vos propres classes dans le champ "Classes personnalisées"
|
| 254 |
-
|
| 255 |
-
### **RT-DETR**
|
| 256 |
-
- Optimisé pour le temps réel ⚡
|
| 257 |
-
|
| 258 |
-
### **Table Transformer**
|
| 259 |
-
- Spécialisé dans la détection de tableaux 📊
|
| 260 |
-
""")
|
| 261 |
-
|
| 262 |
-
# Exemples
|
| 263 |
-
gr.Examples(
|
| 264 |
-
examples=[
|
| 265 |
-
["example1.jpg", "DETR ResNet-50", 0.5, ""],
|
| 266 |
-
["example2.jpg", "OWL-ViT", 0.3, "smartphone, laptop, coffee cup"],
|
| 267 |
],
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
| 269 |
)
|
| 270 |
|
| 271 |
if __name__ == "__main__":
|
| 272 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
import numpy as np
|
| 7 |
+
import cv2
|
| 8 |
|
| 9 |
+
# Modèles optimisés pour le temps réel
|
| 10 |
+
REALTIME_MODELS = {
|
| 11 |
+
"YOLOS Tiny (ultra-rapide)": "hustvl/yolos-tiny",
|
| 12 |
"DETR ResNet-50": "facebook/detr-resnet-50",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"YOLOS Small": "hustvl/yolos-small",
|
| 14 |
+
"Conditional DETR": "microsoft/conditional-detr-resnet-50"
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
|
| 17 |
+
# Cache global pour le modèle
|
| 18 |
+
current_detector = None
|
| 19 |
+
current_model_name = None
|
| 20 |
|
| 21 |
+
def load_detector(model_name):
|
| 22 |
+
"""Charge le détecteur avec cache"""
|
| 23 |
+
global current_detector, current_model_name
|
| 24 |
+
|
| 25 |
+
if current_model_name != model_name:
|
| 26 |
+
print(f"🔄 Chargement du modèle: {model_name}")
|
| 27 |
+
model_id = REALTIME_MODELS[model_name]
|
| 28 |
+
current_detector = pipeline(
|
| 29 |
+
"object-detection",
|
| 30 |
+
model=model_id,
|
| 31 |
+
device=0 if torch.cuda.is_available() else -1
|
| 32 |
+
)
|
| 33 |
+
current_model_name = model_name
|
| 34 |
+
print(f"✅ Modèle chargé: {model_name}")
|
| 35 |
+
|
| 36 |
+
return current_detector
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
@spaces.GPU
|
| 39 |
+
def process_webcam_frame(frame, model_choice, confidence_threshold):
|
| 40 |
+
"""
|
| 41 |
+
Traite chaque frame de la webcam en temps réel
|
| 42 |
+
Cette fonction est appelée automatiquement pour chaque frame
|
| 43 |
+
"""
|
| 44 |
+
if frame is None:
|
| 45 |
+
return frame
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
# Charger le détecteur
|
| 49 |
+
detector = load_detector(model_choice)
|
|
|
|
| 50 |
|
| 51 |
+
# Convertir numpy array en PIL Image si nécessaire
|
| 52 |
+
if isinstance(frame, np.ndarray):
|
| 53 |
+
# Gradio webcam donne du RGB
|
| 54 |
+
pil_image = Image.fromarray(frame)
|
|
|
|
|
|
|
|
|
|
| 55 |
else:
|
| 56 |
+
pil_image = frame
|
|
|
|
| 57 |
|
| 58 |
+
# Redimensionner pour accélérer le traitement
|
| 59 |
+
original_size = pil_image.size
|
| 60 |
+
max_size = 640 # Réduire la taille pour plus de vitesse
|
| 61 |
+
|
| 62 |
+
if max(original_size) > max_size:
|
| 63 |
+
ratio = max_size / max(original_size)
|
| 64 |
+
new_size = (int(original_size[0] * ratio), int(original_size[1] * ratio))
|
| 65 |
+
resized_image = pil_image.resize(new_size)
|
| 66 |
+
else:
|
| 67 |
+
resized_image = pil_image
|
| 68 |
+
ratio = 1.0
|
| 69 |
+
|
| 70 |
+
# Détection sur l'image redimensionnée
|
| 71 |
+
detections = detector(resized_image)
|
| 72 |
+
|
| 73 |
+
# Filtrer par confiance
|
| 74 |
+
filtered_detections = [
|
| 75 |
+
det for det in detections
|
| 76 |
+
if det['score'] >= confidence_threshold
|
| 77 |
]
|
| 78 |
|
| 79 |
+
# Redimensionner les coordonnées vers la taille originale
|
| 80 |
+
for det in filtered_detections:
|
| 81 |
+
if ratio != 1.0:
|
| 82 |
+
det['box']['xmin'] = int(det['box']['xmin'] / ratio)
|
| 83 |
+
det['box']['ymin'] = int(det['box']['ymin'] / ratio)
|
| 84 |
+
det['box']['xmax'] = int(det['box']['xmax'] / ratio)
|
| 85 |
+
det['box']['ymax'] = int(det['box']['ymax'] / ratio)
|
| 86 |
|
| 87 |
+
# Dessiner les détections sur l'image originale
|
| 88 |
+
annotated_image = draw_detections_fast(pil_image, filtered_detections)
|
| 89 |
|
| 90 |
+
# Convertir back en numpy pour Gradio
|
| 91 |
+
return np.array(annotated_image)
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
+
print(f"❌ Erreur de traitement: {e}")
|
| 95 |
+
return frame
|
| 96 |
|
| 97 |
+
def draw_detections_fast(image, detections):
|
| 98 |
+
"""Version optimisée pour dessiner les détections"""
|
| 99 |
+
if not detections:
|
| 100 |
+
return image
|
| 101 |
+
|
| 102 |
draw = ImageDraw.Draw(image)
|
| 103 |
|
| 104 |
+
# Police par défaut pour la vitesse
|
| 105 |
try:
|
|
|
|
|
|
|
| 106 |
font = ImageFont.load_default()
|
| 107 |
+
except:
|
| 108 |
+
font = None
|
| 109 |
|
| 110 |
+
colors = ["#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4", "#FECA57"]
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
for i, detection in enumerate(detections):
|
| 113 |
box = detection['box']
|
| 114 |
label = detection['label']
|
| 115 |
score = detection['score']
|
| 116 |
|
| 117 |
+
# Coordonnées
|
| 118 |
x1, y1 = box['xmin'], box['ymin']
|
| 119 |
x2, y2 = box['xmax'], box['ymax']
|
| 120 |
|
| 121 |
+
# Couleur
|
| 122 |
color = colors[i % len(colors)]
|
| 123 |
|
| 124 |
+
# Boîte
|
| 125 |
+
draw.rectangle([x1, y1, x2, y2], outline=color, width=2)
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# Label avec score
|
| 128 |
+
text = f"{label} {score:.2f}"
|
|
|
|
| 129 |
|
| 130 |
+
# Fond du texte (simplifié)
|
| 131 |
+
if font:
|
| 132 |
+
bbox = draw.textbbox((x1, y1-20), text, font=font)
|
| 133 |
+
draw.rectangle(bbox, fill=color)
|
| 134 |
+
draw.text((x1, y1-20), text, fill="white", font=font)
|
| 135 |
+
else:
|
| 136 |
+
draw.text((x1, y1-15), text, fill=color)
|
| 137 |
|
| 138 |
return image
|
| 139 |
|
| 140 |
+
# Interface Gradio avec streaming
|
| 141 |
+
with gr.Blocks(title="🎥 Détection Live", theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
gr.Markdown("""
|
| 144 |
+
# 🎥 Détection d'Objets en Temps Réel
|
| 145 |
|
| 146 |
+
**Activez votre webcam** et voyez la détection se faire en direct !
|
| 147 |
|
| 148 |
+
⚡ **Optimisé pour la vitesse** avec des modèles légers
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
""")
|
| 150 |
|
| 151 |
with gr.Row():
|
| 152 |
+
with gr.Column(scale=2):
|
| 153 |
+
# Composant webcam avec streaming
|
| 154 |
+
webcam = gr.Interface(
|
| 155 |
+
fn=process_webcam_frame,
|
| 156 |
+
inputs=[
|
| 157 |
+
gr.Image(sources=["webcam"], streaming=True, type="numpy"),
|
| 158 |
+
gr.Dropdown(
|
| 159 |
+
choices=list(REALTIME_MODELS.keys()),
|
| 160 |
+
value="YOLOS Tiny (ultra-rapide)",
|
| 161 |
+
label="🤖 Modèle (changement en direct)"
|
| 162 |
+
),
|
| 163 |
+
gr.Slider(
|
| 164 |
+
minimum=0.1,
|
| 165 |
+
maximum=1.0,
|
| 166 |
+
value=0.5,
|
| 167 |
+
step=0.1,
|
| 168 |
+
label="🎯 Seuil de confiance"
|
| 169 |
+
)
|
| 170 |
+
],
|
| 171 |
+
outputs=gr.Image(type="numpy", streaming=True),
|
| 172 |
+
live=True, # ⭐ CRUCIAL: Active le mode live
|
| 173 |
+
title=None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
)
|
| 175 |
|
| 176 |
with gr.Column(scale=1):
|
| 177 |
+
gr.Markdown("""
|
| 178 |
+
## 📊 Informations Live
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
### 🎛️ Contrôles en temps réel:
|
| 181 |
+
- **Modèle**: Change instantanément
|
| 182 |
+
- **Confiance**: Ajuste le filtrage
|
| 183 |
+
- **Streaming**: Traitement frame par frame
|
| 184 |
+
|
| 185 |
+
### ⚡ Optimisations:
|
| 186 |
+
- Images redimensionnées à 640px
|
| 187 |
+
- Modèles légers prioritaires
|
| 188 |
+
- Cache intelligent des modèles
|
| 189 |
+
- Dessin optimisé
|
| 190 |
+
|
| 191 |
+
### 🎯 Modèles recommandés:
|
| 192 |
+
- **YOLOS Tiny**: Maximum de vitesse
|
| 193 |
+
- **DETR ResNet-50**: Bon équilibre
|
| 194 |
+
""")
|
| 195 |
+
|
| 196 |
+
# Version alternative avec Interface simple
|
| 197 |
+
gr.Markdown("---")
|
| 198 |
+
gr.Markdown("## 🎥 Version Alternative (Interface Simple)")
|
| 199 |
+
|
| 200 |
+
alternative_interface = gr.Interface(
|
| 201 |
+
fn=process_webcam_frame,
|
| 202 |
+
inputs=[
|
| 203 |
+
gr.Image(sources=["webcam"], streaming=True),
|
| 204 |
+
gr.Dropdown(
|
| 205 |
+
choices=list(REALTIME_MODELS.keys()),
|
| 206 |
+
value="YOLOS Tiny (ultra-rapide)"
|
| 207 |
+
),
|
| 208 |
+
gr.Slider(0.1, 1.0, 0.5, step=0.1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
],
|
| 210 |
+
outputs=gr.Image(streaming=True),
|
| 211 |
+
live=True, # ⭐ Mode live activé
|
| 212 |
+
title="Détection Webcam Live",
|
| 213 |
+
description="Cliquez sur la webcam pour démarrer le streaming live!"
|
| 214 |
)
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
+
demo.launch()
|