python - 实时跟踪眼睛运动
问题描述
大家好,我用python编写了一个检测眼睛运动的代码,但它只适用于视频文件。
我应该改变什么让它通过网络摄像头实时检测运动眼睛?
有代码:
import cv2
import numpy as np
cap = cv2.VideoCapture("eye_recording.flv")
while True:
ret, frame = cap.read()
if ret is False:
break
roi = frame[269: 795, 537: 1416]
rows, cols, _ = roi.shape
gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray_roi = cv2.GaussianBlur(gray_roi, (7, 7), 0)
_, threshold = cv2.threshold(gray_roi, 3, 255, cv2.THRESH_BINARY_INV)
_, contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
#cv2.drawContours(roi, [cnt], -1, (0, 0, 255), 3)
cv2.rectangle(roi, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.line(roi, (x + int(w/2), 0), (x + int(w/2), rows), (0, 255, 0), 2)
cv2.line(roi, (0, y + int(h/2)), (cols, y + int(h/2)), (0, 255, 0), 2)
break
cv2.imshow("Threshold", threshold)
cv2.imshow("gray roi", gray_roi)
cv2.imshow("Roi", roi)
key = cv2.waitKey(30)
if key == 27:
break
cv2.destroyAllWindows()
解决方案
改变
cap = cv2.VideoCapture(0)
而已
推荐阅读
- python - 为什么 LDAP_MATCHING_RULE_IN_CHAIN 或 1.2.840.113556.1.4.1941 为 AD 用户的递归组提供空白结果?
- sql - 甲骨文。从选择的广告中获取最大值然后进行比较
- mongodb - 使用聚合在嵌套数组中选择文档的字段
- android - 如何使整个活动不响应触摸?
- python - 从python中的n个点找到表示折线所需的最小点
- unity3d - 如何在脚本中访问统一着色器属性?
- kubernetes - 在 Kubernetes 中,如何在服务选择器中按名称选择 pod?
- android - 如何解析与 JSON 解析的字符串和对象相同的键名?
- react-native - 在 setState React Native 中更新两个对象
- reactjs - 当对象状态改变时反应上下文和重新渲染组件