There is no haarcascades file! Since it may cause an error, I think it is a good idea to create a haarcascades directory in the same hierarchy as the py file and copy the entire cascade file.
haarcascade_eye.xml haarcascade_frontalface_default.xml
It works even if you pick up two of them and copy them.
checkcv.py
import cv2
face_cascade = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('./haarcascades/haarcascade_eye.xml')
img = cv2.imread('./images/img.jpg')
gray_scale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray_scale)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
r_gray = gray_scale[y:y+h, x:x+w]
r_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(r_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(r_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
I have to study more.
Recommended Posts