Ali Salehi
Ali Salehi

Reputation: 341

capture both eyes in python opencv

I want to detect both eyes from image, but when I use Haar cascade it returns each eye separately. what should I do to crop both eyes together from the image? here is my code:

image = cv2.imread('KA.AN1.39.tiff')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
eye = eye_cascade.detectMultiScale(image)
for (x,y,w,h) in eye:
    eye = image[y : y+h , x : x+w]
    cv2.imshow('eye', eye)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

thanks for your help

Upvotes: 0

Views: 1955

Answers (1)

Chankey Pathak
Chankey Pathak

Reputation: 21676

for x,y,w,h in eye:
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]
    eyes = eyePair_cascade.detectMultiScale(roi_gray)   
    if len(eyes) == 0: return   
    for (ex,ey,ew,eh) in eyes:
        eyes_roi = roi_color[ey: ey+eh, ex:ex + ew]
        #cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

cv2.imwrite(outfile, eyes_roi)

Also see Detection of eye pairs in Python OpenCV (Panoptigram)

Upvotes: 1

Related Questions