LoginSignup
0
1

More than 3 years have passed since last update.

物体検出の例

Last updated at Posted at 2019-08-06

このobject_detection.pyの例は、指定された検出モデル、ラベルファイル、および画像を使用して、DetectionEngine APIでオブジェクト検出を実行します。 ラベルファイルが指定されていない場合、デフォルトで顔が検出されます(顔検出モデルを使用する必要があります)。

以下の例では、1,000種類のオブジェクトまたは人間の顔のいずれかを検出するようにトレーニングされたMobileNet SSDを使用しています。

開始する前に、開発ボードまたはUSBアクセラレータをセットアップしておく必要があります。

google-coral-ai-iot-products.jpg

ファイルを入手する

次の例に必要なファイルをダウンロードします:

EXAMPLE_DIR=$HOME/coral-examples

mkdir -p $EXAMPLE_DIR && cd $EXAMPLE_DIR

curl -O https://dl.google.com/coral/canned_models/mobilenet_ssd_v2_face_quant_postprocess_edgetpu.tflite \
-O https://dl.google.com/coral/canned_models/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite \
-O https://dl.google.com/coral/canned_models/coco_labels.txt \
-O https://coral.withgoogle.com/static/docs/images/cat.jpg \
-O https://coral.withgoogle.com/static/docs/images/face.jpg

オブジェクト検出を実行する

最初に、デモのあるディレクトリに移動します:

# If using the Dev Board:
cd /usr/lib/python3/dist-packages/edgetpu/demo

# If using the USB Accelerator with Debian/Ubuntu:
cd /usr/local/lib/python3.6/dist-packages/edgetpu/demo

# If using the USB Accelerator with Raspberry Pi:
cd /usr/local/lib/python3.5/dist-packages/edgetpu/demo

猫の写真を使用してobject_detection.pyを実行します:

python3 object_detection.py \
--model $EXAMPLE_DIR/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite \
--label $EXAMPLE_DIR/coco_labels.txt \
--input $EXAMPLE_DIR/cat.jpg \
--output $EXAMPLE_DIR/object_detection_results.jpg

cat.jpg

次のような結果が表示されます

-----------------------------------------
cat
score =  0.87890625
box =  [609.4696044921875, 260.6632471084595, 1023.9177703857422, 999.6587753295898]

デフォルトでは、より多くのオブジェクトが検出され、そのうちのいくつかは非常に低い信頼スコアを持っています。 これは、object_detection.pyスクリプトのtop_kの値が10に設定されているためです。この低い値を設定すると、上位1つまたはいくつかの結果のみを受け取ることができます。

また、スクリプトは、検出されたオブジェクトのボックスオーバーレイを使用して画像のコピーを作成し、-outputパラメーターで指定された場所に保存します。

object_detection_results.jpg
object_detection results.jpg

助けて! Raspberry Piを使用していて、No such file or directory: 'feh'というエラーが表示された場合は、sudo apt-get install fehを実行してから、もう一度試してください。

顔検出を実行する

デモがあるディレクトリにいることを確認してください:

# If using the Dev Board:
cd /usr/lib/python3/dist-packages/edgetpu/demo

# If using the USB Accelerator with Debian/Ubuntu:
cd /usr/local/lib/python3.6/dist-packages/edgetpu/demo

# If using the USB Accelerator with Raspberry Pi:
cd /usr/local/lib/python3.5/dist-packages/edgetpu/demo

顔付きの写真を使用して、顔検出モデル(およびラベルファイルなし)でobject_detection.pyを実行します:

python3 object_detection.py \
--model $EXAMPLE_DIR/mobilenet_ssd_v2_face_quant_postprocess_edgetpu.tflite \
--input $EXAMPLE_DIR/face.jpg \
--output $EXAMPLE_DIR/face_detection_results.jpg

face.jpg

次のような結果が表示されます:

-----------------------------------------
score =  0.9921875
box =  [[420.00265979766846, 49.222673773765564], [798.6797246932983, 354.40516090393066]]
-----------------------------------------
score =  0.9609375
box =  [[137.51397091150284, 110.30885183811188], [541.597005367279, 435.9664545059204]]
-----------------------------------------
score =  0.91796875
box =  [[834.5663707256317, 215.26478624343872], [1027.9360916614532, 374.5929465293884]]
-----------------------------------------
score =  0.83203125
box =  [[1.7380172908306122, 186.02276635169983], [157.47189247608185, 325.94583439826965]]

face_detection_results.jpg

独自のオブジェクト検出モデルを作成するには、オブジェクト検出モデルを再トレーニングする方法に関するチュートリアルをお読みください。

(Google Coral Edge TPU 全球代理店販売:https://store.gravitylink.com/global)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1