%pylab inline
import matplotlib.pyplot as plt
import cv2
import numpy as np
from mytools import *
left = imreadRGB("left.png");
right = imreadRGB("right.png");
biplot(left,right);
"""
Port of haar-c/eyeDetection.cpp
Using OpenCV-Python based tutorial as reference
https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html
"""
eye_cascade = cv2.CascadeClassifier('haar-c/haarcascade_eye_tree_eyeglasses.xml')
def findEye(imrgb):
imgray = cv2.cvtColor(imrgb, cv2.COLOR_RGB2GRAY)
cv2.equalizeHist( imgray, imgray );
eyes = eye_cascade.detectMultiScale(imgray)
if (len(eyes) == 0):
print 'No eye detected'
return None;
else:
return eyes[0] # return first
rectL = findEye(left)
rectR = findEye(right)
print rectL, rectR
(x,y,w,h) = rectL
eyeL = left[y:y+h, x:x+h]
(x,y,w,h) = rectR
eyeR = right[y:y+h, x:x+h]
biplot(eyeL, eyeR)
img_names = ("P_Sujeto1_1_4.png", "left.png", "right.png", "left_eye.png", "right_eye.png", "P_Sujeto3_1_1.png", "P_Sujeto3_1_4.png")
for im_name in img_names:
im = imreadRGB(im_name)
rect = findEye(im)
if (rect is None):
print "Fail detection for", im_name
continue
(x,y,w,h) = rect
cv2.rectangle(im, (x,y), (x+w, y+h), (0,0,255), 1)
plt.imshow(im), plt.title(im_name), plt.show()