【问题标题】:cv2.CascadeClassifier.detectMultiScale() failing with -215:Assertion failedcv2.CascadeClassifier.detectMultiScale() 以 -215 失败:断言失败
【发布时间】:2021-06-27 14:46:12
【问题描述】:

我在 Windows 10 上使用 Python v3.8.3 和 OpenCV v4.5.1。我要做的就是通过尝试检测人脸来验证 OpenCV 是否正常工作。这是input image。我见过这个answer,但这并不能解决我的问题。

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 opencv face-detection


    【解决方案1】:

    使用这行代码解决了我的问题:

    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2022-10-15
      • 2019-12-04
      • 2021-09-10
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多