implement own Haar-Cascade


currently I am trying to implement my own hardhat-recognition to my MV-cam. For the Cascade classifier I used openCV and with my webcam it is working. Then I created a new .xml-classifier-file with the additional parameters “-SaveBaseFormat” and “-FeatureType HAAR” in order to make it usable for the MV-cam. Then I used this ( script to transform it into a .cascade-file which I included like this into my micropython sript:

the cascade script is on the internal flash drive

hat_cascade = image.HaarCascade(“/cascade_scenario4.cascade”, stages=15)

while (True):

Capture snapshot

img = sensor.snapshot()

Find objects.

Note: Lower scale factor scales-down the image more and detects smaller objects.

Higher threshold results in a higher detection rate, with more false positives.

objects = img.find_features(hat_cascade, threshold=1, scale_factor=1.35)

Draw objects

for r in objects:

But my script just does nothing. It gets the snapshots, but recognized nothing. Does anyone have any experience in including own haar-cascades?
Did I implement it right?

best regards

I’ve altered Ibrahim to this issue. Not sure how to start the debugging process for this as I don’t know this part of the code.

Can you print the Haar Cascade object to see if it was loaded without issues?

print hat_cascade

Hi, I’m not sure why it’s not working, try to remove the stages argument to test all stages. If you can you attach the xml and binary cascades I’ll test them for you.

Hi :slight_smile:

yes the print(hat_cascade) works. It prints values like:

I attached the XML, but I can’t attach the .cascade-file due to the filetype…?

Best regards
cascade_scenario4.xml (67 KB)

Zip it up.

It seems to be working on some hats and not others:

Note it has 16 stages (the script prints cascade info) you should use them all.

I used all cascade stages by just leaving out the stages arg:

hat_cascade = image.HaarCascade("/cascade_scenario4.cascade")

Also used a 0.85 threshold and 1.15-1.25 scaling factor.

Thanks for the replies :slight_smile:
In the attachment ist the zipped up XML & Cascade

best regards (10.1 KB)

1 Like

Sorry for asking again, but can someone tell me wheather this .cascade-file is working?

Currently, when I run this code:

import sensor, time, image, pyb

Reset sensor


Sensor settings


HQVGA and GRAYSCALE are the best for face tracking.


greenLed = pyb.LED(2) # define green LED.

Load Haar Cascade

By default this will use all stages, lower satges is faster but less accurate.

hat_cascade = image.HaarCascade(“/cascadesc22.cascade”)



FPS clock

clock = time.clock()

while (True):

Capture snapshot

img = sensor.snapshot()

Find objects.

Note: Lower scale factor scales-down the image more and detects smaller objects.

Higher threshold results in a higher detection rate, with more false positives.

objects = img.find_features(hat_cascade, threshold=0.85, scale_factor=1.2)

Draw objects

for r in objects:


Print FPS.

Note: Actual FPS is higher, streaming the FB makes it slower.


I just get the line: width:36 height:36 n_stages:17 n_features:150 n_rectangles:260
And the red-LED turns on and I can’t do anything, not even disconnecting :confused:

What am I doing wrong?
attached the cascade-file

best reards (1.86 KB)

Hi, I’ve asked Ibrahim for help.

No this one doesn’t work, when I run the script my cam hangs, if you can attach the xml file for this cascade I’ll regenerate it and test again.


I attached the XML-file.
Hope this helps solving my problem :slight_smile:

best regards
cascade.xml (67.2 KB)


This one works (doesn’t crash the camera) it’s different from the one you sent before (cascadesc22.cascade) I’m attaching the binary cascade I made with the script (1.69 KB)

Hi all,

thanks for the replies!

I have another question:
I created the XML-file with openCV. In order to run it on my Laptop (using it’s webcam) I created the new version of the XML-file (without the parameter -SaveBaseFormat).
The result is very good an it detects a lot of hardhats.
Now, in order to run it on the openMV-Cam I need the old format of the XML-file (created by using the parameter -SaveBaseFormat). But then I use the script ( in order to convert it into a .cascade-file and it detects almost nothing. How ist this possible: I used the same parameters as I did for the new format. :confused:

Is this a common issue=?

best regards


Have you tired playing with the threshold and scale ratio?

Hi iabdalkader,
I tried to replace the eye detection cascade with a new one. I converted the .xml file to .cascade file using the provided tool. But the video stream stuck and I think the camera cracked . I read the thread above and bascially the problem is the same as Fabianhummel’s. You created a new binary cascade ( for him and it also works on my machine. I want to know how you did that. I also attached my xml file. It’ll be great help if you create a binary cascade for me.

Best regards, (25.4 KB)

Hi, it seems the cascade tool only works on Linux.

hi @iabdalkader
i want to create a cascade file from xml file
xml file is a opencv sample (haarcascade_fullbody)
i try to convert xml file to cscsde file whit but it return

usage: conver [-h] [-i] [-n NAME] [-s STAGES] [-c] file
conver: error: too few arguments

Traceback (most recent call last):
File “C:\Users\azaduni\Desktop\conver”, line 288, in
File “C:\Users\azaduni\Desktop\conver”, line 272, in main
args = parser.parse_args()
File “C:\Python27\lib\”, line 1656, in parse_args
args, argv = self.parse_known_args(args, namespace)
File “C:\Python27\lib\”, line 1688, in parse_known_args
return self._parse_known_args(args, namespace)
File “C:\Python27\lib\”, line 1901, in parse_known_args
(‘too few arguments’))
File “C:\Python27\lib\”, line 2311, in error
self.exit(2, _(‘%s: error: %s\n’) % (self.prog, message))
File “C:\Python27\lib\”, line 2299, in exit
SystemExit: 2

its my xml file
and its whit path chenged
haarcascade_fullbody.xml (466 KB) (10.1 KB)

I don’t know what is, please use our script:

./ haarcascade_fullbody.xml

Also Note it doesn’t work on Windows.

thank you for answer to my question
i use your script and now had this problem