Giter Club home page Giter Club logo

rekognition-batch's Introduction

AWSでサーバレスな動画解析(Rekognition)

こんにちは。 日々の業務ではインフラエンジニアをやっています。@hayaosatoです。

皆様は最近は動画解析などの技術も流行っていてそれらのSaaSも提供されていますね。 AWSではAmazon Rekognition(Rekognition)というサービスが提供されています。 本記事ではこのRekognitionを使うためのサーバレスアーキテクチャを構築してみたいと思います。

構成

構成図は以下の通りです。

Rekognitionで動画解析を行うと、動画の長さによりますが解析時間がかかってしまいます。 Rekognitionでは解析時間の完了をAmazon SNS(以下、SNS)のトピックを発行することで通知してくれます。 今回はS3に動画がアップロードされたことをトリガにAWS Lambda(以下、Lambda)からRekognitonの動画解析開始 Rekognitionの動画解析完了後SNSトピックからLambdaを呼び出し、LambdaからRekognitonの解析結果を取得する というアーキテクチャを作ってみます。

S3 -> Lambda

Lambdaファンクションを作成し、トリガを設定します。 この際に注意すべきは動画はファイルサイズが大きくなりがちなので、 マルチパートアップロードの完了時もちゃんとトリガが飛ぶようにしておきましょう。

Lambda -> Rekognition -> SNS

Rekognitionで解析を開始するためのLambdaファンクションを作成します。 言語はPythonにしました。ドキュメントはこちら。 今回は顔認識を使ってみることにしたので、start_face_detectionを使用します。

response = client.start_face_detection(
    Video={
        'S3Object': {
            'Bucket': 'your-bucket-name',
            'Name': 'your-video-name'
        }
    },
    ClientRequestToken='string',
    NotificationChannel={
        'SNSTopicArn': 'your-sns-topic-arn',
        'RoleArn': 'your-role-arn-for-rekognition'
    }
)

ここで、NotificationChannelにSNSとROLEのARNを指定しています。 SNSTopicArnはその通りRekognitionが解析完了した際に通知を飛ばすためのSNSトピックのARNです。 RoleArnはRekognitionがSNSトピックを飛ばすためのRoleを与える必要があります。

SNS -> Lambda -> Rekognition

Rekognitionから発行されたSNSトピックからLambdaが呼び出されるようにサブスクリプションを作成したら、 最後にLambdaからRekognitionに対して動画解析の結果を取得します。

Lambdaに届くeventの'Sns'キーのValueは以下のようになっています。

{
    'Type': 'Notification',
    'MessageId': 'xxxxxxxx',
    'TopicArn': 'arn:aws:sns:your-region:your-account-id:your-topic-arn',
    'Subject': None,
    'Message': '{"JobId":"xxxxxxxxxx","Status":"SUCCEEDED","API":"APINAME","Timestamp":1571799891913,"Video":{"S3ObjectName":"your-video.mp4","S3Bucket":"your-video-bucket"}}',
    'Timestamp': '2019-10-23T03:04:52.660Z',
    'SignatureVersion': '1',
    'Signature': 'xxxxxxxxx'
    'SigningCertUrl': 'https://sns.ap-northeast-1.amazonaws.com/SimpleNotificationService-xxxx.pem',
    'UnsubscribeUrl': 'https://sns.ap-northeast-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:xxxxxx',
    'MessageAttributes': {}
}

このようにして、Messageを取り出すことでRekognitionから結果を抜き出すための情報が揃います。 MessageからJobIdを抜き出して、

(NextTokenは省略しています。)

response = client.get_face_detection(
    JobId='xxxxxx'
)

Rekognitionから解析結果のJSONを受け取りことができます。

まとめ

このようにして、Rekognitionを利用してサーバレスなアーキテクチャを作成してみました。 サーバレスでイベントドリブンってやっぱりいいですね

rekognition-batch's People

Contributors

hayaosato avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.