Microsoft Congnitive Services - Vision API in python
Microsoft cognitive services has a lot of services. Some of microsoft cognitive servies are as follows
- Vision
- Speech
- Language
- Knowledge
- Search
Vision
Image AnalysisThis feature returns information about visual content found in an image. Use tagging, domain-specific models, and descriptions in four languages to identify content and label it with confidence. Apply the adult/racy settings to help you detect potential adult content. Identify image types and color schemes in pictures.
To use this service an API key is required. We can be obtained from this URL. It offers a 7 day trail for free and then an Free Azure account is required to use this service.
https://azure.microsoft.com/en-us/try/cognitive-services/?api=computer-vision
After obtaining the API key we can write a script to pass an image and get different insights from that image. Here is complete python code for this purpose.
import urllib, json
import requests
# Path of image
img_filename = "D:/IMAGE.jpg"
#
with open(img_filename, 'rb') as f:
img_data = f.read()
# Replace the subscription_key string value with your valid subscription key.
subscription_key = 'YOUR_API_KEY'
## Request headers.
header = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key,
}
# Request parameters.
params = urllib.parse.urlencode({
'visualFeatures': 'Categories',
'details': 'Landmarks',
'language': 'en',
})
api_url = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze?%s"%params
r = requests.post(api_url,
params=params,
headers=header,
data=img_data)
print(r.json())
No comments