1
0

More than 3 years have passed since last update.

クラファンで手に入れた日本酒でAIをまなぶ

Last updated at Posted at 2021-03-26

最近受かったAzure AI Engineer Associate(Designing and Implementing an Azure AI Solution)のおさらいをPythonで。
ここではAzure Cognitive ServicesのComputer Visionを使って、日本酒の画像から銘柄を取り出してみる。Computer VisionはAzureが提供する画像やビデオのコンテンツを分析するAIサービス。

コーディングの前にComputer Visionを準備する

Azureにリソースを作成するのはAzure CLIでやりたいので、前もってインストール

# ブラウザでサインイン(MFAを有効にしてないとき)
# az login
# ブラウザでサインイン(MFAを有効にしてるとき)
# TODO: <TENANT_ID>はAzure Active Directoryで確認できるよ
az login --tenant <TENANT_ID>

# 最初に東日本リージョンに"rg-ai-wo-manabu"リソースグループを作成
$json_rg = az group create --location japaneast --name rg-ai-wo-manabu `
| ConvertFrom-Json
$json_rg

# Computer Visionリソースを"cv-nihonshu"という名前で作る
# 使うのは無料の価格プランF0
$json_cv = az cognitiveservices account create --name cv-nihonshu `
--resource-group $json_rg.name --kind ComputerVision --sku F0 `
--location $json_rg.location --yes | ConvertFrom-Json
$json_cv

# キーを確認(このあとPythonコードで使う)
$json_keys = az cognitiveservices account keys list --name $json_cv.name `
--resource-group $json_rg.name | ConvertFrom-Json
$json_keys.key1

画像からテキスト抽出(OCR)してみる

コーディング前にComputer Visionのライブラリをインストール。

pip install --upgrade azure-cognitiveservices-vision-computervision

Computer Visionに食わせる画像は、昨年の秋に初めて参加したクラウドファンディング『日本酒プロジェクト2020』で手に入れた嘉美心酒造さんの日本酒「神心(かみこころ)」(東京農大のおちょこ付き)。この画像を使って、Pythonで銘柄のテキストを抽出してみる。
神心 - 瓶囲い -

from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials


# Computer Visionリソースにアクセスするためのキーとエンドポイント用の変数
# TODO: subscription_keyの値を$json_keys.key1で表示されたキーに書き換える!!
subscription_key = '<$json_keys.key1で表示されたキー>'
endpoint = 'https://japaneast.api.cognitive.microsoft.com/'

# クライアントの認証
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

# 神心の画像アップ先を指定
image_url = 'https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/494429/b3e91162-c8d8-0ade-693c-4be0665a16ca.png'

# OCR機能で画像から銘柄を抽出したい
ocr = computervision_client.recognize_printed_text(image_url, language='ja')
for region in ocr.regions:
    for line in region.lines:
        chars = ""
        for word in line.words:
            chars += word.text + " "
        print(chars)

結果は?

おー、たった10行ちょっとのコードで、ラベルの下の方に印刷されてる『KAMIKOKORO』が拾えてる!!
さすがに漢字とかは拾えないのか。。。残念。
日本語がサポートされるようになったのが最近のこと(2021年2月9日)みたいだし、時間が経てば漢字も認識できるようになるのかな:thinking:
手書き文字の認識ができるらしいreadメソッドは、まだ日本語の指定ができなさそうだなぁ。そもそも縦書きはムリなのかな??
そういえば、recognize_printed_textでlanguage='ja'を指定しなかったら、結果が『KAMIKOKOKO』になった。

ふりかえり

ここ4か月くらいでAzureとかAWSの資格をいくつか取ったけど、Azure AI Engineer Associateの内容が一番おもしろい。クラウドのおかげでAIが簡単に使える時代になってるんですね。
酒蔵さんを応援するクラファンがあることを教えてもらって、クラファンに投資(?)したことなかったし、やってみるなら最近はまってる日本酒の酒蔵さんを応援できるのはいいなと思って入手した日本酒だったけど、AIをまなぶのに使うなんて思ってなかったなぁ。

あ、一応あとかたづけを。

# "rg-ai-wo-manabu"リソースグループを消す
az group delete --name $json_rg.name --yes
1
0
1

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
1
0