1

I am currently trying to get the ROI of both eyes in a face. However, it is only returning the right eye. How do I get the ROI of both eyes like in a single frame?

for (x,y,w,h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h),(0,255,0), 2)
        faceROI = img[y:y+h,x:x+w]

        smile_rects = smile_cascade.detectMultiScale(faceROI, scaleFactor=1.1, minNeighbors=10, minSize=(15, 15), flags=cv2.CASCADE_SCALE_IMAGE)
        eyes = eye_cascade.detectMultiScale(faceROI)
        
        for (x2, y2, w2, h2) in eyes:
            ptA = (x + x2, y + y2)
            ptB = (x + x2 + w2, y + y2 + h2)
            cv2.rectangle(img, ptA, ptB, (0, 0, 255), 2)
            eyeROI = faceROI[y2:y2+h2,x2:x2+w2]
cv2.imshow('image', img)
cv2.imshow('EyeROI', eyeROI)
cv2.waitKey(0)
AaronTan21
  • 33
  • 5

1 Answers1

2

To crop each eye you need to display the image in the loop, and they need to have a different name or else the last one will always overwrite. See code below on a fictional person.

img = cv2.imread('./not_a_real_person.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h),(0,255,0), 2)
    faceROI = img[y:y+h,x:x+w]

    eyes = eye_cascade.detectMultiScale(faceROI)
    eye_count = 1
    for (x2, y2, w2, h2) in eyes:
        ptA = (x + x2, y + y2)
        ptB = (x + x2 + w2, y + y2 + h2)
        cv2.rectangle(img, ptA, ptB, (0, 0, 255), 2)
        eyeROI = faceROI[y2:y2+h2,x2:x2+w2]
        cv2.imshow('eye %d'%eye_count, eyeROI)
        eye_count+=1

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Example

Without the face detection:

img = cv2.imread('./not_a_real_person_cropped.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
eyes = eye_cascade.detectMultiScale(gray)
eye_count = 1
for (ex,ey,ew,eh) in eyes:
    cv2.rectangle(img,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
    eyeROI = img[ey:ey+eh,ex:ex+ew]
    cv2.imshow('eye_%d'%eye_count, eyeROI)
    eye_count+=1

Example2

Tiphel
  • 323
  • 1
  • 11
  • Ohh thanks @Tiphel ! I am wondering if I can use that without the need of using the face_cascade. Like for example, I have a cropped face image. Does detecting the face is still relevant? However, if I am going to remove the face_cascade, what would be the structure of the code since the eye detection is inside the for loop of the face detect. – AaronTan21 Sep 27 '22 at 09:28
  • Yes you can apply the eye detection straight away, the face detection isn't necessary, it would simply restrict the search area and limit false positives. But if your image is already cropped around the face there is no need for it. In the code remove the face loop and apply the eye to the main image. – Tiphel Sep 27 '22 at 10:07
  • I see, but how are we going to get the x, y, w, and h values if we will be removing the for loop of the face @Tiphel ? – AaronTan21 Sep 27 '22 at 12:56
  • It's pretty straight forward, I added the code in my answer edit. – Tiphel Sep 27 '22 at 13:38