Spaces:
Sleeping
Sleeping
Download modeles/detect_notAllowedObjects.py from VisionShield/VISIONSHIELD: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/spaces/VisionShield/VISIONSHIELD/resolve/main/modeles/detect_notAllowedObjects.py
- Command line
-
hf download hf://spaces/VisionShield/VISIONSHIELD/modeles/detect_notAllowedObjects.py
-
curl -L -o detect_notAllowedObjects.py https://huggingface.co/spaces/VisionShield/VISIONSHIELD/resolve/main/modeles/detect_notAllowedObjects.py
6.65 kB
| import os | |
| import cv2 | |
| from ultralytics import YOLO | |
| #Ici, nous devons détecter si un/plusieurs objets suspect met en danger le document envoyé par l'utilisateur à son contact | |
| def detect_objects_in_folder(folder_path): | |
| model = YOLO("/app/models/yolov8n.pt") | |
| conf_threshold = 0.4 #on configure notre seuil de confiance à 0.4 donc si le modèle est sur à 40% que l'objet est tel objet alors la detection est validée | |
| # Objets interdits selon les classes COCO (Common Objects in Context) | |
| # et interdit selon VISION SHIELD car ils menancent le doc | |
| forbidden_classes = { | |
| 62: "écran", | |
| 67: "téléphone", | |
| 69: "appareil photo", | |
| 73: "ordinateur" | |
| } | |
| if not os.path.isdir(folder_path): #oni retourne une erreur si on ne trouve pas le dossier | |
| print(f" Dossier introuvable : {folder_path}") | |
| return False | |
| image_files = [f for f in os.listdir(folder_path) if f.lower().endswith((".jpg", ".jpeg", ".png"))] | |
| #on récupère toutes les images ayant la bonne extensions donc pas de vidéos | |
| if not image_files: | |
| print("Aucune image trouvée.") #erreur si aucune image trouvée | |
| return False | |
| for image_name in image_files: # on boucles sur notre dossier | |
| image_path = os.path.join(folder_path, image_name) | |
| frame = cv2.imread(image_path) # on lit notre image | |
| if frame is None: #erreur si on ne parvient pas à lire l'image | |
| print(f" Impossible de lire : {image_name}") | |
| continue | |
| #on lance la détection en appelant notre modèle YOLO | |
| results = model.predict(source=frame, conf=conf_threshold, verbose=False) | |
| #on récupère tous les objets détecter dans notre image | |
| detections = results[0].boxes | |
| for box in detections: # on boucle sur tous les objets trouvés | |
| # on récupère la classe prédite de tous les objets détectés | |
| cls_id = int(box.cls.cpu().item()) | |
| #si l'une des classes des objets détectés fait partie du dictionnaire des objets interdits alors | |
| if cls_id in forbidden_classes: | |
| label = forbidden_classes[cls_id] #on récupère la classe | |
| print(f"Objet interdit détecté : {label} dans {image_name}") | |
| return False # dès qu'on détecte un objet interdit, on arrete la fontion pour prevenir l'app de la transgression de l'utilisateur | |
| print("Aucun objet interdit détecté dans le dossier.") | |
| return True #si tout va bien | |
| #def detect_objects_in_folder(video_folder): | |
| #model = YOLO("yolov8n.pt") | |
| #conf_threshold = 0.4 | |
| # Liste des classes interdites (COCO) → ici : téléphone seulement (classe 67) | |
| #forbidden_classes = { | |
| # 67: "téléphone" | |
| #} | |
| #if not os.path.isdir(video_folder): | |
| # print(f"❌ Dossier introuvable : {video_folder}") | |
| # return | |
| #video_files = [f for f in os.listdir(video_folder) if f.lower().endswith(".mov")] | |
| #if not video_files: | |
| # print("⚠️ Aucune vidéo .mov trouvée.") | |
| # return | |
| #for video_name in video_files: | |
| #video_path = os.path.join(video_folder, video_name) | |
| #cap = cv2.VideoCapture(video_path) | |
| #if not cap.isOpened(): | |
| # print(f"❌ Impossible d’ouvrir : {video_name}") | |
| # continue | |
| #print(f"📹 Analyse objets interdits : {video_name}") | |
| #while True: | |
| #ret, frame = cap.read() | |
| #if not ret: | |
| # break | |
| #results = model.predict(source=frame, conf=conf_threshold, verbose=False) | |
| #detections = results[0].boxes | |
| #for box in detections: | |
| #cls_id = int(box.cls.cpu().item()) | |
| #if cls_id in forbidden_classes: | |
| # x1, y1, x2, y2 = map(int, box.xyxy[0]) | |
| # label = forbidden_classes[cls_id] | |
| # cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2) | |
| # cv2.putText(frame, f"{label}", (x1, y1 - 10), | |
| # cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2) | |
| # print(f"⚠️ Objet interdit détecté : {label} dans {video_name}") | |
| # cv2.imshow(f"{video_name} - Objets interdits", frame) | |
| # if cv2.waitKey(1) & 0xFF == ord('q'): | |
| # break | |
| #cap.release() | |
| # cv2.destroyAllWindows() | |
| # print(f"✅ Fin de l’analyse de {video_name}\n") | |
| #print("✅ Toutes les vidéos analysées pour objets interdits.") | |
| """def detect_objects_in_folder(video_folder): | |
| model = YOLO("yolov8n.pt") | |
| conf_threshold = 0.4 | |
| # ✅ Objets interdits avec classes COCO | |
| forbidden_classes = { | |
| 62: "écran", | |
| 67: "téléphone", | |
| 69: "appareil photo", | |
| 73: "ordinateur" | |
| } | |
| if not os.path.isdir(video_folder): | |
| print(f"❌ Dossier introuvable : {video_folder}") | |
| return | |
| video_files = [f for f in os.listdir(video_folder) if f.lower().endswith(".mov")] | |
| if not video_files: | |
| print("⚠️ Aucune vidéo .mov trouvée.") | |
| return | |
| os.makedirs("outputs_forbidden_objects", exist_ok=True) | |
| for video_name in video_files: | |
| video_path = os.path.join(video_folder, video_name) | |
| cap = cv2.VideoCapture(video_path) | |
| if not cap.isOpened(): | |
| print(f"❌ Impossible d’ouvrir : {video_name}") | |
| continue | |
| print(f"📹 Analyse objets interdits : {video_name}") | |
| frame_count = 0 | |
| while True: | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| results = model.predict(source=frame, conf=conf_threshold, verbose=False) | |
| detections = results[0].boxes | |
| for box in detections: | |
| cls_id = int(box.cls.cpu().item()) | |
| if cls_id in forbidden_classes: | |
| x1, y1, x2, y2 = map(int, box.xyxy[0]) | |
| label = forbidden_classes[cls_id] | |
| cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2) | |
| cv2.putText(frame, f"{label}", (x1, y1 - 10), | |
| cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2) | |
| print(f"⚠️ Objet interdit détecté : {label} dans {video_name}") | |
| output_path = os.path.join("outputs_forbidden_objects", f"{video_name}_frame_{frame_count}.jpg") | |
| cv2.imwrite(output_path, frame) | |
| frame_count += 1 | |
| cap.release() | |
| print(f"✅ Fin de l’analyse de {video_name}\n") | |
| print("✅ Toutes les vidéos analysées pour objets interdits.")""" | |