Python cv2.CascadeClassifier.detectMultiScale()因-215失败:断言失败
我正在Windows10上使用Python v3.8.3和OpenCV v4.5.1。我所要做的就是通过检测人脸来验证OpenCV是否正常工作。这是你的电话号码。我看到了,但这并不能解决我的问题 编辑:为什么投票被否决Python cv2.CascadeClassifier.detectMultiScale()因-215失败:断言失败,python,opencv,face-detection,Python,Opencv,Face Detection,我正在Windows10上使用Python v3.8.3和OpenCV v4.5.1。我所要做的就是通过检测人脸来验证OpenCV是否正常工作。这是你的电话号码。我看到了,但这并不能解决我的问题 编辑:为什么投票被否决 import cv2 # Load the cascade face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') print(f"face: {face_cascade}
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
print(f"face: {face_cascade}")
# Read the input image
img = cv2.imread('./images/female_frontal.jpg')
print(f"img: {img}")
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(f"gray: {gray}")
# Detect faces
faces = face_cascade.detectMultiScale(gray)
print(f"faces: {faces}")
输出:
$ python profile_dectector.py
face: <CascadeClassifier 019F3220>
img: [[[197 174 128]
[196 173 127]
[196 173 127]
...
[105 61 37]
[108 64 40]
[114 66 42]]]
gray: [[163 162 162 ... 171 168 166]
...
[166 156 153 ... 59 62 64]]
Traceback (most recent call last):
File "profile_dectector.py", line 18, in <module>
faces = face_cascade.detectMultiScale(gray)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-xeqjxthj\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
$python profile\u detector.py
面对:
img:[[197174 128]
[196 173 127]
[196 173 127]
...
[105 61 37]
[108 64 40]
[114 66 42]]]
灰色:[[163 162 162…171 168 166]
...
[166 156 153 ... 59 62 64]]
回溯(最近一次呼叫最后一次):
文件“profile_detector.py”,第18行,在
面=面\级联。检测多尺度(灰色)
cv2.error:OpenCV(4.5.1)C:\Users\appveyor\AppData\Local\Temp\1\pip req build xeqjxthj\OpenCV\modules\objdetect\src\cascadedetest.cpp:1689:error:(-215:断言失败)!函数“cv::CascadeClassifier::detectMultiScale”中的空()
使用这行代码解决了我的问题:
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')