-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_data.py
More file actions
62 lines (45 loc) · 1.54 KB
/
Copy pathcollect_data.py
File metadata and controls
62 lines (45 loc) · 1.54 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
55
56
57
58
59
60
61
62
import cv2
import mediapipe as mp
import numpy as np
mp_face_mesh = mp.solutions.face_mesh
mp_drawing = mp.solutions.drawing_utils
mp_styles = mp.solutions.drawing_styles
cap = cv2.VideoCapture(0)
face_mesh = mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
samples = []
print("Move your head naturally. Press Q to stop.")
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
h, w, _ = frame.shape
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = face_mesh.process(rgb)
if results.multi_face_landmarks:
face_landmarks = results.multi_face_landmarks[0]
mp_drawing.draw_landmarks(
image=frame,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=mp_styles.DrawingSpec(
color=(0, 255, 0),
thickness=1
)
)
coords = [[p.x, p.y, p.z] for p in face_landmarks.landmark]
nose = face_landmarks.landmark[1]
samples.append((coords, [nose.x, nose.y]))
cv2.imshow("Collecting Face Mesh Data", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
np.savez("data/samples.npz", data=np.array(samples, dtype=object))
print("Saved data/samples.npz with", len(samples), "frames")