Skip to content

Instantly share code, notes, and snippets.

@kyoro1
Last active January 8, 2019 08:36
Show Gist options
  • Save kyoro1/efc6971bd81967c11cf3885736be2538 to your computer and use it in GitHub Desktop.
Save kyoro1/efc6971bd81967c11cf3885736be2538 to your computer and use it in GitHub Desktop.
Microsoft Azureのcomputer visionでカンタンな分析をしてみよう ref: https://qiita.com/kyoro1/items/fcef81e84da99bc52de9
import requests
import matplotlib.pyplot as plt
import json
from PIL import Image
from io import BytesIO
## 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)
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}
response = requests.post(analyze_url,
headers=headers,
params=params,
data=image_data)
img = Image.open(image_path)
plt.imshow(img)
caption = response.json()['description']['captions'][0]['text']
print(caption)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment