Last active
January 8, 2019 08:36
-
-
Save kyoro1/efc6971bd81967c11cf3885736be2538 to your computer and use it in GitHub Desktop.
Microsoft Azureのcomputer visionでカンタンな分析をしてみよう ref: https://qiita.com/kyoro1/items/fcef81e84da99bc52de9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import matplotlib.pyplot as plt | |
import json | |
from PIL import Image | |
from io import BytesIO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
response = requests.post(analyze_url, | |
headers=headers, | |
params=params, | |
data=image_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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