LoginSignup
43
29

More than 5 years have passed since last update.

Amazon Rekognitionで犬と唐揚げを見分けるアプリを作ってみた

Last updated at Posted at 2017-06-28

はじめに

1.「犬が唐揚げに見える」

というネタが2014年くらいに流行りました。
mig.jpeg

2.「Not Hotdog」の流行

2017年の5月くらいに「Not Hotdog」という、ファニーな画像解析アプリが流行りました。
Not Hotdog on the App Store - iTunes - Apple
元ネタ
not-hotdog.jpg

3.「Amazon Rekognition」の誕生

Amazon Rekognitionという、深層学習に基づく画像認識APIサービスが開始されました。
たとえばホットドッグの画像をアップロードすると、画像を解析し、画像内にホットドッグがあることを返してくれます。
スクリーンショット 2017-06-26 13.29.58.png

4.ということで

犬と唐揚げを見分けたい人のために、画像解析アプリを作ることにしました。

事前準備

・Identity Pool ID の作成

New Identity Poolにアクセスし、ウィザードに従ってIdentity Pool IDの作成を行います。
下記サイトに詳しい解説が載っているので、こちらを参考にしてください。
[Amazon Cognito] Cognito の導入方法がより簡単になりました! | Developers.IO

・IAMポリシーのアタッチ

作成したIDに、AmazonRekognitionFullAccessのポリシーをアタッチします。

Androidアプリの作成

1.ライブラリの追加

AmazonRekognitionのSDKをAndroidで使う場合、最低限これらのDependenciesが必要になります。

compile 'com.amazonaws:aws-android-sdk-core:2.4.4'
compile 'com.amazonaws:aws-android-sdk-rekognition:2.4.4'
compile 'com.amazonaws:aws-android-sdk-cognito:2.4.4'

2.AmazonRekognitionClientの作成

こんな感じで作ります。
事前準備で作成したIdentity Pool IDとRegionに適宜置き換えてください。

    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
            getContext(), // Context
            IDENTITY_POOL_ID, // Identity Pool ID
            Regions.US_EAST_1 // Region
    );
    amazonRekognitionClient = new AmazonRekognitionClient(credentialsProvider);

3.イメージの作成

この例ではBitmapをimageBytesに変換しています。
(もっとスマートなやり方があるかも)

        ByteBuffer imageBytes = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        imageBytes = ByteBuffer.wrap(baos.toByteArray());

4.APIリクエストの作成

※ withImageにはnew Image()を渡す必要があります。これで数時間ハマりました。
詳しい説明は下記Stack Overflowをご参照ください。
java - How can I DetectFaces in Amazon Rekognition AWS with Android Studio? - Stack Overflow

        DetectLabelsRequest request = new DetectLabelsRequest().withImage(new Image().withBytes(imageBytes)).withMaxLabels(10).withMinConfidence(77F);
        DetectLabelsResult result = amazonRekognitionClient.detectLabels(request);
        List<Label> labels = result.getLabels();

5.完成

犬と唐揚げを見分けるアプリが出来ました。
screenshot-3.png
screenshot-4.png
screenshot-5.png

6.おわり

AmazonRekognitionSDK for Androidの解説は以上です。

今回作成したデモアプリは下記からダウンロード可能です。
not dog - Google Play の Android アプリ

Githubでソースの全文を公開しているので、よろしければこちらもご参照ください。
Identity Pool IDの部分を置き換えることだけで、AmazonRekognitionを試すことが出来ます。
unoemon/notdog: Realtime dog or not dog classification!

43
29
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
43
29