はじめに
Amazon Rekognition を教科書通りに使ってみた
EC2 インスタンスからS3 の画像をAmazon Rekognition へ投入して、分析結果を出力する。
環境
Windows 10
AWS アカウント
EC2 インスタンス
S3
ReKognition
EC2 をセットアップする
aws configure の設定
aws configure --profile adminuser
上のコマンドを実行して、対話形式で、キー情報を入力
S3 に画像をアップロード
下のサイトを参考にどうぞ
http://dev.classmethod.jp/cloud/aws/cm-advent-calendar-2015-aws-re-entering-s3/
java プログラムの作成
サンプルプログラムの作成
vi DetectLabelsExample.java
Qiita をうまく使いこなせておらずきたないですが、、、
下のサンプルコードをどうぞ
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.rekognition.model.AmazonRekognitionException;
import com.amazonaws.services.rekognition.model.DetectLabelsRequest;
import com.amazonaws.services.rekognition.model.DetectLabelsResult;
import com.amazonaws.services.rekognition.model.Image;
import com.amazonaws.services.rekognition.model.Label;
import com.amazonaws.services.rekognition.model.S3Object;
import java.util.List;
public class DetectLabelsExample {
public static void main(String[] args) throws Exception {
String photo = "YOUR Photo.jpg";
String bucket = "YOUR bucket NAME";
AWSCredentials credentials;
try {
credentials = new ProfileCredentialsProvider("adminuser").getCredentials();
} catch(Exception e) {
throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
+ "Please make sure that your credentials file is at the correct "
+ "location (/Users/userid/.aws/credentials), and is in a valid format.", e);
}
AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder
.standard()
.withRegion(Regions.US_WEST_2)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
DetectLabelsRequest request = new DetectLabelsRequest()
.withImage(new Image()
.withS3Object(new S3Object()
.withName(photo).withBucket(bucket)))
.withMaxLabels()
.withMinConfidence(77F);
try {
DetectLabelsResult result = rekognitionClient.detectLabels(request);
List labels = result.getLabels();
System.out.println("Detectedls for " + photo);
for (Label label: labels) {
System.out.println(label.getName() + ": " + label.getConfidence().toString());
}
} catch(AmazonRekognitionException e) {
e.printStackTrace();
}
}
javac DetectLabelsExample.java
下のような結果が出ます。
$ java DetectLabelsExample
Detected labels for photo.jpg
Human: 99.30579
People: 99.30792
Person: 99.30792
Bicycle: 99.07324
Bike: 99.07324
Mountain Bike: 99.07324
Vehicle: 99.07324