[英]OpenCV 4.0.0 SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set
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我在工作目录中有“lbpcascade_frontalface.xml”,所以这应该不是问题
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.新答案OpenCV 现在似乎有一个专用于级联的目录,它们被放置在data
,我现在在教程中看到类似这样的东西haar_cascade_face = cv2.CascadeClassifier('data/haarcascade/haarcascade_frontalface_default.xml')
你可能必须查找data
在您的机器上或以上我的工作中的位置。 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.自从我在 2019 年初完成这个项目以来,我一直没有接触过这个项目。请记住,这仅适用于正面,如果您想将 Haar's Cascade 用于眼睛,这是一个单独的文件。
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旧答案原来我不需要下载另一个文件并使用它,因为 opencv 附带了这一点代码工作
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 :好吧,我遇到了同样的问题,因为@TylerStrouth 提到此代码片段不起作用:
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
因为如果您刚刚以pip install opencv-python
或sudo apt-get install python3-opencv
的标准格式安装了opencv ,则数据目录中没有 haarcascades 文件
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.您将收到类似于此 stackoverflow question的错误,其中提到的解决方案对我有用,也就是说,如果您使用的是 python3,那么您还需要在运行上述代码片段之前安装opencv-contrib-python 。
pip install opencv-contrib-python
which has full package (contains both main modules and contrib/extra modules )它有完整的包(包含主要模块和 contrib/extra 模块)
如下更改您的代码,这对我有用
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正如上面@TylerStrouth 所解释的,opencv 有一个级联目录,其中级联文件可用,我在 Ubuntu 16.04 上运行人脸检测代码时也遇到了同样的问题,解决方法如下
Get the location of opencv using使用获取opencv的位置
whereis opencv哪里有opencv
Mine was in /usr/share/opencv我的在 /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检查级联是否存在于该位置,并将该位置与所需的 haarcascade 复制粘贴到 cv2.CascadeClassifier 中
I have encountered same issue in little different way.我以不同的方式遇到了同样的问题。 I was using Jupiter notebook to execute code here我在这里使用 Jupiter notebook 执行代码
I copied XML file from here and created a XML file in current Jupiter directory, when loading this files using below:我从这里复制了 XML 文件并在当前的 Jupiter 目录中创建了一个 XML 文件,当使用以下方法加载此文件时:
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.所以,我尝试了其他方式,删除了这个文件,并在当前目录中以 XML 格式下载了实际文件,这解决了我的问题。
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:当我使用hogcascade_pedestrians.xml
从本地视频中检测行人时,我遇到了同样的错误,我正在阅读hogcascade_pedestrians.xml
,如下所示:
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.