Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot locate haarcascade default frontal face XML file on windows

I am new to image processing and i've been getting my hands dirty with opencv for python for past few weeks. Today I tried to use the default haarcascade XML file for the face detection. Here is my code:

import cv2
import numpy as np

front_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

img = cv2.imread('lena.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = front_cascade.detectMultiScale(gray, 1.3, 6)
#for (x, y, w, h) in faces:
#    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 155), 3)

print faces

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

Problem here is that it prints out an empty set. And to my surprise when I change the name of the XML file to something like 'aaa.xml', it still produces the same output. Any help would be highly appreciated.

like image 681
Ahmed Dhanani Avatar asked Oct 25 '25 09:10

Ahmed Dhanani


1 Answers

When you say front_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml'), the cascade file must be present in the same folder in which the program is being executed, Best practice is to pass the full qualified path of the cascade file, If you are not able to locate the cascade files bundled with opencv then you can also download the .xml files from this link.

And then you may load the haarcascde by passing in the full qualified path:

front_cascade = cv2.CascadeClassifier('/Users/anmoluppal/Downloads/cascades/haarcascade_frontalface_default.xml')
like image 151
ZdaR Avatar answered Oct 26 '25 22:10

ZdaR