I know how to identify a face and the eyes, with opencv and haarcascade. But how can I crop or get only the eyes from webcam capture? Just one, or even the both, so I can use as a new frame in imshow...
Asked
Active
Viewed 249 times
3 Answers
0
See on this post:
How to crop an image in OpenCV using Python
import cv2
img = cv2.imread("lenna.png")
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
Your "img[y:y+h, x:x+w]" is just the output from your haarcascade. Maybe you recieve multiple output so you have to choose one.

Mare Seestern
- 335
- 1
- 3
- 13
0
After detect face you can do this.
eyes = eye_cascade.detectMultiScale(roi_gray)
for ex, ey, ew, eh in eyes:
roi = roi_color[ey:ey + ew, ex:ex + eh]
Then can show roi using cv2.imshow().

Milon Mahato
- 180
- 1
- 12
0
The haarcascade_eye.xml and haarcascade_frontalface_default.xml link file
import cv2
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
#load image saved in the same directory
img = cv2.imread("your_image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for f in faces:
x, y, w, h = [ v for v in f ]
cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
alt_gray = gray[y:y+h, x:x+w]
alt_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(alt_gray)
eye_crop = []
for f in eyes:
x, y, w, h = [ v for v in f ]
cv2.rectangle(alt_color, (x,y), (x+w, y+h), (255,0,0), 2)
# Define the region of interest in the image
eye_crop.append(alt_gray[y:y+h, x:x+w])
for eye in eye_crop:
cv2.imshow('eye',eye)
cv2.waitKey(0)

Inadel
- 101
- 7