-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathvisual_search_samples.py
331 lines (269 loc) · 11.5 KB
/
visual_search_samples.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import json
import os
from visual_search_client import VisualSearchClient
from visual_search_client.models import (
VisualSearchRequest,
CropArea,
ImageInfo,
Filters,
KnowledgeRequest,
)
from azure.core.credentials import AzureKeyCredential
SUBSCRIPTION_KEY = None
ENDPOINT = "https://api.bing.microsoft.com" + "/v7.0/"
CWD = os.path.dirname(__file__)
TEST_IMAGES = os.path.join(CWD, "TestImages")
def search_image_binary(subscription_key):
"""VisualSearchImageBinary.
This will send an image binary in the body of the post request and print out the imageInsightsToken, the number of tags, the number of actions, and the first actionType.
"""
client = VisualSearchClient(
endpoint=ENDPOINT, credential=AzureKeyCredential(subscription_key)
)
image_path = os.path.join(TEST_IMAGES, "image.jpg")
with open(image_path, "rb") as image_fd:
# You need to pass the serialized form of the model
knowledge_request = json.dumps(VisualSearchRequest().serialize())
print("Search visual search request with binary of dog image")
result = client.images.visual_search(
image=image_fd, knowledge_request=knowledge_request
)
if not result:
print("No visual search result data.")
return
# Visual Search results
if result.image.image_insights_token:
print(
"Uploaded image insights token: {}".format(
result.image.image_insights_token
)
)
else:
print("Couldn't find image insights token!")
# List of tags
if result.tags:
first_tag = result.tags[0]
print("Visual search tag count: {}".format(len(result.tags)))
# List of actions in first tag
if first_tag.actions:
first_tag_action = first_tag.actions[0]
print("First tag action count: {}".format(len(first_tag.actions)))
print("First tag action type: {}".format(first_tag_action.action_type))
else:
print("Couldn't find tag actions!")
else:
print("Couldn't find image tags!")
def search_image_binary_with_crop_area(subscription_key):
"""VisualSearchImageBinaryWithCropArea.
This will send an image binary in the body of the post request, along with a cropArea object, and print out the imageInsightsToken, the number of tags, the number of actions, and the first actionType.
"""
client = VisualSearchClient(
endpoint=ENDPOINT, credential=AzureKeyCredential(subscription_key)
)
image_path = os.path.join(TEST_IMAGES, "image.jpg")
with open(image_path, "rb") as image_fd:
crop_area = CropArea(top=0.1, bottom=0.5, left=0.1, right=0.9)
knowledge_request = VisualSearchRequest(
image_info=ImageInfo(crop_area=crop_area)
)
# You need to pass the serialized form of the model
knowledge_request = json.dumps(knowledge_request.serialize())
print("Search visual search request with binary of dog image")
result = client.images.visual_search(
image=image_fd, knowledge_request=knowledge_request
)
if not result:
print("No visual search result data.")
return
# Visual Search results
if result.image.image_insights_token:
print(
"Uploaded image insights token: {}".format(
result.image.image_insights_token
)
)
else:
print("Couldn't find image insights token!")
# List of tags
if result.tags:
first_tag = result.tags[0]
print("Visual search tag count: {}".format(len(result.tags)))
# List of actions in first tag
if first_tag.actions:
first_tag_action = first_tag.actions[0]
print("First tag action count: {}".format(len(first_tag.actions)))
print("First tag action type: {}".format(first_tag_action.action_type))
else:
print("Couldn't find tag actions!")
else:
print("Couldn't find image tags!")
def search_url_with_filters(subscription_key):
"""VisualSearchUrlWithFilters.
This will send an image url in the knowledgeRequest parameter, along with a \"site:www.bing.com\" filter, and print out the imageInsightsToken, the number of tags, the number of actions, and the first actionType.
"""
client = VisualSearchClient(
endpoint=ENDPOINT, credential=AzureKeyCredential(subscription_key)
)
image_url = (
"https://images.unsplash.com/photo-1512546148165-e50d714a565a?w=600&q=80"
)
filters = Filters(site="www.bing.com")
knowledge_request = VisualSearchRequest(
image_info=ImageInfo(url=image_url),
knowledge_request=KnowledgeRequest(filters=filters),
)
# You need to pass the serialized form of the model
knowledge_request = json.dumps(knowledge_request.serialize())
print("Search visual search request with url of dog image")
result = client.images.visual_search(knowledge_request=knowledge_request)
if not result:
print("No visual search result data.")
return
# Visual Search results
if result.image.image_insights_token:
print(
"Uploaded image insights token: {}".format(
result.image.image_insights_token
)
)
else:
print("Couldn't find image insights token!")
# List of tags
if result.tags:
first_tag = result.tags[0]
print("Visual search tag count: {}".format(len(result.tags)))
# List of actions in first tag
if first_tag.actions:
first_tag_action = first_tag.actions[0]
print("First tag action count: {}".format(len(first_tag.actions)))
print("First tag action type: {}".format(first_tag_action.action_type))
else:
print("Couldn't find tag actions!")
else:
print("Couldn't find image tags!")
def search_insights_token_with_crop_area(subscription_key):
"""VisualSearchInsightsTokenWithCropArea.
This will send an image insights token in the knowledgeRequest parameter, along with a cropArea object, and print out the imageInsightsToken, the number of tags, the number of actions, and the first actionType.
"""
client = VisualSearchClient(
endpoint=ENDPOINT, credential=AzureKeyCredential(subscription_key)
)
image_insights_token = "bcid_113F29C079F18F385732D8046EC80145*ccid_oV/QcH95*mid_687689FAFA449B35BC11A1AE6CEAB6F9A9B53708*thid_R.113F29C079F18F385732D8046EC80145"
crop_area = CropArea(top=0.1, bottom=0.5, left=0.1, right=0.9)
knowledge_request = VisualSearchRequest(
image_info=ImageInfo(
image_insights_token=image_insights_token, crop_area=crop_area
),
)
# You need to pass the serialized form of the model
knowledge_request = json.dumps(knowledge_request.serialize())
print("Search visual search request with url of dog image")
result = client.images.visual_search(knowledge_request=knowledge_request)
if not result:
print("No visual search result data.")
return
# Visual Search results
if result.image.image_insights_token:
print(
"Uploaded image insights token: {}".format(
result.image.image_insights_token
)
)
else:
print("Couldn't find image insights token!")
# List of tags
if result.tags:
first_tag = result.tags[0]
print("Visual search tag count: {}".format(len(result.tags)))
# List of actions in first tag
if first_tag.actions:
first_tag_action = first_tag.actions[0]
print("First tag action count: {}".format(len(first_tag.actions)))
print("First tag action type: {}".format(first_tag_action.action_type))
else:
print("Couldn't find tag actions!")
else:
print("Couldn't find image tags!")
def search_url_with_json(subscription_key):
"""VisualSearchURLWithJSON.
This will send a visual search request in JSON form, and print out the imageInsightsToken, the number of tags, and the first actionCount and actionType.
"""
client = VisualSearchClient(
endpoint=ENDPOINT, credential=AzureKeyCredential(subscription_key)
)
try:
"""
The visual search request can be passed in as a JSON string
The image is specified via URL in the ImageInfo object, along with a crop area as shown below:
{
"imageInfo": {
"url": "https://images.unsplash.com/photo-1512546148165-e50d714a565a?w=600&q=80",
"cropArea": {
"top": 0.1,
"bottom": 0.5,
"left": 0.1,
"right": 0.9
}
},
"knowledgeRequest": {
"filters": {
"site": "www.bing.com"
}
}
}
"""
visual_search_request_json = '{"imageInfo":{"url":"https://images.unsplash.com/photo-1512546148165-e50d714a565a?w=600&q=80","cropArea":{"top":0.1,"bottom":0.5,"left":0.1,"right":0.9}},"knowledgeRequest":{"filters":{"site":"www.bing.com"}}}'
# An image binary is not necessary here, as the image is specified via URL
visual_search_results = client.images.visual_search(
knowledge_request=visual_search_request_json
)
print("Search visual search request with url of dog image")
if not visual_search_results:
print("No visual search result data.")
else:
# Visual Search results
if visual_search_results.image.image_insights_token:
print(
"Uploaded image insights token: {}".format(
visual_search_results.image.image_insights_token
)
)
else:
print("Couldn't find image insights token!")
# List of tags
if visual_search_results.tags:
first_tag_result = visual_search_results.tags[0]
print(
"Visual search tag count: {}".format(
len(visual_search_results.tags)
)
)
# List of actions in first tag
if first_tag_result.actions:
first_action_result = first_tag_result.actions[0]
print(
"First tag action count: {}".format(
len(first_tag_result.actions)
)
)
print(
"First tag action type: {}".format(
first_action_result.action_type
)
)
else:
print("Couldn't find tag actions!")
else:
print("Couldn't find image tags!")
except Exception as e:
print("Encountered exception. " + str(e))
if __name__ == "__main__":
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..", "..")))
search_image_binary(SUBSCRIPTION_KEY)
search_image_binary_with_crop_area(SUBSCRIPTION_KEY)
search_url_with_filters(SUBSCRIPTION_KEY)
search_insights_token_with_crop_area(SUBSCRIPTION_KEY)
search_url_with_json(SUBSCRIPTION_KEY)