Prototipado para la practica Eyes Care

© 2015, Victor Arribas, CC-BY-SA

1. Detección del ojo

In [54]:
%pylab inline
import matplotlib.pyplot as plt
import cv2
import numpy as np
Populating the interactive namespace from numpy and matplotlib

Baseline

In [55]:
from mytools import *
In [56]:
left = imreadRGB("left.png");
right = imreadRGB("right.png");


biplot(left,right);
In [57]:
"""
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
In [58]:
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)
[ 9 18 94 94] [52 16 98 98]

Generalización

In [59]:
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()
No eye detected
Fail detection for left_eye.png
No eye detected
Fail detection for right_eye.png