Skip to content

Instantly share code, notes, and snippets.

@mahiya
Last active September 14, 2023 01:41
Show Gist options
  • Save mahiya/a7d235b73220082dbc5b49d94a1c53ae to your computer and use it in GitHub Desktop.
Save mahiya/a7d235b73220082dbc5b49d94a1c53ae to your computer and use it in GitHub Desktop.
Azure AI Vision - Vectorize Image API を使って画像をベクトル化する処理 (Python)
{
"endpoint": "https://xxxxx.cognitiveservices.azure.com/",
"key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
import json
import requests
# Azure AI Vision (Computer Vision) のアカウントのエンドポイントと認証キー情報を computer_vision_account.json から取得する
with open('computer_vision_account.json', 'r') as f:
account = json.load(f)
computer_vision_endpoint = account['endpoint']
computer_vision_key = account['key']
def image2vec(image_file_path: str):
""" 指定した画像ファイルのベクトル情報を取得して返す """
with open(image_file_path, 'rb') as f:
url = f'{computer_vision_endpoint}computervision/retrieval:vectorizeImage?api-version=2023-02-01-preview&modelVersion=latest'
headers = { 'Content-Type': 'application/octet-stream', 'Ocp-Apim-Subscription-Key': computer_vision_key }
resp = requests.post(url, data=f.read(), headers=headers)
return json.loads(resp.text)['vector']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment