AWS Rekognition with Scala

# AWS Rekognition
https://aws.amazon.com/jp/rekognition/

# Sample
https://docs.aws.amazon.com/rekognition/latest/dg/get-started-exercise.html

# やったこと
- IAMユーザー作成
- ~/.aws/credentialsにAPI KeyとAPI Secret Key情報の追加

# 雑に遊んでみる

```scala:build.sbt
name := "look-alike"

version := "0.1"

scalaVersion := "2.12.4"

libraryDependencies ++= Seq(
  "com.amazonaws" % "aws-java-sdk" % "1.11.274"
)
```

```scala:Main.scala
package fscoward.look_alike

import java.awt.image.BufferedImage
import java.io.{File, FileInputStream}
import java.nio.ByteBuffer
import javax.imageio.ImageIO

import com.amazonaws.auth.{AWSCredentials, AWSStaticCredentialsProvider}
import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.regions.Regions
import com.amazonaws.services.rekognition.model.{DetectLabelsRequest, Image, RecognizeCelebritiesRequest}
import com.amazonaws.services.rekognition.{AmazonRekognition, AmazonRekognitionClientBuilder}
import com.amazonaws.util.IOUtils

import scala.collection.JavaConverters._

object Main {
  def main(args: Array[String]): Unit = {

    val photo = "photo.jpeg"
    val credentials: AWSCredentials = new ProfileCredentialsProvider("rekognition").getCredentials()
    val rekognitionClient: AmazonRekognition = AmazonRekognitionClientBuilder
      .standard()
      .withRegion(Regions.US_WEST_2)
      .withCredentials(new AWSStaticCredentialsProvider(credentials))
      .build()

    val inputStream = new FileInputStream(new File(photo))
    val imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream))
    val image = new Image().withBytes(imageBytes)

    detectLabels(photo, rekognitionClient, image)
    recognizeCelebrities(photo, rekognitionClient, image)
  }

  private def recognizeCelebrities(photo: String, rekognitionClient: AmazonRekognition, image: Image) = {
    val recognizeCelebritiesRequest = new RecognizeCelebritiesRequest().withImage(image)
    val recognizeCelebritiesResult = rekognitionClient.recognizeCelebrities(recognizeCelebritiesRequest)
    val list = recognizeCelebritiesResult.getCelebrityFaces.asScala
    println(s"Recognize Celebrities for ${photo}")
    list.foreach(println)
  }

  private def detectLabels(photo: String, rekognitionClient: AmazonRekognition, image: Image) = {
    val request = new DetectLabelsRequest().withImage(image)
    val result = rekognitionClient.detectLabels(request)
    val labels = result.getLabels
    println("Detected labels for " + photo)
    labels.asScala.foreach(label => println(s"${label.getName}: ${label.getConfidence.toString}"))
  }

}

```

# 結果

```shell
sbt:look-alike> run
[info] Compiling 1 Scala source to /Users/fscoward/Documents/IdeaProjects/look-alike/target/scala-2.12/classes ...
[info] Done compiling.
[info] Packaging /Users/fscoward/Documents/IdeaProjects/look-alike/target/scala-2.12/look-alike_2.12-0.1.jar ...
[info] Done packaging.
[info] Running fscoward.look_alike.Main
Detected labels for photo.jpeg
Human: 99.26916
People: 99.26914
Person: 99.26916
Female: 66.76838
Girl: 66.76838
Woman: 66.76838
Finger: 54.91195
Leisure Activities: 52.50578
Face: 52.316517
Recognize Celebrities for photo.jpeg
{Urls: [www.imdb.com/name/nm3802115],Name: Rena Matsui,Id: OE8ky3d,Face: {BoundingBox: {Width: 0.3478261,Height: 0.4864865,Left: 0.17149758,Top: 0.0731982},Confidence: 99.836006,Landmarks: [{Type: eyeLeft,X: 0.27689487,Y: 0.29615954}, {Type: eyeRight,X: 0.39640635,Y: 0.24933314}, {Type: nose,X: 0.37854448,Y: 0.37207562}, {Type: mouthLeft,X: 0.33323634,Y: 0.4615591}, {Type: mouthRight,X: 0.40887538,Y: 0.41892907}],Pose: {Roll: -16.735506,Yaw: 11.267441,Pitch: -13.339288},Quality: {Brightness: 51.041508,Sharpness: 99.998024}},MatchConfidence: 94.0}
{Urls: [www.imdb.com/name/nm5634166],Name: Matsumura Sayuri,Id: 19iD8C,Face: {BoundingBox: {Width: 0.33494365,Height: 0.46846846,Left: 0.5120773,Top: -0.0045045046},Confidence: 99.92121,Landmarks: [{Type: eyeLeft,X: 0.59283286,Y: 0.24800687}, {Type: eyeRight,X: 0.70564073,Y: 0.14843765}, {Type: nose,X: 0.6766601,Y: 0.30344892}, {Type: mouthLeft,X: 0.6670941,Y: 0.37145668}, {Type: mouthRight,X: 0.7607751,Y: 0.30522147}],Pose: {Roll: -33.262516,Yaw: -12.167916,Pitch: -13.668684},Quality: {Brightness: 51.08542,Sharpness: 99.99453}},MatchConfidence: 100.0}
[success] Total time: 9 s, completed 2018/02/10 23:39:57
```

## 感想
たったこれだけで、写真に写っているのが誰かを認識できるの凄い・・・
OpenCVのインストールして・・・っていう苦労がない

なお、使った画像は白石麻衣と松村沙友理の写った写真ですが、松井玲奈と間違えて認識されています。。。自分で学習させよう。