Hello I am trying to create a facial recognition program but I have a peculiar error: here is my code:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier("lbpcascade_frontalface.xml")
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
and this error is the output
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set
I have "lbpcascade_frontalface.xml" in the working directory so that shouldn't be an issue
if it helps when I enter
cv2.__version__
I get
'4.0.0'
New Answer OpenCV seems to now have a directory dedicated to cascades, they are placed in data
and I am seeing something like this floating around in tutorials now haar_cascade_face = cv2.CascadeClassifier('data/haarcascade/haarcascade_frontalface_default.xml')
You may have to find where data
is on your machine or the above my work. I have not touched this project since I finished it in early 2019. Bear in mind this only works for frontal face, if you want to use Haar's Cascade for eyes that is a separate file.
old answer Turns out I didn't need to download another file and use it because opencv comes with it this little bit of code worked
cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
Well I was in this same problem, as @TylerStrouth mentioned this code snippet doesn't work :
cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
because there are no haarcascades files in data directory if you have just installed opencv in a standard format of pip install opencv-python
or sudo apt-get install python3-opencv
You will get an error something similar to this stackoverflow question , therein is the mentioned solution that worked for me, that is if you're using python3 then you need also to install opencv-contrib-python before running the above code snippet.
pip install opencv-contrib-python
which has full package (contains both main modules and contrib/extra modules )
如下更改您的代码,这对我有用
har_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +'har.xml')
As explained by @TylerStrouth above opencv has a directory of cascades in which the cascade files are available, I also faced the same problem while running the code for face detection on Ubuntu 16.04 and solved it as follows
Get the location of opencv using
whereis opencv
Mine was in /usr/share/opencv
Check whether the cascades are present in that location and copy paste the location in cv2.CascadeClassifier along with the required haarcascade
I have encountered same issue in little different way. I was using Jupiter notebook to execute code here
I copied XML file from here and created a XML file in current Jupiter directory, when loading this files using below:
classifier = CascadeClassifier('haarcascade_frontalface_default.xml')
Its returned me error :
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set
So, I tried other way, removed this file, and downloaded actual file as XML format in current directory, which resolved my issue.
I was having the same error when i was using hogcascade_pedestrians.xml
to detect pedestrians from a local video and i was reading the hogcascade_pedestrians.xml
as follows:
pedestrainsClassifier = cv2.CascadeClassifier("hogcascade_pedestrians.xml")
Of which you should read it as follows:
pedestrainsClassifier = cv2.CascadeClassifier(f"{cv2.data.haarcascades}hogcascade_pedestrians.xml")
Alternatively you can do it as follows:
pedestrainsClassifier = cv2.CascadeClassifier(cv2.data.haarcascades +"hogcascade_pedestrians.xml")
Good luck
在 opencv-python 的3.4.9.33
版( pip show opencv-python
,Windows)上,以下行工作正常: trained_face_data = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.