opencv_traincascade too quick with bad results

asked 2017-11-05 10:45:26 -0500

Hello, I'm trying to make an app in python which basically counts the amount of money in the picture or camera feed.

I assumed that Haar cascade detection is the right one for this job, so I followed this tutorial: https://pythonprogramming.net/haar-ca...

So I downloaded about 2500 negative images showing tables, I took the image he used (50x50): image description, scaled every negative to 100x100 and after execution of these 3 commands:

opencv_createsamples.exe -img .\obverse.jpg -bg .\bg.txt -info .\info\info.lst -pngoutput .\info\ -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 2500

opencv_createsamples.exe -info .\info\info.lst -num 2500 -w 20 -h 10 -vec positives.vec

opencv_traincascade.exe -data .\data\ -vec .\positives.vec -bg .\bg.txt -numPos 2000 -numNeg 1000 -numStages 10 -w 20 -h 10

I seem to be getting good results (didn't actually wait till stage 10, but it generates some good results (seems to me)).

However, after swapping the watch to this image of a polish banknote:

image description,

resizing it from 909x403 to 50x25, resizing negatives to 100x50 and running previous 3 commands, the training lasts 1 minute until stage 8, where it achieves false alarm rate:

opencv_traincascade.exe -data .\data\ -vec .\positives.vec -bg .\bg.txt -numPos 2000 -numNeg 1000 -numStag
es 10 -w 20 -h 10
PARAMETERS:
cascadeDirName: .\data\
vecFileName: .\positives.vec
bgFileName: .\bg.txt
numPos: 2000
numNeg: 1000
numStages: 10
precalcValBufSize[Mb] : 1024
precalcIdxBufSize[Mb] : 1024
acceptanceRatioBreakValue : -1
stageType: BOOST
featureType: HAAR
sampleWidth: 20
sampleHeight: 10
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC
Number of unique features given windowSize [20,10] : 19865

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   2000 : 2000
NEG count : acceptanceRatio    1000 : 1
Precalculation time: 1.158
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|    0.999|    0.312|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 2 seconds.

===== TRAINING 1-stage =====
<BEGIN
POS count : consumed   2000 : 2002
NEG count : acceptanceRatio    1000 : 0.323311
Precalculation time: 1.197
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|   0.9965|    0.624|
+----+---------+---------+
|   4|   0.9955|    0.397|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 6 seconds.

===== TRAINING 2-stage =====
<BEGIN
POS count : consumed   2000 : 2011
NEG count : acceptanceRatio    1000 : 0.115727
Precalculation time: 1.268
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|    0.741|
+----+---------+---------+
|   4|   0.9995|    0.546|
+----+---------+---------+
|   5|   0.9955|    0.378|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 11 seconds.

===== TRAINING 3-stage =====
<BEGIN
POS count : consumed   2000 : 2020
NEG count : acceptanceRatio    1000 : 0.0514801
Precalculation time: 1.249
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|   0.9995|    0.845|
+----+---------+---------+
|   4|   0.9965|    0.661|
+----+---------+---------+
|   5|    0.996|    0.498|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 15 seconds.

===== TRAINING 4-stage =====
<BEGIN
POS count : consumed   2000 : 2028
NEG count : acceptanceRatio    1000 : 0.0274937
Precalculation time: 1.277
+----+---------+---------+
|  N |    HR   |    FA ...
(more)
edit retag flag offensive close merge delete

Comments

1

anyone,who read that blog post, came here later, complaining , that receipe would not work.

you're #37

in short - you cannot do with a single positive image, but need a ton of them.

also: https://docs.opencv.org/master/dc/d88...

berak gravatar imageberak ( 2017-11-05 10:47:51 -0500 )edit

Yep, but what if I want to differentiate between similar banknotes and coins?

jakub.pietrzak96 gravatar imagejakub.pietrzak96 ( 2017-11-05 11:00:27 -0500 )edit

impossible. it can only detect a single object class.

berak gravatar imageberak ( 2017-11-05 11:13:34 -0500 )edit

Should I consider abandoning this method for my purposes then? For digits detection for example? Or maybe shape detection?

jakub.pietrzak96 gravatar imagejakub.pietrzak96 ( 2017-11-05 11:18:42 -0500 )edit

Your thinking is just wrong, you first need 1 cascade classifier to detect banknotes, another to detect coins. Once you detect them, you need a subsequent classifier on top of the bounding box telling you which value the note or coin has. I think you are mixing up concepts of machine learning here. Read a bit about the difference of detection/localization and classification/labelling.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-11-06 03:17:27 -0500 )edit