OpenCV Error: Unknown error code -49 in Python

Viewed 6251

I am trying to learn face detection in python-3.6 using cv2.

I follow the src given in a book.

I have already installed opencv-python(3.2.0) by pip

.xml and .jpg files are all in the same path with python code.

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)

I am getting an error to which I am not able to find anywhere.

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

Kindly help me in this. Thank you.

4 Answers

I have spent lot of manhours on this small trivial issue. The real issue is that Open CV will work only with the Haarcascade XML files, which are available on the Opencv.org website and not from the github

I had the same error, and it was because you downloaded the model using the method of clicking the right buttom mouse and "Download as...". If you open that file, you will see that the structure of the XML is wrong. It should start with something like this:

<?xml version="1.0"?>
<!--
number of positive samples 3000
number of negative samples 1500
-->
<opencv_storage>
<cascade type_id="opencv-cascade-classifier">
  <stageType>BOOST</stageType>
  <featureType>LBP</featureType>

So, you should open as raw the file in the repository(https://github.com/opencv/opencv/blob/master/data/lbpcascades/lbpcascade_frontalface.xml) and copy it or download the repository using git.

Even my answer is 3 years later, I hope it helps someone.

Related