python - 用眼镜捕捉眼球的运动
问题描述
我正在做一个项目来捕捉虹膜(眼睛)的方向,比如左、右和中心,但是每当我戴着眼镜进行测试时,它开始在检测运动时出现故障,是否可以添加任何代码来减少这个故障。下面是我的代码。
def eye_directions(eye_points, facial_landmarks):
eye_region = np.array([(facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y),
(facial_landmarks.part(eye_points[1]).x, facial_landmarks.part(eye_points[1]).y),
(facial_landmarks.part(eye_points[2]).x, facial_landmarks.part(eye_points[2]).y),
(facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y),
(facial_landmarks.part(eye_points[4]).x, facial_landmarks.part(eye_points[4]).y),
(facial_landmarks.part(eye_points[5]).x, facial_landmarks.part(eye_points[5]).y)], np.int32)
height, width, _ = frame.shape
mask = np.zeros((height, width), np.uint8)
cv2.polylines(mask, [eye_region], True, 255, 2)
cv2.fillPoly(mask, [eye_region], 255)
eye = cv2.bitwise_and(gray, gray, mask = mask)
#slicing the eye into parts
min_x = np.min(eye_region[:, 0])
max_x = np.max(eye_region[:, 0])
min_y = np.min(eye_region[:, 1])
max_y = np.max(eye_region[:, 1])
gray_eye = eye[min_y: max_y, min_x: max_x]
_, threshold_eye = cv2.threshold(gray_eye, 70, 255, cv2.THRESH_BINARY)
#height and width of the eye
height, width = threshold_eye.shape
#eye threshold
left_side_thresh = threshold_eye[0: height, 0: int(width/2)]
left_side_white = cv2.countNonZero(left_side_thresh)
right_side_thresh = threshold_eye[0: height, int(width/2): width]
right_side_white = cv2.countNonZero(right_side_thresh)
if left_side_white == 0:
eye_direction = 1
elif right_side_white == 0:
eye_direction = 5
else:
eye_direction = left_side_white / right_side_white
return eye_direction
解决方案
推荐阅读
- c# - Xamarin - 点击时如何从列表视图中的 ImageCell 获取数据绑定字符串值?
- c# - 向 WCF + Autofac 无扩展服务添加身份验证
- api - Proxy in nginx - forwarding location
- r - Re-format matched items into two columns
- scala - Mina SSHD enable KEX with SHA1 in version 2.6.0
- javascript - 为Highchart的柱形图添加标签
- webpack - Webpack 5 供应商块命名
- installation - Visual Studio 2019 Project creation Issue
- python - Incorrect visualization in Python Plotly
- python - Can anyone define a recursive function to solve this problem?