Repository navigation
Expand file tree
/
Copy pathSMP.py
More file actions
69 lines (57 loc) · 2.07 KB
/
Copy pathSMP.py
File metadata and controls
69 lines (57 loc) · 2.07 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
63
64
65
66
67
68
69
import cv2
import mediapipe as mp
import time
mp_face = mp.solutions.face_mesh
face_mesh = mp_face.FaceMesh(
static_image_mode=False,
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.7,
min_tracking_confidence=0.5
)
mp_drawing = mp.solutions.drawing_utils
# индексы для глаз и рта
LEFT_EYE = list(range(33, 133))
RIGHT_EYE = list(range(362, 463))
MOUTH = list(range(78, 308))
cap = cv2.VideoCapture(0)
p_time = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = face_mesh.process(rgb_frame)
if results.multi_face_landmarks:
h, w, _ = frame.shape
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(
frame,
face_landmarks,
mp_face.FACEMESH_TESSELATION,
mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=1, circle_radius=1),
mp_drawing.DrawingSpec(color=(0, 255, 255), thickness=1)
)
# нос
nose_tip = face_landmarks.landmark[1]
cx, cy = int(nose_tip.x * w), int(nose_tip.y * h)
cv2.circle(frame, (cx, cy), 6, (0, 0, 255), -1)
# глаза
for idx in LEFT_EYE + RIGHT_EYE:
lx, ly = int(face_landmarks.landmark[idx].x * w), int(face_landmarks.landmark[idx].y * h)
cv2.circle(frame, (lx, ly), 1, (255, 0, 0), -1)
# рот
for idx in MOUTH:
mx, my = int(face_landmarks.landmark[idx].x * w), int(face_landmarks.landmark[idx].y * h)
cv2.circle(frame, (mx, my), 1, (0, 165, 255), -1)
c_time = time.time()
fps = int(1 / (c_time - p_time)) if p_time else 0
p_time = c_time
cv2.putText(frame, f'FPS: {fps}', (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.imshow("Face Landmarks", frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()