Ask Your Question

Error with cv2.CascadeClassifier() using Trained Classifier

asked 2017-07-06 15:35:48 -0500

Highwayman gravatar image

The below code runs fine when 'cascade.xml' is replaced with the 'haarcascade_frontalface_default.xml' classifier included with opencv. However, it throws this error using the cascade.xml file I created by going through the training process.

Traceback (most recent call last): File "/home/pi/Desktop/", line 4, in <module> face_cascade = cv2.CascadeClassifier('cascade.xml') error: /home/pi/opencv-3.2.0/modules/core/src/persistence.cpp:2659: error: (-212) cascade.xml(114): Preliminary end of the stream in function icvXMLParseTag

Both xml files are in the same directory in the python script, so it seems there is something wrong with my xml file. I noticed the format of the two is different, and it seems some things are missing from the end of mine. Did training not complete correctly? It went through five stages as requested with the stages parameter. Here's the xml file since I can't attach it

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('cascade.xml')
img = cv2.imread('image.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-07-08 02:35:13 -0500

berak gravatar image

updated 2017-07-08 02:37:12 -0500

yes, your cascade is broken.

you might try to go back to the training, delete the cascade.xml from the stages folder (but NOT the stageX.xml files) , restart opencv_traincascade with your prev params, it should take offat the last stage you computed.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-07-06 15:35:48 -0500

Seen: 4,943 times

Last updated: Jul 06 '17