Repository navigation
Expand file tree
/
Copy pathimage_analysis.py
More file actions
189 lines (158 loc) · 8.6 KB
/
Copy pathimage_analysis.py
File metadata and controls
189 lines (158 loc) · 8.6 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import matplotlib.pyplot as plt
from io import BytesIO
import requests
from PIL import Image, ImageDraw
import streamlit as st
from st_clickable_images import clickable_images
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials
class App:
def __init__(self):
if "cog_endpoint" not in st.session_state:
st.session_state.update({
"cog_endpoint": os.environ["AZ_COG_ENDPOINT"],
"cog_key": os.environ["AZ_COG_KEY"],
"cog_region": os.environ["AZ_COG_REGION"]
})
credential = CognitiveServicesCredentials(st.session_state.cog_key)
self.cv_client = ComputerVisionClient(st.session_state.cog_endpoint, credential)
def v_spacer(self, height, sb=False) -> None:
for _ in range(height):
if sb:
st.sidebar.write('\n')
else:
st.write('\n')
def main(self):
st.write("An example of using the Azure Computer Vision Image Analysis service to extract a wide variety of visual features from images")
st.write("Includes object detection with location of labelled objects within bounding boxes")
st.markdown("**CLICK** any image below to see its features with associated confidence level")
features = [VisualFeatureTypes.description,
VisualFeatureTypes.tags,
VisualFeatureTypes.categories,
VisualFeatureTypes.brands,
VisualFeatureTypes.objects,
VisualFeatureTypes.adult]
images = [
"https://images.unsplash.com/photo-1597848212624-a19eb35e2651?w=700",
"https://images.unsplash.com/photo-1546436836-07a91091f160?w=700",
"https://images.unsplash.com/photo-1614200179396-2bdb77ebf81b?w=700",
"https://images.unsplash.com/photo-1534361960057-19889db9621e?w=700",
"https://images.unsplash.com/photo-1612175790672-698305ab79c8?w=700",
"https://images.unsplash.com/photo-1526336024174-e58f5cdd8e13?w=700",
"https://images.unsplash.com/photo-1533738363-b7f9aef128ce?w=700",
"https://images.unsplash.com/photo-1528825871115-3581a5387919?w=700",
"https://images.unsplash.com/photo-1568736333610-eae6e0ab9206?w=700",
"https://images.unsplash.com/photo-1532105956626-9569c03602f6?w=700",
"https://images.unsplash.com/photo-1614732414444-096e5f1122d5?w=700",
"https://c.static-nike.com/a/images/w_1920,c_limit/bzl2wmsfh7kgdkufrrjq/image.jpg?w=700",
"https://cdn.britannica.com/82/130482-050-87C2665C/Actor-Robin-Williams.jpg?w=700",
"https://images.unsplash.com/photo-1518780664697-55e3ad937233?w=700",
"https://images.unsplash.com/photo-1520716963369-9b24de965de4?w=700",
"https://images.unsplash.com/photo-1532526338225-bc66ea49a9f2?w=700",
"https://images.unsplash.com/photo-1517256064527-09c73fc73e38?w=700",
"https://images.unsplash.com/photo-1616325629936-99a9013c29c6?w=700",
"https://images.unsplash.com/photo-1540778339538-067eae485e9f?w=700",
"https://images.unsplash.com/photo-1545631757-8b75025a310e?w=700",
"https://images.unsplash.com/photo-1615535248235-253d93813ca5?w=700",
"https://m.media-amazon.com/images/I/818isgqqL3L._AC_SX466_.jpg?w=700",
"https://images.unsplash.com/photo-1551120599-440aefce5263?w=700",
"https://www.lydiamonks.com/wp-content/uploads/2014/06/sugarlump-and-unicorn_1.jpg?w=700"
]
clicked = clickable_images(
images,
titles=[f"Image #{str(i)}" for i in range(5)],
div_style={"display": "flex", "justify-content": "center", "flex-wrap": "wrap"},
img_style={"margin": "5px", "height": "200px"},
)
st.markdown(f"**You clicked image #{clicked}**" if clicked > -1 else "**No image clicked**")
if clicked == -1:
return
# Call image analysis service
analysis = self.cv_client.analyze_image(images[clicked] , features)
# Analysis - captions
if (len(analysis.description.captions) > 0):
self.v_spacer(height=2, sb=False)
st.markdown("**Description**")
for caption in analysis.description.captions:
st.write("{} ({:.2f}%)".format(caption.text, caption.confidence * 100))
# Analysis - tags
if (len(analysis.tags) > 0):
self.v_spacer(height=2, sb=False)
st.markdown("**Tags**")
for tag in analysis.tags:
st.write("- {} ({:.2f}%)".format(tag.name, tag.confidence * 100))
# Analysis - categories
if (len(analysis.categories) > 0):
self.v_spacer(height=2, sb=False)
st.markdown("**Categories**")
landmarks = []
celebrities = []
for category in analysis.categories:
st.write("- {} ({:.2f}%)".format(category.name, category.score * 100))
if category.detail:
if category.detail.landmarks:
for landmark in category.detail.landmarks:
if landmark not in landmarks:
landmarks.append(landmark)
if category.detail.celebrities:
for celebrity in category.detail.celebrities:
if celebrity not in celebrities:
celebrities.append(celebrity)
# Analysis - landmarks
if len(landmarks) > 0:
self.v_spacer(height=2, sb=False)
st.markdown("**Landmarks**")
for landmark in landmarks:
st.write("- {} ({:.2f}%)".format(landmark.name, landmark.confidence * 100))
# Analysis - celebrities
if len(celebrities) > 0:
self.v_spacer(height=2, sb=False)
st.markdown("**Celebrities**")
for celebrity in celebrities:
st.write("- {} ({:.2f}%)".format(celebrity.name, celebrity.confidence * 100))
# Analysis - brands
if (len(analysis.brands) > 0):
self.v_spacer(height=2, sb=False)
st.markdown("**Brands**")
for brand in analysis.brands:
st.write("- {} ({:.2f}%)".format(brand.name, brand.confidence * 100))
# Save image for in prep for thumbnail and object detection
response = requests.get(images[clicked])
image = Image.open(BytesIO(response.content))
image_analysis_file = "temp/image_analysis.jpg"
image = image.save(image_analysis_file)
# Generate a thumbnail
self.v_spacer(height=2, sb=False)
st.markdown("**Thumbnail Generation**")
with open(image_analysis_file, mode="rb") as image_data:
thumbnail_stream = self.cv_client.generate_thumbnail_in_stream(100, 100, image_data, True)
image_analysis_thumbnail = 'temp/image_analysis_thumbnail.jpg'
with open(image_analysis_thumbnail, "wb") as thumbnail_file:
for chunk in thumbnail_stream:
thumbnail_file.write(chunk)
st.image(image_analysis_thumbnail)
# Object detection with bounding boxes and labelling
if len(analysis.objects) > 0:
self.v_spacer(height=2, sb=False)
st.markdown("**Object Detection**")
fig = plt.figure(figsize=(8, 8))
plt.tight_layout()
plt.axis('off')
image = Image.open(image_analysis_file)
draw = ImageDraw.Draw(image)
color = 'cyan'
for detected_object in analysis.objects:
st.markdown("- {} ({:.2f}%)".format(detected_object.object_property, detected_object.confidence * 100))
r = detected_object.rectangle
bounding_box = ((r.x, r.y), (r.x + r.w, r.y + r.h))
draw.rectangle(bounding_box, outline=color, width=3)
plt.annotate(detected_object.object_property,(r.x, r.y), backgroundcolor=color)
plt.imshow(image)
outputfile = 'temp/image_analysis_object_detection.jpg'
fig.savefig(outputfile)
st.image(outputfile)
if __name__ == "__main__":
app = App()
app.main()