Development Tip

오류 : detectMultiScale 함수의 (-215)! empty ()

yourdevel 2020. 12. 11. 20:23
반응형

오류 : detectMultiScale 함수의 (-215)! empty ()


나는 파이썬 2.7에서 cv2를 배우려고 노력하고 있지만 코드를 실행할 때 특정 부분에서 다음을 수행합니다.

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
 eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')


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)

다음을 반환합니다.

File "face_detection.py", line 11, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: /home/arthurckl/Desktop/opencv-3.0.0-rc1/modules/objdetect/src/cascadedetect.cpp:1595: error: (-215) !empty() in function detectMultiScale

여기서 답을 찾으려고했지만 가장 좋은 방법은 face_cascade를 잘못된 방식으로로드해야한다는 것입니다. 도움이 필요하십니까?


XML 또는 파일이 누락되었거나 경로가 올바르지 않거나 create_capture 경로가 올바르지 않습니다.

opencv 샘플의 경로는 다음과 같습니다.

cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
nested_fn  = args.get('--nested-cascade', "../../data/haarcascades/haarcascade_eye.xml")

cam = create_capture(video_src, fallback='synth:bg=../data/lena.jpg:noise=0.05')

같은 코드를 실행했습니다. 여기서 주목해야 할 두 가지가 있습니다. 1. .xml 파일의 전체 경로를 제공합니다. 2. 마지막에 키 누름 이벤트 명령을 제공합니다.

끝에이 코드 블록을 추가하고 파일을 실행하십시오.

k = cv2.waitKey(0)
if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
    cv2.imwrite('messigray.png',img)
    cv2.destroyAllWindows()

예를 들어, 내 코드는 다음과 같습니다.

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_eye.xml')

img = cv2.imread('lena.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#faces = face_cascade.detectMultiScale(gray)

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]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

cv2.imshow('img',img)

k = cv2.waitKey(0)
if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
    cv2.imwrite('messigray.png',img)
    cv2.destroyAllWindows()

내 출력은 여기에 이미지 설명을 입력하는 것처럼 보입니다.


XML 파일이 누락되었습니다. GitHub 저장소에서 파일을 가져 와서 프로젝트와 동일한 디렉토리에 배치 할 수 있습니다. GitHub의 폴더 링크는 여기 입니다. haarcascade_frontalface_default.xml 이라는 파일을 다운로드하면됩니다 . 실제로 파일은 시스템에 있습니다. Python 설치 폴더 site-packages 폴더 로 이동 하여 cv2 / data 폴더에서 파일을 확인하십시오.


전체 파일 경로를 사용하고 xml 파일 경로에서 "\"대신 "\\"를 사용하십시오.

파일 경로는 다음과 같아야합니다.

face_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')

대신에:

cascade_fn = args.get('--cascade', "..\..\data\haarcascades\haarcascade_frontalface_alt.xml")

Anaconda를 사용하는 경우 Anaconda 경로를 추가해야합니다.

new_path = 'C:/Users/.../Anaconda/Library/etc/haarcascades/'

face_cascade = cv2.CascadeClassifier(new_path + 'haarcascade_frontalface_default.xml')

homebrew를 사용하는 OSX에서는 opencv 폴더의 전체 경로가 작동합니다.

face_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/3.4.0_1/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/3.4.0_1/share/OpenCV/haarcascades/haarcascade_eye.xml')

경로의 버전 번호를 확인하십시오.


코드를 변경할 필요가 없습니다

download that .xml file , then put the path of that file

it will solve the error (100%)


Probably the face_cascade is empty. You can check if the variable is empty or not by typing following command:

face_cascade.empty()

If it is empty you will get True and this means your file is not available in the path you mentioned. Try to add complete path of xml file as follows:

r'D:\folder Name\haarcascade_frontalface_default.xml'

"\Anaconda3\Lib\site-packages\cv2\data\" I found the xml file in this path for Anaconda


You may find such kind of errors when you did not define the complete path of your XML file. Try this one if you are using opencv3.1.0 in raspberrypi 3: "faceCascade = cv2.CascadeClassifier('/home/pi/opencv-3.1.0/data/haarcascades/haarcascade_frontalface_default.xml')"


the error may be due to, the required xml files has not been loaded properly. Search for the file haarcascade_frontalface_default.xml by using the search engine of ur OS get the full path and put it as the argument to cv2.CascadeClassifier as string


Your XML file was not found. Try using absolute paths like:

/path/to/my/file (Mac, Linux)
C:\\path\\to\\my\\file (Windows)

The error occurs due to missing of xml files or incorrect path of xml file.

Please try the following code,

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.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]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

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

cap.release()
cv2.destroyAllWindows()

Please do not copy paste the content of xml file, because once you paste it to notepad it will be saved a s text file. So directly download the file from the given source.


I had the same problem with opencv-python and I used a virtual environment. If it's your case, you should find the xml files at:

/home/username/virtual_environment/lib/python3.5/site-packages/cv2/data/haarcascade_frontalface_default.xml

/home/username/virtual_environment/lib/python3.5/site-packages/cv2/data/haarcascade_eye.xml

Please be sure that you're using the absolute path. Otherwise, it won't work.

참고URL : https://stackoverflow.com/questions/30508922/error-215-empty-in-function-detectmultiscale

반응형