2017-08-07 113 views
1

我正在嘗試使用cv2在python-3.6中學習人臉檢測。OpenCV錯誤:Python中的未知錯誤代碼-49

我遵循書中給出的src。

我已經通過PIP安裝的OpenCV的Python(3.2.0)

.XML和.jpg文件都與Python代碼相同的路徑。

from numpy import * 
import cv2 

face_cascade = cv2.CascadeClassifier("D:\\Python\\FaceDetec\\lbpcascade_frontalface.xml") 

img = cv2.imread("z1.jpg") 
gary = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

faces = face_cascade.detectMultiScale(gray, 1.2, 3) 
for(x, y, w, h) in faces: 
    img2 = cv2.rectangle(img, (x, y), (x+w, y+h), (255, 255, 255), 2) 
    roi_gray = gray[y:y+h, x:x+w] 
    roi_color = img[y:y+h, x:x+w] 

cv2.imshow("img", img) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 
cv2.imwrite("z1.head.jpg", img) 

我收到一個錯誤,我找不到任何地方。

OpenCV Error: Unknown error code -49 (Input file is empty) in cvOpenFileStorage, file D:\Build\OpenCV\opencv-3.2.0\modules\core\src\persistence.cpp, l 
ine 4422 
Traceback (most recent call last): 
    File "d:\Python\FaceDetec\Harr_cascade.py", line 6, in <module> 
    face_cascade = cv2.CascadeClassifier("D:\\Python\\FaceDetec\\lbpcascade_frontalface.xml") 
cv2.error: D:\Build\OpenCV\opencv-3.2.0\modules\core\src\persistence.cpp:4422: error: (-49) Input file is empty in function cvOpenFileStorage 

請在此幫助我。謝謝。

+0

lbpcascade_frontalface.xml從https://github.com/opencv/opencv/tree/master/data/lbpcascades –

+1

下載對不起,我罰的問題。我從http://opencv.org/ 更改.xml一切正常。謝謝 –

回答

0

您應該在關閉窗口之前保存圖像並將'加利'修復爲'灰色'。 像這樣改變你的代碼。

from numpy import * 
import cv2 

face_cascade = cv2.CascadeClassifier("D:\\Python\\FaceDetec\\lbpcascade_frontalface.xml") 

img = cv2.imread("z1.jpg") 
gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

faces = face_cascade.detectMultiScale(gray, 1.2, 3) 
for(x, y, w, h) in faces: 
    img2 = cv2.rectangle(img, (x, y), (x+w, y+h), (255, 255, 255), 2) 
    roi_gray = gray[y:y+h, x:x+w] 
    roi_color = img[y:y+h, x:x+w] 

cv2.imshow("img", img) 
cv2.imwrite("z1.head.jpg", img) 
cv2.waitKey(0) 
cv2.destroyAllWindows()