-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagging-azure.py
60 lines (47 loc) · 2.27 KB
/
tagging-azure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
##python3 -m pip install --upgrade azure-cognitiveservices-vision-computervision
#python3 -m pip install --upgrade pillow
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes, Details
from msrest.authentication import CognitiveServicesCredentials
import os
import sys
import time
subscription_key = "api_key"
endpoint = "https://machine.cognitiveservices.azure.com/"
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
'''
Categorize an Image - local
This example extracts categories from a local image with a confidence score
'''
print("===== Categorize an Image - local =====")
# Open local image file
local_image = open('/home/ubuntu/pics-83num-resized-half-6mb-max130kb/pic_1.jpg', "rb")
# Select visual feature type(s)
local_image_features = ["categories"]
local_image_features = [VisualFeatureTypes.categories,VisualFeatureTypes.brands,VisualFeatureTypes.adult,VisualFeatureTypes.color,VisualFeatureTypes.description,VisualFeatureTypes.faces,VisualFeatureTypes.image_type,VisualFeatureTypes.objects,VisualFeatureTypes.tags]
# Call API
categorize_results_local = computervision_client.analyze_image_in_stream(local_image, local_image_features)
# Print category results with confidence score
print("Categories from local image: ")
if (len(categorize_results_local.categories) == 0):
print("No categories detected.")
else:
for category in categorize_results_local.categories:
print("'{}' with confidence {:.2f}%".format(category.name, category.score * 100))
print()
# Return tags
# Print results with confidence score
print("Tags in the remote image: ")
if (len(categorize_results_local.tags) == 0):
print("No tags detected.")
else:
for tag in categorize_results_local.tags:
print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100))
print(dir(categorize_results_local))
print('xxxxx')
print(dir(categorize_results_local.tags))
print(categorize_results_local.tags[0])
print(len(categorize_results_local.tags))
print(categorize_results_local.tags[0].name)
print(type(categorize_results_local.tags[0].confidence))