-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheye_detection_p3.py
38 lines (32 loc) · 1.21 KB
/
eye_detection_p3.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
# 定位瞳孔 ,眼球转动
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 | cv2.THRESH_BINARY)
# 轮廓检测
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("o",frame)
cv2.imshow("Threshold", threshold)
cv2.imshow("gray roi", gray_roi)
cv2.imshow("Roi", roi)
key = cv2.waitKey(30)
if key == 27:
break
cv2.destroyAllWindows()