-
Notifications
You must be signed in to change notification settings - Fork 36
/
pupil_detect.py
65 lines (56 loc) · 2.53 KB
/
pupil_detect.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
55
56
57
58
59
60
61
62
63
64
65
#All the stuff here is for finding pupils in eyes
import cv2
import numpy
np = numpy
#Mask an image so all pixels outside feature circle are black
def mask_image_by_feature(image, feature):
circle_mask_image = np.zeros(image.shape, dtype=np.uint8)
cv2.circle(circle_mask_image, (int(feature.pt[0]), int(feature.pt[1])), int(feature.size/2), 1, -1)
masked_image = (image * circle_mask_image).astype(np.uint8)
return masked_image
#Find average brightness of pixels under a feature's circle
def find_average_brightness_of_feature(image, feature):
feature_image = mask_image_by_feature(image, feature)
total_value = feature_image.sum()
area = np.pi * ((feature.size/2)**2)
return total_value/area
#Sums up all the pixels under a feature's circle and averages them
#Darkest first
def sort_features_by_brightness(image, features):
features_and_brightnesses = [(find_average_brightness_of_feature(image, feature), feature) for feature in features]
features_and_brightnesses.sort(key = lambda x:x[0])
return [fb[1] for fb in features_and_brightnesses]
def draw_circle_for_feature(image, feature, color=255, thickness=1):
cv2.circle(image, (int(feature.pt[0]), int(feature.pt[1])), int(feature.size/2), color, thickness)
def find_pupil(gray_image, minsize=.1, maxsize=.5):
detector = cv2.FeatureDetector_create('MSER')
features_all = detector.detect(gray_image)
features_big = [feature for feature in features_all if feature.size > gray_image.shape[0]*minsize]
features_small = [feature for feature in features_big if feature.size < gray_image.shape[0]*maxsize]
if len(features_small) == 0:
return None
features_sorted = sort_features_by_brightness(gray_image, features_small)
pupil = features_sorted[0]
return (int(pupil.pt[0]), int(pupil.pt[1]), int(pupil.size/2))
def circle_pupil(color_image, output_image = None):
if output_image is None:
output_image = color_image
gray_image = cv2.cvtColor(color_image, cv2.COLOR_RGB2GRAY)
pupil_coords = find_pupil(gray_image)
if pupil_coords is not None:
cv2.circle(output_image, pupil_coords[:2], pupil_coords[2], (0,255,0),4)
def draw(photo):
image_to_show = photo.copy()
circle_pupil(image_to_show)
cv2.imshow('Image', image_to_show)
if cv2.waitKey(10) > 0: #If we got a key press in less than 10ms
return 1
return 0
def main():
camera = cv2.VideoCapture(0)
while(1):
success, photo = camera.read()
if draw(photo) > 0:
break
if __name__ == '__main__':
main()