-
Notifications
You must be signed in to change notification settings - Fork 6
/
azure.py
389 lines (300 loc) · 11.2 KB
/
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#
# Python Functions for Azure Computer Vision 4 (Florence)
#
# Serge Retkowsky | Microsoft | 4-May-2023
#
import cv2
import datetime
import math
import json
import os
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import requests
import pandas as pd
import seaborn as sns
from dotenv import load_dotenv
from PIL import Image
# Loading Azure Computer Vision 4 endpoint and key
load_dotenv("azure.env")
key = os.getenv('azure_cv_key')
endpoint = os.getenv("azure_cv_endpoint")
def view_image(image_file):
"""
View image file
"""
plt.imshow(Image.open(image_file))
plt.axis('off')
plt.title("Image: " + image_file, fontdict={'fontsize': 10})
plt.show()
def describe_image_with_AzureCV4(image_file):
"""
Get tags & caption from an image using Azure Computer Vision 4 Florence
"""
options = "&features=tags,caption"
model = "?api-version=2023-02-01-preview&modelVersion=latest"
url = endpoint + "/computervision/imageanalysis:analyze" + model + options
headers_cv = {
'Content-type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': key
}
with open(image_file, 'rb') as f:
data = f.read()
r = requests.post(url, data=data, headers=headers_cv)
results = r.json()
print("Automatic analysis of the image using Azure Computer Vision 4.0:")
print("\033[1;31;34m")
print(" Main caption:")
print(
f" {results['captionResult']['text']} = {results['captionResult']['confidence']:.3f}"
)
print("\033[1;31;32m")
print(" Detected tags:")
for tag in results['tagsResult']['values']:
print(f" {tag['name']:18} = {tag['confidence']:.3f}")
def image_embedding_batch(image_file):
"""
Embedding image using Azure Computer Vision 4 Florence
"""
version = "?api-version=2023-02-01-preview&modelVersion=latest"
vec_img_url = endpoint + "/computervision/retrieval:vectorizeImage" + version
headers_image = {
'Content-type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': key
}
with open(image_file, 'rb') as f:
data = f.read()
r = requests.post(vec_img_url, data=data, headers=headers_image)
image_emb = r.json()['vector']
return image_emb, r
def remove_background(image_file):
"""
Removing background from an image file using Azure Computer Vision 4
"""
remove_background_url = endpoint +\
"/computervision/imageanalysis:segment?api-version=2023-02-01-preview&mode=backgroundRemoval"
headers_background = {
'Content-type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': key
}
print(
"Removing background from the image using Azure Computer Vision 4.0..."
)
with open(image_file, 'rb') as f:
data = f.read()
r = requests.post(remove_background_url, data=data, headers=headers_background)
output_image = "without_background.jpg"
with open(output_image, 'wb') as f:
f.write(r.content)
print("Done")
return output_image
def side_by_side_images(image_file1, image_file2):
"""
Display two images side by side
"""
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(plt.imread(image_file1))
ax[1].imshow(plt.imread(image_file2))
for i in range(2):
ax[i].axis('off')
ax[i].set_title(['Initial image', 'Without the background'][i])
fig.suptitle('Background removal with Azure Computer Vision 4', fontsize=11)
plt.tight_layout()
plt.show()
def view_similar_images_using_image(reference_image, topn_list,
simil_topn_list, num_rows=2, num_cols=3):
"""
Plot similar images using an image with Azure Computer Vision 4 Florence
"""
img_list = topn_list
if img_list[0] != reference_image:
img_list.insert(0, reference_image)
num_images = len(img_list)
FIGSIZE = (12, 8)
fig, axes = plt.subplots(nrows=num_rows, ncols=num_cols, figsize=FIGSIZE)
size = 8 if num_rows >= 3 else 10
for i, ax in enumerate(axes.flat):
if i < num_images:
img = mpimg.imread(img_list[i])
ax.imshow(img)
if i == 0:
imgtitle = f"Image to search:\n {os.path.basename(img_list[i])}"
ax.set_title(imgtitle, size=size, color='blue')
else:
imgtitle = f"Top {i}: {os.path.basename(img_list[i])}\nSimilarity = {round(simil_topn_list[i-1], 5)}"
ax.set_title(imgtitle, size=size, color='green')
ax.axis('off')
else:
ax.axis('off')
plt.show()
print("\033[1;31;32m",
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"Powered by Azure Computer Vision Florence")
def text_embedding(promptxt):
"""
Embedding text using Azure Computer Vision 4 Florence
"""
version = "?api-version=2023-02-01-preview&modelVersion=latest"
vec_txt_url = endpoint + "/computervision/retrieval:vectorizeText" + version
headers_prompt = {
'Content-type': 'application/json',
'Ocp-Apim-Subscription-Key': key
}
prompt = {'text': promptxt}
r = requests.post(vec_txt_url,
data=json.dumps(prompt),
headers=headers_prompt)
text_emb = r.json()['vector']
return text_emb
def image_embedding(image_file):
"""
Embedding image using Azure Computer Vision 4 Florence
"""
version = "?api-version=2023-02-01-preview&modelVersion=latest"
vec_img_url = endpoint + "/computervision/retrieval:vectorizeImage" + version
headers_image = {
'Content-type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': key
}
with open(image_file, 'rb') as f:
data = f.read()
r = requests.post(vec_img_url, data=data, headers=headers_image)
image_emb = r.json()['vector']
return image_emb
def get_cosine_similarity(vector1, vector2):
"""
Get cosine similarity value between two embedded vectors
Using sklearn
"""
dot_product = 0
length = min(len(vector1), len(vector2))
for i in range(length):
dot_product += vector1[i] * vector2[i]
cosine_similarity = dot_product / (math.sqrt(sum(x * x for x in vector1))\
* math.sqrt(sum(x * x for x in vector2)))
return cosine_similarity
def get_similar_images_using_image(list_emb, image_files, image_file):
"""
Get similar images using an image with Azure Computer Vision 4 Florence
"""
ref_emb = image_embedding(image_file)
idx = 0
results_list = []
for emb_image in list_emb:
simil = get_cosine_similarity(ref_emb, list_emb[idx])
results_list.append(simil)
idx += 1
df_files = pd.DataFrame(image_files, columns=['image_file'])
df_simil = pd.DataFrame(results_list, columns=['similarity'])
df = pd.concat([df_files, df_simil], axis=1)
df.sort_values('similarity',
axis=0,
ascending=False,
inplace=True,
na_position='last')
return df
def get_similar_images_using_prompt(prompt, image_files, list_emb):
"""
Get similar umages using a prompt with Azure Computer Vision 4 Florence
"""
prompt_emb = text_embedding(prompt)
idx = 0
results_list = []
for emb_image in list_emb:
simil = get_cosine_similarity(prompt_emb, list_emb[idx])
results_list.append(simil)
idx += 1
df_files = pd.DataFrame(image_files, columns=['image_file'])
df_simil = pd.DataFrame(results_list, columns=['similarity'])
df = pd.concat([df_files, df_simil], axis=1)
df.sort_values('similarity',
axis=0,
ascending=False,
inplace=True,
na_position='last')
return df
def get_results_using_image(reference_image, nobackground_image,
image_files, list_emb, topn, disp=False):
"""
Get the topn results from a visual search using an image
Will generate a df, display the topn images and return the df
"""
df = get_similar_images_using_image(list_emb, image_files, nobackground_image)
df.head(topn).style.background_gradient(
cmap=sns.light_palette("green", as_cmap=True))
topn_list, simil_topn_list = get_topn_images(df, topn, disp=disp)
nb_cols = 3
nb_rows = (topn + nb_cols - 1) // nb_cols
view_similar_images_using_image(reference_image, topn_list, simil_topn_list,
num_cols=nb_cols, num_rows=nb_rows)
return df
def get_results_using_prompt(query, image_files, list_emb, topn, disp=False):
"""
Get the topn results from a visual search using a text query
Will generate a df, display the topn images and return the df
"""
df = get_similar_images_using_prompt(query, image_files, list_emb)
df.head(topn).style.background_gradient(
cmap=sns.light_palette("green", as_cmap=True))
topn_list, simil_topn_list = get_topn_images(df, topn, disp=disp)
nb_cols = 3
nb_rows = (topn + nb_cols - 1) // nb_cols
view_similar_images_using_prompt(query, topn_list, simil_topn_list,
num_cols=nb_cols, num_rows=nb_rows)
return df
def get_topn_images(df, topn=5, disp=False):
"""
Get topn similar images
"""
idx = 0
if disp:
print("\033[1;31;34mTop", topn, "images:\n")
topn_list = []
simil_topn_list = []
while idx < topn:
row = df.iloc[idx]
if disp:
print(
f"{idx+1:03} {row['image_file']} with similarity index = {row['similarity']}"
)
topn_list.append(row['image_file'])
simil_topn_list.append(row['similarity'])
idx += 1
return topn_list, simil_topn_list
def view_similar_images_using_prompt(query, topn_list, simil_topn_list,
num_rows=2, num_cols=3):
"""
Plot similar images using a prompt with Azure Computer Vision 4 Florence
"""
print("\033[1;31;34m")
print("Similar images using query =", query)
num_images = len(topn_list)
FIGSIZE = (12, 8)
fig, axes = plt.subplots(nrows=num_rows, ncols=num_cols, figsize=FIGSIZE)
size = 8 if num_rows >= 3 else 10
for i, ax in enumerate(axes.flat):
if i < num_images:
img = mpimg.imread(topn_list[i])
ax.imshow(img)
imgtitle = f"Top {i+1}: {os.path.basename(topn_list[i])}\nSimilarity = {round(simil_topn_list[i], 5)}"
ax.set_title(imgtitle, size=size, color='green')
ax.axis('off')
else:
ax.axis('off')
plt.show()
print("\033[1;31;32m",
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"Powered by Azure Computer Vision Florence")
def video_details(video_filename):
"""
Video information
"""
cap = cv2.VideoCapture(video_filename)
fps = int(cap.get(cv2.CAP_PROP_FPS))
nbframes = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = nbframes / fps
print(f"Video filename: {video_filename}")
print(f"Video duration in secs = {duration:.2f} seconds")
print(f"Frames per second: {fps}")
print(f"Total number of frames: {nbframes}")
return duration, fps, nbframes