Spaces:
Sleeping
Sleeping
Create detec_faces_yolo.py
Browse files- detec_faces_yolo.py +65 -0
detec_faces_yolo.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
|
| 5 |
+
def detect_faces_in_images(images_folder):
|
| 6 |
+
model = YOLO("yolov8n.pt")
|
| 7 |
+
conf_threshold = 0.4
|
| 8 |
+
show_preview = True
|
| 9 |
+
max_persons_allowed = 1
|
| 10 |
+
|
| 11 |
+
total_images = 0
|
| 12 |
+
too_many_people_count = 0
|
| 13 |
+
problematic_images = []
|
| 14 |
+
|
| 15 |
+
print(f"📂 Début de l’analyse des images dans : {images_folder}")
|
| 16 |
+
|
| 17 |
+
for filename in os.listdir(images_folder):
|
| 18 |
+
if not filename.lower().endswith((".jpg", ".png", ".jpeg")):
|
| 19 |
+
continue
|
| 20 |
+
|
| 21 |
+
image_path = os.path.join(images_folder, filename)
|
| 22 |
+
image = cv2.imread(image_path)
|
| 23 |
+
if image is None:
|
| 24 |
+
print(f"❌ Image non lisible : {filename}")
|
| 25 |
+
continue
|
| 26 |
+
|
| 27 |
+
results = model.predict(source=image, conf=conf_threshold, classes=[0], verbose=False)
|
| 28 |
+
boxes = results[0].boxes.xyxy.cpu().numpy()
|
| 29 |
+
person_count = len(boxes)
|
| 30 |
+
total_images += 1
|
| 31 |
+
|
| 32 |
+
for box in boxes:
|
| 33 |
+
x1, y1, x2, y2 = map(int, box)
|
| 34 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 35 |
+
|
| 36 |
+
cv2.putText(image, f"{person_count} personne(s)", (10, 30),
|
| 37 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
|
| 38 |
+
|
| 39 |
+
if person_count > max_persons_allowed:
|
| 40 |
+
problematic_images.append(filename)
|
| 41 |
+
too_many_people_count += 1
|
| 42 |
+
|
| 43 |
+
if show_preview:
|
| 44 |
+
cv2.imshow("Détection YOLOv8", image)
|
| 45 |
+
if cv2.waitKey(0) & 0xFF == ord('q'):
|
| 46 |
+
break
|
| 47 |
+
|
| 48 |
+
cv2.destroyAllWindows()
|
| 49 |
+
|
| 50 |
+
print("\n✅ Analyse terminée.")
|
| 51 |
+
print(f"🖼️ Images analysées : {total_images}")
|
| 52 |
+
print(f"⚠️ Images avec > {max_persons_allowed} personne(s) : {too_many_people_count}")
|
| 53 |
+
|
| 54 |
+
if problematic_images:
|
| 55 |
+
print("📌 Images à problème :")
|
| 56 |
+
for img in problematic_images:
|
| 57 |
+
print(f" - {img}")
|
| 58 |
+
else:
|
| 59 |
+
print("✅ Aucune image problématique.")
|
| 60 |
+
|
| 61 |
+
# Pour tester ce fichier seul
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
test_folder = r"C:\Users\lisig\Desktop\Vision_shield_projet\IA_objets\Lina\Lina_images_test"
|
| 64 |
+
detect_faces_in_images(test_folder)
|
| 65 |
+
|