-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_faces_from_video.py
More file actions
54 lines (41 loc) · 1.69 KB
/
extract_faces_from_video.py
File metadata and controls
54 lines (41 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cv2
import os
import time
# --------- CONFIGURABLE SETTINGS ----------
person_id = "4_Umang"
save_dir = f"dataset/{person_id}"
video_source = "http://192.168.1.8:4747/video"
# Default webcam; for DroidCam use: "http://192.168.0.101:4747/video"
face_count_target = 3
# ------------------------------------------
os.makedirs(save_dir, exist_ok=True)
cap = cv2.VideoCapture(video_source)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
face_saved = 0
frame_skip = 0
print(f"[INFO] Starting face capture for: {person_id}")
print("[INFO] Press 'q' to quit early.\n")
while face_saved < face_count_target:
ret, frame = cap.read()
if not ret:
print("[ERROR] Camera not accessible.")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)
for (x, y, w, h) in faces:
if frame_skip % 5 == 0: # Save every 5th detected face (avoid duplicates)
face_img = frame[y:y + h, x:x + w]
filename = os.path.join(save_dir, f"{face_saved + 1}.jpg")
cv2.imwrite(filename, face_img)
face_saved += 1
print(f"[✓] Saved: {filename}")
time.sleep(1) # Pause to give user time to change pose
# Draw rectangle for user feedback
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
frame_skip += 1
cv2.imshow("Live Capture (Press 'q' to exit)", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
print(f"\n✅ Face capture complete. Total saved: {face_saved}")