Python Forum

Full Version: How to crop eyes/nose/mouth in the face only? [OpenCV]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When I crop only face it does the job, but when I want to crop eyes, nose, mouth that are detected in the face it doesn't.

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
    roi_gray = gray[y:y+w, x:x+w]
    roi_color = frame[y:y+h, x:x+w]
    eyes = left_eye_cascade.detectMultiScale(roi_gray, 1.3, 22)
    for (ex, ey, ew, eh) in eyes:
        cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 5)

crop_img = frame[ey:ey+eh, ex:ex+ew]
cv2.imshow("cropped", crop_img)
What am I doing wrong? Sorry for if this sound dumb. I'm just new to Python and OpenCV and I am just curious in how this works. Any help/explanation is welcome. Thank you