-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathloreal.py
More file actions
executable file
·24 lines (22 loc) · 1 KB
/
loreal.py
File metadata and controls
executable file
·24 lines (22 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import requests
import json
import os
import logging
def caption(image_path):
# Replace <Subscription Key> with your valid subscription key.
subscription_key = "6288ad9fa371475dad4c60fa1ae1933f"
assert subscription_key
vision_base_url = "https://eastus.api.cognitive.microsoft.com/vision/v2.0/"
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,Tags,Description,Color,ImageType'}
response = requests.post(
analyze_url, headers=headers, params=params, data=image_data)
response.raise_for_status()
# The 'analysis' object contains various fields that describe the image. The most
# relevant caption for the image is obtained from the 'description' property.
analysis = response.json()
caption = analysis['description']['captions'][0]['text']
return caption