2017-07-25 184 views
0

當我執行的Python代碼下面提到我得到一個錯誤信息:OpenCV的由蟒蛇erorr

Traceback (most recent call last):

File "C:\Users\smart-26\Desktop\예제\face.py", line 28, in

faces = face_cascade.detectMultiScale(grayframe, 1.1, 3, 0, (30, 30))

cv2.error: D:\Build\OpenCV\opencv->

3.2.0\modules\objdetect\src\cascadedetect.cpp:1681: error: (-215) !empty() in

function cv::CascadeClassifier::detectMultiScale

我不明白這條消息:

error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

這裏是我到目前爲止的代碼:

import cv2 

CAM_ID = (0) 

cap = cv2.VideoCapture(CAM_ID) 

if cap.isOpened() == False: 
    print ('Can\'t open the CAM(%d)' % (CAM_ID)) 
    exit() 

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

while(True): 

    ret, frame = cap.read() 

    grayframe = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    grayframe = cv2.equalizeHist(grayframe) 

    faces = face_cascade.detectMultiScale(grayframe, 1.1, 3, 0, (30, 30)) 
     for (x,y,w,h) in faces: 
     cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3, 4, 0) 


    cv2.imshow('Face',frame) 


    if cv2.waitKey(10) >= 0: 
     break; 


cv2.namedWindow('Face') 
cap.release() 
cv2.destroyWindow('Face') 
+1

看看[源代碼](https://github.com/opencv/opencv/blob/3.2.0/modules/objdetect/src/cascadedetect.cpp#L1681)。這會調用'CascadeClassifierImpl'的['empty'](https://github.com/opencv/opencv/blob/3.2.0/modules/objdetect/src/cascadedetect.cpp#L888)方法。這暗示分類器XML不能正確加載。 |你的代碼沒有任何錯誤檢查。加載XML以及讀取幀都可能失敗。你需要處理。 –

回答