我正在尝试开发一个简单的应用程序,以便在给定的图片中检测人脸和眼睛:
from cv2 import *
face_cascade = CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = CascadeClassifier('haarcascade_eye.xml')
img = imread("123.jpg")
gray = cvtColor(img, COLOR_BGR2GRAY)
rows,cols = gray.shape
gray = getRotationMatrix2D((cols/2,rows/2),-90,1)
faces = face_cascade.detectMultiScale(gray, 1.3, 5, 0)
print faces
for (x,y,w,h) in faces:
img = rectangle(img, (x,y), ((x+w),(x+h)), (255,0,0), 2)
#gray = rectangle(gray, (x,y), ((x+w), (x+y)), (0, 255, 0), 4)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_grey)
for (ex,ey, ew, eh) in eyes:
roi_color = rectangle(roi_color, (x,y), ((x+w), (y+h)), (50, 50, 50), 3)
imshow("img", img)
waitKey(9)
destroyAllWindows()
(注意:旋转是必需的,因为使用函数后,输出图像会逆时针旋转90度。)
我遇到了以下错误:
Traceback (most recent call last): File "/home/namit/Codes/wow.py", line 10, in faces = face_cascade.detectMultiScale(gray, 1.3, 5, 0) error: /home/namit/OpenCV/opencv-2.4.9/modules/objdetect/src/cascadedetect.cpp:1081: error: (-215) scaleFactor > 1 && image.depth() == CV_8U in function detectMultiScale