简体   繁体   English

错误:(-215:Assertion failed):empty() in function 'cv::CascadeClassifier::detectMultiScale'

[英]Error:(-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

I am trying out Face-detection using "Open-CV" by python.我正在尝试使用 python 的“Open-CV”进行人脸检测。 And the program is throwing me an error that I couldn't understand as a python beginner.该程序向我抛出了一个错误,作为 python 初学者,我无法理解。 The Error from Program:程序的错误:

Traceback (most recent call last):
  File "c:\Users\(User's name)\Documents\Python\Face-dection\Facedectection.py", line 6, in <module>
    gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' 

The code:编码:

import cv2

face_cascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

img = cv2.imread("photo.jpg")
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

faces=face_cascade.detectMultiScale(gray_img, scaleFactor=1.05,minNeighbors=5)

for x, y, w, h in faces:
    img=cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)

resized=cv2.resize(img,(int(img.shape[1]/3)),(int(img.shape[0]/3)))

cv2.imshow("Deteced-face",resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note: Photo and the.xml file is placed in the same folder注意:照片和.xml文件放在同一个文件夹下

You need to add cv2.data.haarcascades before referencing to the .xml file.您需要在引用.xml文件之前添加cv2.data.haarcascades

#[...]

face_cascade=cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") #Note the change

#[...]

Also there's one more problem in your code, have a look at this block of code:您的代码中还有一个问题,请查看以下代码块:

resized=cv2.resize(img,(int(img.shape[1]/3)),(int(img.shape[0]/3)))

You're supposed to pass an image and it's new width and height (in a tuple format) as the arguments but you're passing the image and the width and height as separate integers and not in a tuple format.您应该将图像及其新的宽度和高度(以元组格式)作为 arguments 传递,但是您将图像以及宽度和高度作为单独的整数而不是元组格式传递。

This is how it should look like:它应该是这样的:

resized=cv2.resize(img,(int(img.shape[1]/3), int(img.shape[0]/3))) 
'''
Notice I have removed an extra bracket after
cv2.resize(img,(int(img.shape[1]/3)) -> cv2.resize(img,(int(img.shape[1]/3)...
and also I have removed an extra bracket before
(int(img.shape[0]/3))) -> int(img.shape[0]/3)))
'''

This should work:这应该有效:

import cv2

face_cascade=cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") #Note the change

img = cv2.imread("photo.jpg")
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

faces=face_cascade.detectMultiScale(gray_img, scaleFactor=1.05,minNeighbors=5)

for x, y, w, h in faces:
    img=cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)

resized=cv2.resize(img,(int(img.shape[1]/3), int(img.shape[0]/3))) 

cv2.imshow("Deteced-face",resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cv2.error: OpenCV(4.0.0) (-215:Assertion failed) !_src.empty() in function &#39;cv::cvtColor&#39; - cv2.error: OpenCV(4.0.0) (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' cv2.error:(-215:Assertion failed) !_src.empty() in function &#39;cv::cvtColor&#39; - cv2.error:(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' 如何修复opencv / modules / objdetect / src / cascadedetect.cpp:1698:错误:(-215:断言失败)!empty()在函数&#39;detectMultiScale&#39;^中? - How to fix opencv/modules/objdetect/src/cascadedetect.cpp:1698: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'^? 在函数 &#39;cv::resize&#39; 中检索 opencv 错误(-215:Assertion failed)!ssize.empty() - Retrieving opencv error (-215:Assertion failed) !ssize.empty() in function 'cv::resize' 面临错误:(-215:Assertion failed)._src:empty() in function 'cv::cvtColor' - facing a the error : (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' OpenCV 分类器错误:(-215) !empty() in function detectMultiScale - OpenCV Classifier error: (-215) !empty() in function detectMultiScale 错误:(-215:断言失败)_src.total() &gt; 0 在 function 'cv::warpPerspective' - error: (-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective' OpenCV 4.4.0 错误:(-215:Assertion failed) !_src.empty() 在函数 &#39;cv::cvtColor&#39; 中,14 个其他错误模块 cv2 - OpenCV 4.4.0 error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' , 14 other erros module cv2 错误:(-215:Assertion failed).ssize.empty() in function 'resize' 在为框架设置 cv2.resize 时出现 - error: (-215:Assertion failed) !ssize.empty() in function 'resize' shows up when setting a cv2.resize for frame \\ resize.cpp:3787:错误:(-215:断言失败)函数!= 0在函数&#39;cv :: hal :: resize&#39;中 - \resize.cpp:3787: error: (-215:Assertion failed) func != 0 in function 'cv::hal::resize'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM