OpenCV 5.0.0
Open Source Computer Vision
読み込み中...
検索中...
見つかりません
🤖 AIによる機械翻訳(非公式) — これは OpenCV 5.0.0 公式リファレンス(英語)を AI (Claude) で自動翻訳したものです。訳に誤りを含む場合があります。正確な情報は 公式英語版(原文) を参照してください。
CNNを用いた物体検出

ビルド

「dnn_objectect」モジュールのサンプルをビルドする。詳細はOpenCVのビルドチュートリアルを参照してほしい。CMakeオプション BUILD_EXAMPLES=ON を有効にし、これらのターゲットをビルドする(Linux):

  • example_dnn_objdetect_image_classification
  • example_dnn_objdetect_obj_detect

opencv_extra/dnn_objdetect から重みファイルとモデル定義ファイルをダウンロードする

物体検出

example_dnn_objdetect_obj_detect <model-definition-file> <model-weights-file> <test-image>

以下の例はすべて、Intel(R) Core(TM)2 i3-4005U CPU @ 1.70GHz(GPUなし)を搭載したノートパソコンで実行した。

このモデルは非常に高速で、複数のバウンディングボックスを予測するのに平均でわずか 0.172091 秒しかかからない。

<bin_path>/example_dnn_objdetect_obj_detect SqueezeDet_deploy.prototxt SqueezeDet.caffemodel tutorials/images/aeroplane.jpg
Total objects detected: 1 in 0.168792 seconds
------
Class: aeroplane
Probability: 0.845181
Co-ordinates: 41 116 415 254
------
Train_Dets
<bin_path>/example_dnn_objdetect_obj_detect SqueezeDet_deploy.prototxt SqueezeDet.caffemodel tutorials/images/bus.jpg
Total objects detected: 1 in 0.201276 seconds
------
Class: bus
Probability: 0.701829
Co-ordinates: 0 32 415 244
------
Train_Dets
<bin_path>/example_dnn_objdetect_obj_detect SqueezeDet_deploy.prototxt SqueezeDet.caffemodel tutorials/images/cat.jpg
Total objects detected: 1 in 0.190335 seconds
------
Class: cat
Probability: 0.703465
Co-ordinates: 34 0 381 282
------
Train_Dets
<bin_path>/example_dnn_objdetect_obj_detect SqueezeDet_deploy.prototxt SqueezeDet.caffemodel tutorials/images/persons_mutli.jpg
Total objects detected: 2 in 0.169152 seconds
------
Class: person
Probability: 0.737349
Co-ordinates: 160 67 313 363
------
Class: person
Probability: 0.720328
Co-ordinates: 187 198 222 323
------
Train_Dets

ぜひ他の画像でもモデルを実行してみてほしい。

しきい値の変更

デフォルトでは、このモデルは確信度 0.53 で検出結果をしきい値処理する。フィルタリングの際には多数のバウンディングボックスが予測されるが、省略可能な引数 threshold の値を渡すことで、何がしきい値処理されるかを手動で制御できる。例えば:

<bin_path>/example_dnn_objdetect_obj_detect <model-definition-file> <model-weights-file> <test-image> <threshold>

しきい値を例えば 0.0 に変更すると、以下のようになる:

Train_Dets

これはあまり役に立たないようだ。

画像分類

example_dnn_objdetect_image_classification <model-definition-file> <model-weights-file> <test-image>

モデルのサイズは4.9MBで、画像の分類にわずか0.136401秒しかかからない。

サンプルでモデルを実行すると、以下の結果が得られる:

<bin_path>/example_dnn_objdetect_image_classification SqueezeNet_deploy.prototxt SqueezeNet.caffemodel tutorials/images/aeroplane.jpg
Best class Index: 404
Time taken: 0.137722
Probability: 77.1757

synset_words.txt を見ると、予測されたクラスは airliner に属する

<bin_path>/example_dnn_objdetect_image_classification SqueezeNet_deploy.prototxt SqueezeNet.caffemodel tutorials/images/cat.jpg
Best class Index: 285
Time taken: 0.136401
Probability: 40.7111

これはクラス Egyptian cat に属する

<bin_path>/example_dnn_objdetect_image_classification SqueezeNet_deploy.prototxt SqueezeNet.caffemodel tutorials/images/space_shuttle.jpg
Best class Index: 812
Time taken: 0.137792
Probability: 15.8467

これはクラス space shuttle に属する