-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure_cv_image_sample.py
More file actions
90 lines (77 loc) · 3.66 KB
/
azure_cv_image_sample.py
File metadata and controls
90 lines (77 loc) · 3.66 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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials
from array import array
import os
from PIL import Image
import sys
import time
subscription_key = "ee60559e9bda4bd3b523f09ed23b0e5d"
endpoint = "https://cvfortest.cognitiveservices.azure.com/"
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
remote_image_url = "https://www.apple.com/newsroom/images/values/corporate/standard/Apple_google-partner-on-covid-19-contact-tracing-technology_04102020_inline.jpg.large.jpg"
'''
Describe an Image - remote
This example describes the contents of an image with the confidence score.
'''
print("===== Describe an image - remote =====")
# Call API
description_results = computervision_client.describe_image(remote_image_url )
# Get the captions (descriptions) from the response, with confidence level
print("Description of remote image: ")
if (len(description_results.captions) == 0):
print("No description detected.")
else:
for caption in description_results.captions:
print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100))
'''
Categorize an Image - remote
This example extracts (general) categories from a remote image with a confidence score.
'''
print("===== Categorize an image - remote =====")
# Select the visual feature(s) you want.
remote_image_features = ["categories"]
# Call API with URL and features
categorize_results_remote = computervision_client.analyze_image(remote_image_url , remote_image_features)
# Print results with confidence score
print("Categories from remote image: ")
if (len(categorize_results_remote.categories) == 0):
print("No categories detected.")
else:
for category in categorize_results_remote.categories:
print("'{}' with confidence {:.2f}%".format(category.name, category.score * 100))
'''
Tag an Image - remote
This example returns a tag (key word) for each thing in the image.
'''
print("===== Tag an image - remote =====")
# Call API with remote image
tags_result_remote = computervision_client.tag_image(remote_image_url )
# Print results with confidence score
print("Tags in the remote image: ")
if (len(tags_result_remote.tags) == 0):
print("No tags detected.")
else:
for tag in tags_result_remote.tags:
print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100))
'''
Detect Objects - remote
This example detects different kinds of objects with bounding boxes in a remote image.
'''
print("===== Detect Objects - remote =====")
# Get URL image with different objects
#remote_image_url_objects = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/objects.jpg"
#remote_image_url_objects= "file:///c://Documents/Photo/P_20191231_155438_vHDR_On.jpg"
remote_image_url_objects = "https://ichef.bbci.co.uk/news/976/cpsprodpb/747E/production/_119122892_charles_colliar.jpg"
# Call API with URL
detect_objects_results_remote = computervision_client.detect_objects(remote_image_url_objects)
# Print detected objects results with bounding boxes
print("Detecting objects in remote image:")
if len(detect_objects_results_remote.objects) == 0:
print("No objects detected.")
else:
for object in detect_objects_results_remote.objects:
print("object at location {}, {}, {}, {}".format( \
object.rectangle.x, object.rectangle.x + object.rectangle.w, \
object.rectangle.y, object.rectangle.y + object.rectangle.h))