OpenCV面部检测出现此错误 [英] OpenCV Facial Detection come ups with this error

查看:246
本文介绍了OpenCV面部检测出现此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到此错误:

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

尝试运行此代码时

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascase_frontalface_default.xml')

cap = cv2.VideoCapture(0)

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

    cv2.imshow('img', img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

我在[this [(https://www.youtube.com/watch?v=88HdqNDQsEk&t=432s)YouTube视频]中找到了代码.

I found the code in [this[(https://www.youtube.com/watch?v=88HdqNDQsEk&t=432s) YouTube video.

推荐答案

我认为这是由于缺少.xml文件造成的.您可以检查.xml文件的路径是否正确吗?

I believe this is due to the missing .xml file. Can you check if the path your your .xml file is correct?

还是您从其他地方启动该程序?由于路径将从您运行程序的位置开始.

Or do you start this program from elsewhere? Since the path will be start from where you run the program.

例如,即使您的python文件也与.xml文件位于同一目录中.如果您从其他目录(如 python folder/app.py )运行python,则会抛出错误.您必须将xml路径改为"folder/model.xml".

For example, even your python file is in the same directory as .xml file. If you run python from other directory like python folder/app.py It will throw the error. You will have to change your xml path to 'folder/model.xml' instead.

这篇关于OpenCV面部检测出现此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆