-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathirisDistance.py
More file actions
47 lines (36 loc) · 2.21 KB
/
irisDistance.py
File metadata and controls
47 lines (36 loc) · 2.21 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
import cv2 as cv
import numpy as np
import mediapipe as mp
mp_face_mesh = mp.solutions.face_mesh
LEFT_IRIS = [474,475, 476, 477]
RIGHT_IRIS = [469, 470, 471, 472]
input = cv.VideoCapture(0) #get video from
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:
success, frame = input.read()
if not success:
print("No frame :(")
break
frame = cv.flip(frame, 1) #get the frame convert into nice colours
rgb_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
img_h, img_w = frame.shape[:2]
results = face_mesh.process(rgb_frame) # google ai stuff
if results.multi_face_landmarks:
mesh_points=np.array([np.multiply([p.x, p.y], [img_w, img_h]).astype(int) for p in results.multi_face_landmarks[0].landmark]) #get the general landmarks
cv.polylines(frame, [mesh_points[LEFT_IRIS]], True, (0,255,0), 1, cv.LINE_AA) #draw diamonds round iris points
cv.polylines(frame, [mesh_points[RIGHT_IRIS]], True, (0,255,0), 1, cv.LINE_AA)
(l_cx, l_cy), l_radius = cv.minEnclosingCircle(mesh_points[LEFT_IRIS]) #get a circle encompassing the eyes
(r_cx, r_cy), r_radius = cv.minEnclosingCircle(mesh_points[RIGHT_IRIS])
center_left = np.array([l_cx, l_cy], dtype=np.int32) # find the centre of the circle
center_right = np.array([r_cx, r_cy], dtype=np.int32)
cv.circle(frame, center_left, int(l_radius), (255,0,255), 1, cv.LINE_AA) # draw circle
cv.circle(frame, center_right, int(r_radius), (255,0,255), 1, cv.LINE_AA)
estFocal = 458.814 #needs to be updated for drone (https://pyimagesearch.com/2015/01/19/find-distance-camera-objectmarker-using-python-opencv/)
estDistance = ((0.0177*estFocal)/(l_radius * 2) + (0.0177*estFocal)/(r_radius * 2))/2 #get average estimated distance of iris
cv.putText(frame, str(estDistance) + "m", (10,450), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, 1)
cv.imshow('iris distance', frame)
key = cv.waitKey(1)
if key ==ord('q'):
break
input.release()
cv.destroyAllWindows()