LoginSignup
5
6

More than 3 years have passed since last update.

Amazon Rekognitionを使いPHPを通してレシピ画像を分析してみる

Last updated at Posted at 2019-06-14

Amazon Rekognitionとは

Amazon Rekognition機械学習の専門知識が無くとも、APIを通して画像分析、動画分析が可能なサービスです。

目標

Amazon Rekognitionを使いレシピ画像を分析してみる

  • ステップ1. レシピ画像を試しにいくつか分析してみる!
  • ステップ2. PHPを使い、Amazon S3上にあるレシピ画像を分析してみる

ステップ1 レシピ画像を試しにいくつか解析してみる!

awsアカウント作成後以下にアクセス
https://ap-northeast-1.console.aws.amazon.com/rekognition/home?region=ap-northeast-1#/label-detection

まず簡単そうなキーマカレーを解析してみました

screencapture-ap-northeast-1-console-aws-amazon-rekognition-home-2019-06-05-19_11_55.png

Foodが99.7% ほぼ食べ物であることは間違いないという結果。
Eggが99.7%。 
Cutleryが98.3% カトラリー。
Spoonが98.3% さらにスプーン。確率が結構高い。
Plantが91.9% 植物?
Burger 61.8% バーガー

もう一つ実験!海鮮トマト鍋!

screencapture-ap-northeast-1-console-aws-amazon-rekognition-home-2019-06-05-19_23_41.png

Bowl 98.3 % ボール。
Meal 98.1 % 食事。Foodとの違いはなんだろう
Food 98.1 % 食べ物。
Dish 98.1 % お皿。
Seafood 88.2 % シーフード。これはすごい
Sea Life 86.7 % 海洋生物?。エビ・カニを認識できたもよう
Animal 86.7 % 動物?
Seashell 70.6 % 貝殻。
Clam 70.6 % あさり!食材の特定までできた
Invertebrate 70.6 % 無脊椎動物.カニを認識した?
Soup Bowl 68.2 % スープボール。
Crawdad 59.3 % ロブスターに似た甲殻類
Stew 58.8 % シチュー。
Lobster 58.7 % ロブスター。まぁエビがいるから

どちらも、食べ物であることはほぼ確定できた。
ある程度までは食材の認識もできている。

左メニューに「画像の節度」というものがあることに気がつき、確認したところ、アダルトコンテンツかどうか確認できるとのこと。これも2つの料理写真を登録してみたが、もちろんセーフだった。

screencapture-ap-northeast-1-console-aws-amazon-rekognition-home-2019-06-05-19_37_37.png

ちなみにデモの、女性の水着写真でテストしてみたところ、
Suggestive 98.3 % いかがわしい
Female Swimwear Or Underwear 98.3% 水着、下着
という結果。

ステップ2. Amazon S3上にあるレシピ画像を分析してみる!

まず公式ドキュメントはこちら
https://docs.aws.amazon.com/ja_jp/rekognition/latest/dg/images-s3.html

アカウント設定など

AWS CLIや、AWS SDKなどがまだサーバーに入っていない方は、こちらを参考にセットアップしてください
https://docs.aws.amazon.com/ja_jp/rekognition/latest/dg/getting-started.html

まず、AWS CLIで試してみる

サーバーに接続し、コマンドプロンプトにて以下を実行

aws rekognition help

上記の応答があればOK

次に、S3サーバー上にある画像のタグを取得してみる

aws rekognition detect-labels --image "S3Object={Bucket=任意のS3のバケット名,Name=upload/save_image/00/00000003_recipe_07_3.jpg}" --region ap-northeast-1

LABELS  97.9916305542   Food
LABELS  97.9916305542   Burrito
LABELS  88.9598083496   Sandwich Wrap
LABELS  87.1909713745   Burger
LABELS  81.8015441895   Bread
LABELS  80.849319458    Meal
LABELS  77.7892456055   Dish
LABELS  57.3305091858   Lunch

無事にラベルが返ってきた
これでAWS CLIの確認まで完了。

続いてAWS SDKを通してS3上の画像を分析する

今回はAWS SDK for PHPを利用します

まず、aws-sdk-phpのインストール
homeディレクトリ配下に作成

mkdir ~/aws-test
cd ~/aws-test
curl -sS https://getcomposer.org/installer | php
php composer.phar require aws/aws-sdk-php

※番外:ここで、以下のエラーが出たので、swapを割当ててやる必要がある。

The following exception is caused by a lack of memory or swap, or not having swap configured
su -
/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1
free -m
             total       used       free     shared    buffers     cached
Mem:           533        257        275         16         14        106
-/+ buffers/cache:        136        396
Swap:         1023        147        876

Swapに割当られたので、これで再度以下を実行.

php composer.phar require aws/aws-sdk-php

無事成功したので、
テスト用のphpを作成する

vi ~/aws-test/aws-rekognition-test.php


<?php

require 'vendor/autoload.php';
use Aws\Rekognition\RekognitionClient;

// 画像URL指定
$file_name = 'https://s3-ap-northeast-1.amazonaws.com/任意のバケット名/upload/save_image/39/39008c3fa1d8.jpg';

$options = [
    'region'      => 'ap-northeast-1',
    'version'     => 'latest',
];

try {
    $rekognition = new RekognitionClient($options);
    // Call DetectFaces
    $result = $rekognition->detectLabels([
        'Image' => [
            'Bytes' => file_get_contents($file_name),
        ],
    ]);
} catch (Exception $e) {
    echo $e->getMessage();
}
var_dump($result["Labels"]);

実行結果

php aws-rekognition-test.php
array(11) {
  [0]=>
  array(4) {
    ["Name"]=>
    string(5) "Plant"
    ["Confidence"]=>
    float(95.830200195312)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["Name"]=>
    string(13) "Confectionery"
    ["Confidence"]=>
    float(87.443229675293)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(1) {
      [0]=>
      array(1) {
        ["Name"]=>
        string(4) "Food"
      }
    }
  }
  [2]=>
  array(4) {
    ["Name"]=>
    string(4) "Food"
    ["Confidence"]=>
    float(87.443229675293)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(0) {
    }
  }
  [3]=>
  array(4) {
    ["Name"]=>
    string(6) "Sweets"
    ["Confidence"]=>
    float(87.443229675293)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(1) {
      [0]=>
      array(1) {
        ["Name"]=>
        string(4) "Food"
      }
    }
  }
  [4]=>
  array(4) {
    ["Name"]=>
    string(6) "Flower"
    ["Confidence"]=>
    float(78.220794677734)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(1) {
      [0]=>
      array(1) {
        ["Name"]=>
        string(5) "Plant"
      }
    }
  }
  [5]=>
  array(4) {
    ["Name"]=>
    string(7) "Blossom"
    ["Confidence"]=>
    float(78.220794677734)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(1) {
      [0]=>
      array(1) {
        ["Name"]=>
        string(5) "Plant"
      }
    }
  }
  [6]=>
  array(4) {
    ["Name"]=>
    string(5) "Bread"
    ["Confidence"]=>
    float(67.359039306641)
    ["Instances"]=>
    array(1) {
      [0]=>
      array(2) {
        ["BoundingBox"]=>
        array(4) {
          ["Width"]=>
          float(0.96886533498764)
          ["Height"]=>
          float(0.7333869934082)
          ["Left"]=>
          float(0.029630890116096)
          ["Top"]=>
          float(0.2265917211771)
        }
        ["Confidence"]=>
        float(67.359039306641)
      }
    }
    ["Parents"]=>
    array(1) {
      [0]=>
      array(1) {
        ["Name"]=>
        string(4) "Food"
      }
    }
  }
  [7]=>
  array(4) {
    ["Name"]=>
    string(7) "Dessert"
    ["Confidence"]=>
    float(64.220893859863)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(1) {
      [0]=>
      array(1) {
        ["Name"]=>
        string(4) "Food"
      }
    }
  }
  [8]=>
  array(4) {
    ["Name"]=>
    string(7) "Pottery"
    ["Confidence"]=>
    float(62.139472961426)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(0) {
    }
  }
  [9]=>
  array(4) {
    ["Name"]=>
    string(5) "Petal"
    ["Confidence"]=>
    float(59.423400878906)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(2) {
      [0]=>
      array(1) {
        ["Name"]=>
        string(5) "Plant"
      }
      [1]=>
      array(1) {
        ["Name"]=>
        string(6) "Flower"
      }
    }
  }
  [10]=>
  array(4) {
    ["Name"]=>
    string(4) "Flax"
    ["Confidence"]=>
    float(55.462413787842)
    ["Instances"]=>
    array(0) {
    }
    ["Parents"]=>
    array(2) {
      [0]=>
      array(1) {
        ["Name"]=>
        string(5) "Plant"
      }
      [1]=>
      array(1) {
        ["Name"]=>
        string(6) "Flower"
      }
    }
  }
}

これでPHPからS3上の画像ファイルを読み込み、レシピ画像のラベルが取得できるようになった。

5
6
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
5
6