python Amazon Rekognition detect_labelsその1
はじめに
Amazon Rekognitionを使用してみる。
Amazon Rekognitionは簡単に言えば画像分析サービス。
画像に有名人がいるかとか、その画像に含まれている要素は何なのかを判断してくれる
ほかにも、文字認識もできる。
今回は画像のラベル検出(detect_labels)を試してみる
準備
下記のプログラムを用意する。
- rekognition.py
import boto3 import sys import json if len(sys.argv) != 2: exit() client=boto3.client('rekognition') #写真を読み出して、Byte変換してdetect_labels送信 file = open(sys.argv[1],'rb') response = client.detect_labels(Image={'Bytes':file.read()}, MaxLabels=30) print(type(response)) print(json.dumps(response)) print('Detected labels for ' + sys.argv[1]) print() for label in response['Labels']: print ("ラベル: " + label['Name']) print ("信頼: " + str(label['Confidence'])) print ("**********インスタンス**********") for instance in label['Instances']: print (" 境界") print (" Top : " + str(instance['BoundingBox']['Top'])) print (" Left : " + str(instance['BoundingBox']['Left'])) print (" Width : " + str(instance['BoundingBox']['Width'])) print (" Height: " + str(instance['BoundingBox']['Height'])) print (" 信頼: " + str(instance['Confidence'])) print() print ("Parents:") for parent in label['Parents']: print (" " + parent['Name']) print ("********************************") print () label_count=len(response['Labels']) print("Labels detected: " + str(label_count))
実行結果
画像は以前使用したイーヌ
$ python rekognition.py 85898497.jpg ***レスポンス表示は中略*** Detected labels for 85898497.jpg ラベル: Mammal 信頼: 95.82337188720703 **********インスタンス********** Parents: Animal ******************************** ラベル: Animal 信頼: 95.82337188720703 **********インスタンス********** Parents: ******************************** ラベル: Wildlife 信頼: 62.20775604248047 **********インスタンス********** Parents: Animal ******************************** ラベル: Hedgehog 信頼: 58.174739837646484 **********インスタンス********** Parents: Mammal Animal ******************************** ラベル: Canine 信頼: 56.14072799682617 **********インスタンス********** Parents: Mammal Animal ******************************** Labels detected: 5
どこからどう見ても犬だし、ハリネヅミだけど100じゃないのが面白い。
まとめ
今回は手軽に引数の画像を解析するようにしてみたが、S3バケットの画像データからも分析できるので、その方法も試してみたい。