implement own Haar-Cascade

Discussion related to "under the hood" OpenMV topics.
fabianhummel
Posts: 6
Joined: Thu Feb 02, 2017 10:56 am

implement own Haar-Cascade

Postby fabianhummel » Fri Feb 03, 2017 4:17 am

Hi,

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 (https://github.com/openmv/openmv/blob/m ... cascade.py) 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):
clock.tick()

# 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:
img.draw_rectangle(r)


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
User avatar
kwagyeman
Posts: 1407
Joined: Sun May 24, 2015 2:10 pm

Re: implement own Haar-Cascade

Postby kwagyeman » Fri Feb 03, 2017 10:55 am

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.
Nyamekye,
User avatar
kwagyeman
Posts: 1407
Joined: Sun May 24, 2015 2:10 pm

Re: implement own Haar-Cascade

Postby kwagyeman » Fri Feb 03, 2017 11:11 am

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

Code: Select all

print hat_cascade
Nyamekye,
User avatar
iabdalkader
Posts: 403
Joined: Sun May 24, 2015 3:53 pm

Re: implement own Haar-Cascade

Postby iabdalkader » Fri Feb 03, 2017 11:20 am

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.
fabianhummel
Posts: 6
Joined: Thu Feb 02, 2017 10:56 am

Re: implement own Haar-Cascade

Postby fabianhummel » Fri Feb 03, 2017 11:55 am

Hi :)

yes the print(hat_cascade) works. It prints values like:
20.13845
20.13423
20.13026
20.12651
20.12297
20.11963
...

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

Best regards
Attachments
cascade_scenario4.xml
(67.04 KiB) Downloaded 45 times
User avatar
kwagyeman
Posts: 1407
Joined: Sun May 24, 2015 2:10 pm

Re: implement own Haar-Cascade

Postby kwagyeman » Fri Feb 03, 2017 6:12 pm

Zip it up.
Nyamekye,
User avatar
iabdalkader
Posts: 403
Joined: Sun May 24, 2015 3:53 pm

Re: implement own Haar-Cascade

Postby iabdalkader » Fri Feb 03, 2017 7:41 pm

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

Image

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

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

Code: Select all

hat_cascade = image.HaarCascade("/cascade_scenario4.cascade")
Also used a 0.85 threshold and 1.15-1.25 scaling factor.
fabianhummel
Posts: 6
Joined: Thu Feb 02, 2017 10:56 am

Re: implement own Haar-Cascade

Postby fabianhummel » Mon Feb 06, 2017 3:20 am

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

best regards
Attachments
d-shop.zip
(10.15 KiB) Downloaded 60 times
fabianhummel
Posts: 6
Joined: Thu Feb 02, 2017 10:56 am

Re: implement own Haar-Cascade

Postby fabianhummel » Mon Feb 13, 2017 4:49 am

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.reset()

# Sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# HQVGA and GRAYSCALE are the best for face tracking.
sensor.set_framesize(sensor.HQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)

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")
print(hat_cascade)
# frontalface
# /cascade_scenario4.cascade
# FPS clock
clock = time.clock()

while (True):
clock.tick()

# 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:
img.draw_rectangle(r)

greenLed.on()
time.sleep(10)
greenLed.off()

# Print FPS.
# Note: Actual FPS is higher, streaming the FB makes it slower.
print(clock.fps())


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 :/

What am I doing wrong?
attached the cascade-file

best reards
Attachments
cascadesc22.zip
(1.86 KiB) Downloaded 38 times
User avatar
kwagyeman
Posts: 1407
Joined: Sun May 24, 2015 2:10 pm

Re: implement own Haar-Cascade

Postby kwagyeman » Mon Feb 13, 2017 11:43 am

Hi, I've asked Ibrahim for help.
Nyamekye,
User avatar
iabdalkader
Posts: 403
Joined: Sun May 24, 2015 3:53 pm

Re: implement own Haar-Cascade

Postby iabdalkader » Mon Feb 13, 2017 5:48 pm

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.
fabianhummel
Posts: 6
Joined: Thu Feb 02, 2017 10:56 am

Re: implement own Haar-Cascade

Postby fabianhummel » Tue Feb 14, 2017 4:23 am

Hi,

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

best regards
Attachments
cascade.xml
(67.2 KiB) Downloaded 34 times
User avatar
iabdalkader
Posts: 403
Joined: Sun May 24, 2015 3:53 pm

Re: implement own Haar-Cascade

Postby iabdalkader » Wed Feb 15, 2017 11:00 am

fabianhummel wrote:
Tue Feb 14, 2017 4:23 am
Hi,

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

best regards
Hi,

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
Attachments
cascade.zip
(1.69 KiB) Downloaded 47 times
fabianhummel
Posts: 6
Joined: Thu Feb 02, 2017 10:56 am

Re: implement own Haar-Cascade

Postby fabianhummel » Thu Feb 16, 2017 8:48 am

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 (https://github.com/openmv/openmv/blob/m ... cascade.py) 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. :/

Is this a common issue=?

best regards

Fabian
User avatar
kwagyeman
Posts: 1407
Joined: Sun May 24, 2015 2:10 pm

Re: implement own Haar-Cascade

Postby kwagyeman » Thu Feb 16, 2017 3:33 pm

Have you tired playing with the threshold and scale ratio?
Nyamekye,
Charlie
Posts: 1
Joined: Tue Jan 16, 2018 6:43 am

Re: implement own Haar-Cascade

Postby Charlie » Tue Jan 16, 2018 7:05 am

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 (cascade.zip) 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,
Attachments
eye_det.zip
(25.42 KiB) Downloaded 5 times
User avatar
iabdalkader
Posts: 403
Joined: Sun May 24, 2015 3:53 pm

Re: implement own Haar-Cascade

Postby iabdalkader » Tue Jan 16, 2018 2:44 pm

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

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest