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.
答案 0 :(得分:5)
当你说front_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
时,级联文件必须存在于执行程序的同一文件夹中,最佳做法是传递级联文件的完整限定路径,如果你不能找到与opencv捆绑在一起的级联文件,然后您也可以从this link下载.xml
个文件。
然后你可以通过传递完整的合格路径加载haarcascde:
front_cascade = cv2.CascadeClassifier('/Users/anmoluppal/Downloads/cascades/haarcascade_frontalface_default.xml')
答案 1 :(得分:0)
对于那些试图在Windows conda环境下找到它的人来说,这有助于我找到级联文件。
使用CMD提示符:
dir %CONDA_PREFIX% /s /a /b | findstr /i haarcascade