opencv cascade.detectMultiScale creates error: (-215) !empty()

Question:

I am following this tutorial to understand haar features. While writing following code:

import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('Sachin.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

getting following error:

File "<ipython-input-6-0b479e459b0f>", line 1, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    error: /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/objdetect/src/cascadedetect.cpp:1639: error: (-215) !empty() in function detectMultiScale

(Sachin is the image i downloaded from google, of size (237,237,3) and after converting gray it is (237,237) with min=23, max=210. I am using opencv 3.1.0 with python 3.6, installed using command conda install -c menpo opencv3. i am trying for this solution but unable to find xml file in my desktop. How to solve problem.

Asked By: Hitesh

||

Answers:

You need to specify the full paths to the XML files for Haar Cascade.

On Ubuntu 16.04, these files can be found in /usr/share/opencv/haarcascades.

Answered By: Sunreef

You should be in the same directory as the xml file . Hope this helps!

Answered By: Muhammad Abdullah

You need to give full path for haarcascade_frontalface_default.xml and haarcascade_eye.xml.

use cv2.data.haarcascades which will give you the path.

face_cascade = cv2.CascadeClassifier(
    cv2.data.haarcascades+'haarcascade_frontalface_default.xml')

eye_cascade = cv2.CascadeClassifier(
    cv2.data.haarcascades+'haarcascade_eye.xml')
Answered By: Udesh Ranjan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.