Amazon Rekognitionを利用した、動画からのラベル抽出をやってみます。
(1) S3に解析用動画を準備する
動画からのラベル抽出をRekognitionをするには、S3に動画を保存する必要があります。短めの動画をS3にアップロードしておきます。前回作ったバケットに「video」というフォルダを作り、その中に動画をアップロードしておきます。動画はmp4かmovのようです。
(2)SagemakerでRekognition Video APIを利用前回作成したSagemakerのノートブックインスタンスを立て上げます。
今回の動画認識をするノートブックを作成しておきます。
(i) rekognitionインスタンスの作成
credentialファイルまたは直接アクセスキーを書いておきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
rekognition = boto3.client( | |
'rekognition', | |
aws_access_key_id='***************', | |
aws_secret_access_key='******************', | |
aws_session_token='**********************', | |
region_name='us-east-1' | |
) | |
bucketName = '****************' | |
videoName = "*******************" |
バケットや動画のファイル名は自分の環境に合わせておきます。
(ii) start_label_detection関数の呼び出し
画像と違い、動画の場合はstart_label_detection関数を最初に呼び出す必要があります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start video label recognition job | |
startLabelDetection = rekognition.start_label_detection( | |
Video={ | |
'S3Object': { | |
'Bucket': bucketName, | |
'Name': videoName, | |
} | |
}, | |
MinConfidence = 90 | |
) | |
labelsJobId = startLabelDetection['JobId'] | |
print("Job Id: {0}".format(labelsJobId)) |
Job IDを確認します。
次にラベル取得を行います。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getObjectDetection = rekognition.get_label_detection( | |
JobId=labelsJobId, | |
SortBy='TIMESTAMP' | |
) | |
while(getObjectDetection['JobStatus'] == 'IN_PROGRESS'): | |
time.sleep(5) | |
print('.', end='') | |
getObjectDetection = rekognition.get_label_detection( | |
JobId=labelsJobId, | |
SortBy='TIMESTAMP') | |
display(getObjectDetection['JobStatus']) |
SUCCEEDEDが出れば分析が完了です。
print(getObjectDetection) とするとjson形式で見ることができます。みやすくしてみます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for obj in getObjectDetection['Labels']: | |
print('Time : {0}'.format(obj['Timestamp'])) | |
print('LabelName : {0}'.format(obj['Label']['Name'])) | |
print('Confidence : {0}'.format(obj['Label']['Confidence'])) | |
print() |
0 件のコメント:
コメントを投稿