Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,6 +37,53 @@ def check_camera_availability():
|
|
| 37 |
|
| 38 |
return available_cameras
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# --- CLASSE YOLO OPTIMISÉE ---
|
| 41 |
class YOLOVideoProcessor:
|
| 42 |
def __init__(self, model_path, poly1, poly2, tracker_method="bot"):
|
|
@@ -405,6 +452,17 @@ def main():
|
|
| 405 |
|
| 406 |
# Vérifier que les polygones sont valides
|
| 407 |
valid_polygons = len(poly1) == 4 and len(poly2) == 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
|
| 409 |
# Section principale de la détection en temps réel
|
| 410 |
st.header("Détection en Temps Réel avec Webcam")
|
|
|
|
| 37 |
|
| 38 |
return available_cameras
|
| 39 |
|
| 40 |
+
def preview_polygons(poly1, poly2):
|
| 41 |
+
"""Crée une prévisualisation des polygones sur une image vide"""
|
| 42 |
+
# Créer une image noire de taille standard
|
| 43 |
+
preview_img = np.zeros((640, 1200, 3), dtype=np.uint8)
|
| 44 |
+
|
| 45 |
+
# Dessiner le polygone 1 en vert
|
| 46 |
+
if len(poly1) >= 3:
|
| 47 |
+
cv2.polylines(preview_img, [np.array(poly1, np.int32)], isClosed=True, color=(0, 255, 0), thickness=2)
|
| 48 |
+
cv2.fillPoly(preview_img, [np.array(poly1, np.int32)], color=(0, 255, 0, 0.3))
|
| 49 |
+
|
| 50 |
+
# Ajouter des points et des annotations
|
| 51 |
+
for i, point in enumerate(poly1):
|
| 52 |
+
cv2.circle(preview_img, point, 5, (255, 255, 255), -1)
|
| 53 |
+
draw_text_with_background(preview_img, f"P1-{i+1}: {point}",
|
| 54 |
+
(point[0] + 10, point[1]), font_scale=0.5, bg_color=(0, 100, 0))
|
| 55 |
+
|
| 56 |
+
# Dessiner le polygone 2 en rouge
|
| 57 |
+
if len(poly2) >= 3:
|
| 58 |
+
cv2.polylines(preview_img, [np.array(poly2, np.int32)], isClosed=True, color=(0, 0, 255), thickness=2)
|
| 59 |
+
cv2.fillPoly(preview_img, [np.array(poly2, np.int32)], color=(0, 0, 255, 0.3))
|
| 60 |
+
|
| 61 |
+
# Ajouter des points et des annotations
|
| 62 |
+
for i, point in enumerate(poly2):
|
| 63 |
+
cv2.circle(preview_img, point, 5, (255, 255, 255), -1)
|
| 64 |
+
draw_text_with_background(preview_img, f"P2-{i+1}: {point}",
|
| 65 |
+
(point[0] + 10, point[1]), font_scale=0.5, bg_color=(100, 0, 0))
|
| 66 |
+
|
| 67 |
+
# Ajouter une légende
|
| 68 |
+
draw_text_with_background(preview_img, "Zone 1 (Vert)", (10, 30), font_scale=0.7, bg_color=(0, 100, 0))
|
| 69 |
+
draw_text_with_background(preview_img, "Zone 2 (Rouge)", (10, 60), font_scale=0.7, bg_color=(100, 0, 0))
|
| 70 |
+
|
| 71 |
+
# Dessiner une grille pour aider à positionner
|
| 72 |
+
grid_spacing = 100
|
| 73 |
+
grid_color = (50, 50, 50)
|
| 74 |
+
|
| 75 |
+
for x in range(0, preview_img.shape[1], grid_spacing):
|
| 76 |
+
cv2.line(preview_img, (x, 0), (x, preview_img.shape[0]), grid_color, 1)
|
| 77 |
+
# Ajouter le numéro de coordonnée X
|
| 78 |
+
draw_text_with_background(preview_img, str(x), (x, 20), font_scale=0.5, bg_color=(30, 30, 30))
|
| 79 |
+
|
| 80 |
+
for y in range(0, preview_img.shape[0], grid_spacing):
|
| 81 |
+
cv2.line(preview_img, (0, y), (preview_img.shape[1], y), grid_color, 1)
|
| 82 |
+
# Ajouter le numéro de coordonnée Y
|
| 83 |
+
draw_text_with_background(preview_img, str(y), (5, y), font_scale=0.5, bg_color=(30, 30, 30))
|
| 84 |
+
|
| 85 |
+
return preview_img
|
| 86 |
+
|
| 87 |
# --- CLASSE YOLO OPTIMISÉE ---
|
| 88 |
class YOLOVideoProcessor:
|
| 89 |
def __init__(self, model_path, poly1, poly2, tracker_method="bot"):
|
|
|
|
| 452 |
|
| 453 |
# Vérifier que les polygones sont valides
|
| 454 |
valid_polygons = len(poly1) == 4 and len(poly2) == 4
|
| 455 |
+
|
| 456 |
+
# --- PRÉVISUALISATION DES MASQUES ---
|
| 457 |
+
st.header("🖼️ Prévisualisation des masques")
|
| 458 |
+
|
| 459 |
+
if valid_polygons:
|
| 460 |
+
preview_image = preview_polygons(poly1, poly2)
|
| 461 |
+
preview_image_rgb = cv2.cvtColor(preview_image, cv2.COLOR_BGR2RGB)
|
| 462 |
+
st.image(preview_image_rgb, use_column_width=True, caption="Prévisualisation des masques de détection")
|
| 463 |
+
st.success("✅ Les polygones sont correctement définis.")
|
| 464 |
+
else:
|
| 465 |
+
st.warning("⚠️ Veuillez définir des polygones valides avec exactement 4 points chacun.")
|
| 466 |
|
| 467 |
# Section principale de la détection en temps réel
|
| 468 |
st.header("Détection en Temps Réel avec Webcam")
|