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)
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()
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