3

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.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Hitesh
  • 1,285
  • 6
  • 20
  • 36

3 Answers3

7

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.

Sunreef
  • 4,452
  • 21
  • 33
  • I am not finding this location /home/nd/share/opencv/haarcascades. after changing same error again – Hitesh Nov 16 '17 at 13:37
  • Why `/home/nd` ? I said `/usr`. Are you new to Ubuntu ? – Sunreef Nov 16 '17 at 13:39
  • I change line 3 and 4 i.e. face_cascade and eye_cascade lines but how can i insert /usr/share/opencv/haarcascades in last line i.e. faces = face_cascade.detectMultiScale(gray, 1.3, 5), and error is in this line – Hitesh Nov 16 '17 at 13:43
  • The program crashing at one line doesn't necessarily mean that the error can be found at that precise line. – Sunreef Nov 16 '17 at 14:48
  • `import numpy as np import cv2 face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades_eye.xml') img = cv2.imread('Sachin.jpeg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5)` and getting same error – Hitesh Nov 16 '17 at 14:57
  • 1
    You still don't have the right path... Did you read my answer ? It should be `'/usr/share/opencv/haarcascades/haarcascades_fronta‌​lface_default.xml'` – Sunreef Nov 16 '17 at 15:20
  • Sry for my mistake. But now i modified the code to `import numpy as np import cv2 face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascades_fronta‌​lface_d‌​efault.xml') eye_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascades_eye.xm‌​l') img = cv2.imread('Sachin.jpeg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5)` and still getting same error – Hitesh Nov 16 '17 at 15:32
  • 1
    Can you open a terminal and run these commands: `cd /usr && find -name haarcascade*` This should give you the location of the xml files. Make sure the path is right. If nothing appears, then the files are not there and you'll need to search for them somewhere else or verify your OpenCV installation – Sunreef Nov 16 '17 at 15:47
  • I exactly type `cd /usr && find -name haarcascade*` this on my terminal. nothing appears. SO i have to reinstall opencv. what do you mean by `make sure the path is right` ? – Hitesh Nov 16 '17 at 15:58
  • 1
    Other simple solution is to download the haar files directly and put them in the same folder you are running your program on, that way you don't need to put the whole path and your program will work. That will also make your program work if you change your env. – roccolocko Nov 17 '17 at 06:14
  • @Sunreef thnx. Now your given solution works for me. – Hitesh Nov 20 '17 at 13:25
2

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

Muhammad Abdullah
  • 305
  • 1
  • 2
  • 10
1

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')
Udesh
  • 2,415
  • 2
  • 22
  • 32