前提・実現したいこと
opencv(python)のtrain_cascadeを使用して1Dバーコードを検出させようと試みております。
将来的には2Dバーコードや印鑑を検出させたいと思っております。
positive画像とnegative画像を用意して試しましたが、学習がうまくいっているように思えません。
train_cascadeを使用して学習を行うのは初めてです。
皆様のご経験から下記質問にお答えいただきたいです。よろしくお願い致します。
質問
- そもそもバーコード検出にtrain_cascadeを使用するのは間違っていないか。
- 生成したpositive・negative画像に用いている画像は学習に向いているか。
- opencv_traincascade実行時に与えているオプションは正しいか。
ほか、ご意見ございましたらよろしくお願い致します。
やったこと
positive画像生成
pythonのpyBarcodeを使用してpositive画像(120, 60)を30000枚生成しました。
バーコード下に表示される数字列は削除しています。
negative画像生成
一方negative画像(120, 100)に関してはpositive画像に対してさまざまな画像処理を掛けて、
バーコード画像を崩したものを25000枚使用しました。
[negative画像を生成するために使用した画像処理]
画像処理に使用したライブラリはPillow(PIL)を使用しました。
なお、各画像処理は決められた確率で実行されます。
- ガウシアンブラー(80%の確率で実行)
- ごま塩ノイズ付加(90%の確率で実行)
- 黒白反転(50%の確率で実行)
- ポスタライズ(50%の確率で実行)
- ImageFilterのCONTOUR(50%の確率で実行)
createsamples実行
作成した画像のサイズのままだとメモリ不足のため、サイズを小さく指定しています。
bash
1opencv_createsamples -info positive.dat -vec positive.vec -num 30000 -w 40 -h 20
[実行ログ]
Info file name: positive.dat
Img file name: (NULL)
Vec file name: positive.vec
BG file name: (NULL)
Num: 30000
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: 40
Height: 20
Max Scale: -1
RNG Seed: 12345
Create training samples from images collection...
Done. Created 30000 samples
opencv_traincascade実行
bash
1opencv_traincascade -data cascade -vec positive.vec -bg negative.dat -numStages 1 -numPos 25000 -numNeg 20000 -w 40 -h 20 -featureType LBP
出来たcascade.xmlを用いて検証しましても、バーコードを検出してくれませんでした。
[cascade.xml]
<?xml version="1.0"?><opencv_storage>
<cascade>
<stageType>BOOST</stageType>
<featureType>LBP</featureType>
<height>20</height>
<width>40</width>
<stageParams>
<boostType>GAB</boostType>
<minHitRate>9.9500000476837158e-01</minHitRate>
<maxFalseAlarm>5.0000000000000000e-01</maxFalseAlarm>
<weightTrimRate>9.4999999999999996e-01</weightTrimRate>
<maxDepth>1</maxDepth>
<maxWeakCount>100</maxWeakCount></stageParams>
<featureParams>
<maxCatCount>256</maxCatCount>
<featSize>1</featSize></featureParams>
<stageNum>1</stageNum>
<stages>
<!-- stage 0 -->
<>
<maxWeakCount>2</maxWeakCount>
<stageThreshold>-6.5202511847019196e-02</stageThreshold>
<weakClassifiers>
<>
<internalNodes>
0 -1 0 -32769 -1 -33554433 -1 2147450879 2147221503 -1 -1</internalNodes>
<leafValues>
-9.8655378818511963e-01 9.4936311244964600e-01</leafValues></>
<>
<internalNodes>
0 -1 1 -1 -1 -1 -1 -1 -1 -1 -184614913</internalNodes>
<leafValues>
-9.7467207908630371e-01 9.2135125398635864e-01</leafValues></></weakClassifiers></></stages>
<features>
<>
<rect>
0 17 4 1</rect></>
<>
<rect>
28 0 4 1</rect></></features></cascade>
</opencv_storage>
numStagesを2以上にすると下記のようにエラーを吐くので、1に設定しました。
PARAMETERS:
cascadeDirName: cascade
vecFileName: positive.vec
bgFileName: negative.dat
numPos: 25000
numNeg: 20000
numStages: 2
precalcValBufSize[Mb] : 1024
precalcIdxBufSize[Mb] : 1024
acceptanceRatioBreakValue : -1
stageType: BOOST
featureType: LBP
sampleWidth: 40
sampleHeight: 20
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
Number of unique features given windowSize [40,20] : 16380===== TRAINING 0-stage =====
<BEGIN
POS count : consumed 25000 : 25000
NEG count : acceptanceRatio 20000 : 1
Precalculation time: 5
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 1|
+----+---------+---------+
| 2| 0.99892| 0.07745|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 1 minutes 46 seconds.===== TRAINING 1-stage =====
<BEGIN
POS count : consumed 25000 : 25027
NEG count : acceptanceRatio 0 : 0
Required leaf false alarm rate achieved. Branch training terminated.
環境
ubuntu 16.04
python 3.6.0
Pillow 4.0.0
opencv 3.4.2-dev
pyBarcode 0.7
あなたの回答
tips
プレビュー