はじめに
人気シリーズ、AIで「ねぎ」と「たまねぎ」を見極めよう!の最新記事です。以前の記事では、転移学習によりモデルを作成していましたが、Google Cloud Visionは、前回のAmazon Rekognitionと同じく、トレーニング済みのモデルでかなりの確度で画像認識をしてくれるようです。今回は、これを使って「ねぎ」を見きわめをしてみるぞ。
ちなみに、過去の記事はこちら。
- TensorFlowでネギを見分ける
- TensorFlow ねぎとたまねぎの識別 リベンジ
- Salesforce Einstein Visionでネギを見分ける
- Amazon Rekognitionでネギを見分ける
Google Cloud Vision
GoogleはCloud Visionという画像認識サービスを提供してます。これは、超巨大な検索エンジンをもつGoogleならではのデータ量で高精度なモデルを実現しているそうです。実際に利用したユーザからはかなりの精度で画像認識ができると高い評価を得ているようです。一説によるとAmazonRekognitionよりだいぶ良いとの話。これはわくわくしますね!
前提
CentOS7
アクセスキーの取得
Google Cloud Visionを使うにはAPI Keyが必要です。GCP(Google Cloud Platform)のコンソールでAPI Keyを取得します。
API&ServicesのCredentialsからCreate CredentialsのAPI Keyを選択してAPI Keyを生成、取得してください。
Google Cloud Visionで「ねぎ」「たまねぎ」判定
スクリプト
こんな感じのスクリプトで行けます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
from base64 import b64encode from sys import argv import json import requests import glob import argparse ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate' DELIM="\t" if __name__ == '__main__': parser = argparse.ArgumentParser(description="") parser.add_argument("apikey",help="directory which contain the iamges to be predicated",type=str) parser.add_argument("input",help="directory which contain the iamges to be predicated",type=str) args = parser.parse_args() img_requests = [] with open(args.input, 'rb') as source_image: ctxt = b64encode(source_image.read()).decode() img_requests.append({ 'image': {'content': ctxt}, 'features': [{ 'type': 'LABEL_DETECTION', 'maxResults': 5 }] }) response = requests.post(ENDPOINT_URL, data=json.dumps({"requests": img_requests}).encode(), params={'key': args.apikey}, headers={'Content-Type': 'application/json'}) print("{}".format(args.input)) for i,v in enumerate(response.json()['responses'][0]['labelAnnotations'][:3]): print("{},{}".format(v['description'],v['score'])) |
画像認識、識別
次の画像を使って画像を認識させてみましょう。
次のように画像認識を行います。
1 |
python3.6 GCV.py Axxxxxx tamanegi.jpg |
このレスポンスは下記の通りでした。わぉ!!!たまねぎ見きわめてますね!AmazonRekognitionができてなかったのであまり期待していなかったのですが、Googleおそるべし。
1 2 3 |
Onion,0.83809465 Vegetable,0.7511818 Plant,0.72040075 |
次は、この画像で試します。
おぉ!こちらも確率が落ちてはいますが、Leekが出てますね!
1 2 3 |
Vegetable,0.74428374 Plant,0.7165826 Leek,0.6507471 |
すごい。Amazonができてなかっただけにちょっと驚きです。
まとめ
今回は、「ねぎ」「たまねぎ」の見きわめをGoogle Cloud Visionでやってみました。結果は素晴らしいものでした。これはかなりの利用価値がありそうです。皆さんもいろいろと遊んでみてはいかがでしょうか。
コメント