"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > 人脸检测失败原因及解决方案:Error -215

人脸检测失败原因及解决方案:Error -215

2025-04-16에 게시되었습니다
검색:577

\

Error Handling: Resolving "error: (-215) !empty() in function detectMultiScale" in OpenCV

When attempting to utilize the detectMultiScale() method to detect faces within an image, you may encounter the error "error: (-215) !empty() in function detectMultiScale." This error typically arises when the face cascade classifier, a crucial component for face detection, is not loaded correctly.

To resolve this issue, it is essential to ensure that the path provided to the Haar cascade XML file is valid. In the provided code snippet, the cascade classifier is being loaded with hardcoded paths, which may not be accurate for your system. Instead, OpenCV provides a convenient property to locate these files automatically.

The updated code below demonstrates how to rectify the issue using OpenCV's property:

import cv2

# Use OpenCV's property to locate the Haar cascade XML files
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

# Proceed with face detection
img = cv2.imread('2015-05-27-191152.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
    img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

By utilizing OpenCV's property, you can ensure that the face cascade classifier is loaded correctly, resolving the "error: (-215) !empty() in function detectMultiScale" issue.

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3