1

I'm receiving a detectMultiScale error. I made sure to specify the full path of the file. I am aiming to progress to real-time object recognition and tracking.

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('/home/pi/opencv-3.1.0/data/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/pi/opencv-3.1.0/data/haarcascades/haarcascade_eye.xml')
face_cascade.load('haarcascade_frontalface_default.xml')

img = cv2.imread('/home/pi/cam.jpg',1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY,0)

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

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here is the error code

Traceback (most recent call last):
  File "/home/pi/face.py", line 11, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
error: /home/pi/opencv-3.1.0/modules/objdetect/src/cascadedetect.cpp:1639: error: (-215) !empty() in function detectMultiScale
Dartmouth
  • 1,069
  • 2
  • 15
  • 22
XOR
  • 407
  • 6
  • 16
  • 1
    is the xml file present in that location in the code ? check that. And the third line after imports, why are you loading the file it is already read from the above line – Tes3awy Aug 23 '16 at 16:46

1 Answers1

0

OpenCV way:

You need a file for the HAAR cascade parameters. Sometimes that needs to be downloaded and isn't provided by default. this issue might give you hints about where to find them.

"face recognition" module

dlib is the defacto C++ library now for haar-cascade for face extraction.

The face recognition module in python wraps dlib and can handle both face detection and recognition.

https://pypi.org/project/face-recognition/

Conic
  • 998
  • 1
  • 11
  • 26