LoginSignup
3
2

More than 3 years have passed since last update.

Microsoft Azureのcomputer visionでカンタンな分析をしてみよう

Last updated at Posted at 2018-12-26

お仕事でちょっと下調べしたのがあったので、折角なのでqiitaで共有

0. 環境

  • python 3.7 (Anacondaをpre-install)にて動作確認

1. 必要なlibrary設定

import requests
import matplotlib.pyplot as plt
import json
from PIL import Image
from io import BytesIO

2. Subscriptionなどの設定

## Setting for cloud side
SUBSCRIPTION_KEY = ## Subscription keyの指定
vision_base_url = "https://japaneast.api.cognitive.microsoft.com/vision/v2.0/" ## 利用regionにより適宜変更して下さい

## Setting for local images
WKDIR = ## 画像のディレクトリを指定
IMG_FILE = ## 画像ファイル名(jpg, png)

3. API用の設定

image_path = WKDIR + IMG_FILE
analyze_url = vision_base_url + "analyze"

image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY,
          'Content-Type': 'application/octet-stream'}
params  = {'visualFeatures': 'Categories,Description,Color'
          ,'language': 'ja'} ## 日本語なら'ja'
data = {'byte': image_data}

4. APIをcallしてみよう

response = requests.post(analyze_url, 
                         headers=headers, 
                         params=params, 
                         data=image_data)

5. 結果を確認しよう

img = Image.open(image_path)
plt.imshow(img)
caption = response.json()['description']['captions'][0]['text']
print(caption)

皿の上の料理とワイン
image.png

実は、ワインじゃなくビールなんですが、ワイングラスに入ったビールなので「ワイン」、、いや違う(笑)

Reference

3
2
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
3
2