Spaces:
No application file
No application file
Arthur PAILLE commited on
Commit ·
7f7db41
1
Parent(s): 01070a6
ajout
Browse files- analyse_video.py +57 -0
analyse_video.py
CHANGED
|
@@ -27,3 +27,60 @@ def detecter_visage_principal(detections, frame_w, frame_h):
|
|
| 27 |
"height": int(bbox.height * frame_h),
|
| 28 |
"score": round(best.score[0], 3)
|
| 29 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
"height": int(bbox.height * frame_h),
|
| 28 |
"score": round(best.score[0], 3)
|
| 29 |
}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def analyser_video(chemin_video, dossier_sortie="output"):
|
| 33 |
+
os.makedirs(dossier_sortie, exist_ok=True)
|
| 34 |
+
|
| 35 |
+
cap, fps = charger_video(chemin_video)
|
| 36 |
+
frame_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 37 |
+
frame_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 38 |
+
|
| 39 |
+
detector = mp.solutions.face_detection.FaceDetection(min_detection_confidence=0.5)
|
| 40 |
+
|
| 41 |
+
frames_data = []
|
| 42 |
+
frame_idx = 0
|
| 43 |
+
|
| 44 |
+
print("Analyse en cours...")
|
| 45 |
+
while True:
|
| 46 |
+
ret, frame = cap.read()
|
| 47 |
+
if not ret:
|
| 48 |
+
break
|
| 49 |
+
|
| 50 |
+
timestamp = round(frame_idx / fps, 4)
|
| 51 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 52 |
+
results = detector.process(frame_rgb)
|
| 53 |
+
|
| 54 |
+
bbox = detecter_visage_principal(
|
| 55 |
+
results.detections if results.detections else [],
|
| 56 |
+
frame_w, frame_h
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
frames_data.append({
|
| 60 |
+
"frame": frame_idx,
|
| 61 |
+
"timestamp": timestamp,
|
| 62 |
+
"face_detected": bbox is not None,
|
| 63 |
+
"bbox": bbox
|
| 64 |
+
})
|
| 65 |
+
|
| 66 |
+
frame_idx += 1
|
| 67 |
+
if frame_idx % 100 == 0:
|
| 68 |
+
print(f" {frame_idx} frames traitées...")
|
| 69 |
+
|
| 70 |
+
cap.release()
|
| 71 |
+
detector.close()
|
| 72 |
+
|
| 73 |
+
nb_detected = sum(1 for f in frames_data if f["face_detected"])
|
| 74 |
+
print(f"Terminé : {frame_idx} frames, visage détecté sur {nb_detected} ({100 * nb_detected // frame_idx}%)")
|
| 75 |
+
|
| 76 |
+
output_path = os.path.join(dossier_sortie, "detection_visage.json")
|
| 77 |
+
with open(output_path, "w") as f:
|
| 78 |
+
json.dump({"fps": fps, "frames": frames_data}, f, indent=2)
|
| 79 |
+
|
| 80 |
+
print(f"Résultats sauvegardés : {output_path}")
|
| 81 |
+
return frames_data, fps
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
chemin = sys.argv[1] if len(sys.argv) > 1 else "test.mp4"
|
| 86 |
+
analyser_video(chemin)
|