I am trying to develop a simple application to detect faces as well as eyes in a given image:
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()
(Note: The rotation is necessary, as after using the cvtColor
function, the output image is generated with a rotation of 90 degrees counter clockwise.)
I am getting the following error:
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