2020年11月19日 星期四

opencv training images for object detection memo

官方說明網址 https://docs.opencv.org/3.4/dc/d88/tutorial_traincascade.html

OpenCV使用版本: opencv-3.4.12-vc14_vc15

#這是註解

#資料夾結構

cascade_opencv_train --- pos    #正面影像檔

                                     ---neg    #負面影像檔

#產生neg.txt正面影像檔清單, pos.txt負面影像檔清單

dir /b neg>neg.txt    #部分內容如下

pos\WIN_20201026_13_48_02_Pro.jpg

pos\WIN_20201026_13_48_16_Pro.jpg


dir /b pos>pos.txt    #部分內容如下

neg\frame_100.jpg

neg\frame_1000.jpg

neg\frame_1001.jpg


#使用內建的程式,標註之後要尋找之物件的教學圖案

opencv_annotation.exe --annotations=annotations.txt --images=pos\     #部分內容如下

pos\WIN_20201026_13_48_02_Pro.jpg 1 238 405 135 130

pos\WIN_20201026_13_48_16_Pro.jpg 1 161 346 152 153


#從標註的圖案清單建立sample.vec 檔案

opencv_createsamples -info annotations.txt -vec samples.vec        #下面是我的結果

Info file name: annotations.txt

Img file name: (NULL)

Vec file name: samples.vec

BG  file name: (NULL)

Num: 1000

BG color: 0

BG threshold: 80

Invert: FALSE

Max intensity deviation: 40

Max x angle: 1.1

Max y angle: 1.1

Max z angle: 0.5

Show samples: FALSE

Width: 24

Height: 24

Max Scale: -1

RNG Seed: 12345

Create training samples from images collection...

annotations.txt(171) : parse errorDone. Created 170 samples

#開始訓練,注意,預設的寬與高是24x24,我的正向照片為170個,負面照片為4614個。

opencv_traincascade -data . -vec samples.vec -bg neg.txt -numStages 8 -minHitRate 0.995 -maxFalseAlarmRate 0.3 -featureType LBP -numPos 170 -numNeg 4614

PARAMETERS:
cascadeDirName: .
vecFileName: samples.vec
bgFileName: neg.txt
numPos: 170
numNeg: 4614
numStages: 8
precalcValBufSize[Mb] : 1024
precalcIdxBufSize[Mb] : 1024
acceptanceRatioBreakValue : -1
stageType: BOOST
featureType: LBP
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.3
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
Number of unique features given windowSize [24,24] : 8464

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 1
Precalculation time: 0.914
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0247074|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 1 seconds.

===== TRAINING 1-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 0.024649
Precalculation time: 1.021
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0264413|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 3 seconds.

===== TRAINING 2-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 0.0021845
Precalculation time: 0.979
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0383615|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 26 seconds.

===== TRAINING 3-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 0.00032392
Precalculation time: 0.91
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0448635|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 59 seconds.

===== TRAINING 4-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    0 : 0
Required leaf false alarm rate achieved. Branch training terminated.

#訓練之後就會產生cascade.xml的檔案,可以從下面的連結1下載demo程式去偵測看看。

#最後

#參考網頁,一開始建議先從基本的開始做,之後再看1.的連結,會比較懂。別像我花了一堆時間train,結果都不理想。後來才從2.的連結與官網才成功。

1. https://chtseng.wordpress.com/2020/02/07/%E5%A6%82%E4%BD%95%E8%A8%93%E7%B7%B4%E4%B8%80%E5%80%8B%E8%B2%93%E8%87%89%E5%81%B5%E6%B8%AC%E5%99%A8/

2. https://johnallen.github.io/opencv-object-detection-tutorial/