-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiris_detection.py
54 lines (41 loc) · 2.12 KB
/
iris_detection.py
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 as cv2
import numpy as np
import mediapipe as mp
mp_face_mesh = mp.solutions.face_mesh
LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398]
LEFT_IRIS = [474, 475, 476, 477]
RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246]
RIGHT_IRIS = [469, 470, 471, 472]
camera = cv2.VideoCapture(0)
with mp_face_mesh.FaceMesh(max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5,
min_tracking_confidence=0.5) as face_mesh:
while True:
ret, frame = camera.read()
if not ret:
break
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image_h, image_w = frame.shape[:2]
results = face_mesh.process(rgb_frame)
if results.multi_face_landmarks:
mesh_points = np.array([np.multiply([p.x, p.y], [image_w, image_h]).astype(int) for p in
results.multi_face_landmarks[0].landmark])
# Eyes Detection
cv2.polylines(frame, [mesh_points[LEFT_EYE]], True, (255, 0, 255), 1, cv2.LINE_AA)
cv2.polylines(frame, [mesh_points[RIGHT_EYE]], True, (255, 0, 255), 1, cv2.LINE_AA)
# Iris Detection: Rectangle
cv2.polylines(frame, [mesh_points[LEFT_IRIS]], True, (255, 0, 255), 1, cv2.LINE_AA)
cv2.polylines(frame, [mesh_points[RIGHT_IRIS]], True, (255, 0, 255), 1, cv2.LINE_AA)
# Iris Detection: Circle
(l_cx, l_cy), l_radius = cv2.minEnclosingCircle(mesh_points[LEFT_IRIS])
(r_cx, r_cy), r_radius = cv2.minEnclosingCircle(mesh_points[RIGHT_IRIS])
center_left = np.array([l_cx, l_cy], dtype=np.int32)
center_right = np.array([r_cx, r_cy], dtype=np.int32)
cv2.circle(frame, center_left, int(l_radius), (255, 0, 255), 1, cv2.LINE_AA)
cv2.circle(frame, center_right, int(r_radius), (255, 0, 255), 1, cv2.LINE_AA)
cv2.imshow('image', frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
camera.release()
cv2.destroyAllWindows()