Help us understand the problem. What is going on with this article?

OpenCVのtraincascadeの使い方

More than 3 years have passed since last update.

オプション

Usage: opencv_traincascade
  -data <cascade_dir_name>
  -vec <vec_file_name>
  -bg <background_file_name>
  [-numPos <number_of_positive_samples = 2000>]
  [-numNeg <number_of_negative_samples = 1000>]
  [-numStages <number_of_stages = 20>]
  [-precalcValBufSize <precalculated_vals_buffer_size_in_Mb = 1024>]
  [-precalcIdxBufSize <precalculated_idxs_buffer_size_in_Mb = 1024>]
  [-baseFormatSave]
  [-acceptanceRatioBreakValue <value> = -1>]
--cascadeParams--
  [-stageType <BOOST(default)>]
  [-featureType <{HAAR(default), LBP, HOG}>]
  [-w <sampleWidth = 24>]
  [-h <sampleHeight = 24>]
--boostParams--
  [-bt <{DAB, RAB, LB, GAB(default)}>]
  [-minHitRate <min_hit_rate> = 0.995>]
  [-maxFalseAlarmRate <max_false_alarm_rate = 0.5>]
  [-weightTrimRate <weight_trim_rate = 0.95>]
  [-maxDepth <max_depth_of_weak_tree = 1>]
  [-maxWeakCount <max_weak_tree_count = 100>]
--haarFeatureParams--
  [-mode <BASIC(default) | CORE | ALL
--lbpFeatureParams--
--HOGFeatureParams--

vecファイルを入力として、haar, LBP, HOGなどの特徴を指定して学習データを出力する。訓練用の正例・負例が4など少ない状態で動かしたらエラーになったので数を増やしたら動いた・・・。正例1000、負例80、-numStages 1なら動くことを確認した。訓練時間は1分もかからなかった。正例7000、負例3000は必要らしい。

下記のようなエラーになるときは訓練データ数が足りていない。または-numStagesが大きすぎる。

===== TRAINING 1-stage =====
<BEGIN
POS curreOpenCV Error: Bad argument (Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.
) in get, file /Users/user/miniconda3/conda-bld/work/opencv-2.4.12/apps/traincascade/imagestorage.cpp, line 154
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/user/miniconda3/conda-bld/work/opencv-2.4.12/apps/traincascade/imagestorage.cpp:154: error: (-5) Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.

エラーなく終了すると、

-rw-r--r--  1 kt  staff  1170 Jan  4 05:44 cascade.xml
-rw-r--r--  1 kt  staff   594 Jan  4 05:44 params.xml
-rw-r--r--  1 kt  staff   384 Jan  4 05:44 stage0.xml

これらのファイルが出力される。caccade.xmlをサンプルで使っているxmlの代わりに使えば認識できるはず。要確認。

計算時間(-stageNum 1)

  • 24x24 正例1000, 負例500で1分20秒
  • 24x24 正例1000, 負例1000で2分12秒 (負例の数も影響する)
  • 24x24 正例2000, 負例1000で3分59秒 (正例の数は大きく影響する)

学習結果のキャッシュ

xml出力ファイルにstageファイルができていて、それを消さないと最学習はしない。-stageNumを増やしたとき、キャッシュがあるときはそれを使う。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away