Skip to content

Commit b86767d

Browse files
authored
Visual Search Sample (#9)
* Visual Search doc * Draft of samples * Complete example, not tested
1 parent 59dbeea commit b86767d

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This project framework provides examples for the following services:
2424
* Using the **Bing Image Search SDK** [azure-cognitiveservices-search-imagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-imagesearch) for the [Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-image-search-api/)
2525
* Using the **Bing News Search SDK** [azure-cognitiveservices-search-newssearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-newssearch) for the [News Search API](https://azure.microsoft.com/services/cognitive-services/bing-news-search-api/)
2626
* Using the **Bing Video Search SDK** [azure-cognitiveservices-search-videosearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-videosearch) for the [Video Search API](https://azure.microsoft.com/services/cognitive-services/bing-video-search-api/)
27+
* Using the **Bing Visual Search SDK** [azure-cognitiveservices-search-visualsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-visualsearch) for the [Visual Search API](https://azure.microsoft.com/services/cognitive-services/bing-visual-search-api/)
2728
* Using the **Bing Web Search SDK** [azure-cognitiveservices-search-websearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-websearch) for the [Web Search API](https://azure.microsoft.com/services/cognitive-services/bing-web-search-api/)
2829

2930
### Vision
@@ -83,6 +84,7 @@ We provide several meta-packages to help you install several packages at a time.
8384
4. Set up the environment variable `IMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute ImageSearch tests.
8485
4. Set up the environment variable `NEWSSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute NewsSearch tests.
8586
4. Set up the environment variable `VIDEOSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute VideoSearch tests.
87+
4. Set up the environment variable `VISUALSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute VideoSearch tests.
8688
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute WebSearch tests.
8789
4. Set up the environment variable `COMPUTERVISION_SUBSCRIPTION_KEY` with your key if you want to execute Computer Vision tests. You might override too `COMPUTERVISION_LOCATION` (westcentralus by default).
8890
4. Set up the environment variable `CONTENTMODERATOR_SUBSCRIPTION_KEY` with your key if you want to execute Content Moderator tests. You might override too `CONTENTMODERATOR_LOCATION` (westcentralus by default).

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ azure-cognitiveservices-search-entitysearch
55
azure-cognitiveservices-search-imagesearch
66
azure-cognitiveservices-search-newssearch
77
azure-cognitiveservices-search-videosearch
8+
azure-cognitiveservices-search-visualsearch
89
azure-cognitiveservices-search-websearch
910
azure-cognitiveservices-vision-computervision
1011
azure-cognitiveservices-vision-contentmoderator
47.7 KB
Loading
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import json
2+
import os.path
3+
4+
from azure.cognitiveservices.search.visualsearch import VisualSearchAPI
5+
from azure.cognitiveservices.search.visualsearch.models import (
6+
VisualSearchRequest,
7+
CropArea,
8+
ImageInfo,
9+
Filters,
10+
KnowledgeRequest,
11+
)
12+
from msrest.authentication import CognitiveServicesCredentials
13+
14+
SUBSCRIPTION_KEY_ENV_NAME = "VISUALSEARCH_SUBSCRIPTION_KEY"
15+
16+
CWD = os.path.dirname(__file__)
17+
TEST_IMAGES = os.path.join(CWD, "TestImages")
18+
19+
def search_image_binary(subscription_key):
20+
"""VisualSearchImageBinary.
21+
22+
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.
23+
"""
24+
client = VisualSearchAPI(CognitiveServicesCredentials(subscription_key))
25+
26+
image_path = os.path.join(TEST_IMAGES, "image.jpg")
27+
with open(image_path, "rb") as image_fd:
28+
29+
# You need to pass the serialized form of the model
30+
knowledge_request = json.dumps(VisualSearchRequest().serialize())
31+
32+
print("Search visual search request with binary of dog image")
33+
result = client.images.visual_search(image=image_fd, knowledge_request=knowledge_request)
34+
35+
if not result:
36+
print("No visual search result data.")
37+
return
38+
39+
# Visual Search results
40+
if result.image.image_insights_token:
41+
print("Uploaded image insights token: {}".format(result.image.image_insights_token))
42+
else:
43+
print("Couldn't find image insights token!")
44+
45+
# List of tags
46+
if result.tags:
47+
first_tag = result.tags[0]
48+
print("Visual search tag count: {}".format(len(result.tags)))
49+
50+
# List of actions in first tag
51+
if first_tag.actions:
52+
first_tag_action = first_tag.actions[0]
53+
print("First tag action count: {}".format(len(first_tag.actions)))
54+
print("First tag action type: {}".format(first_tag_action.action_type))
55+
else:
56+
print("Couldn't find tag actions!")
57+
else:
58+
print("Couldn't find image tags!")
59+
60+
def search_image_binary_with_crop_area(subscription_key):
61+
"""VisualSearchImageBinaryWithCropArea.
62+
63+
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.
64+
"""
65+
client = VisualSearchAPI(CognitiveServicesCredentials(subscription_key))
66+
67+
image_path = os.path.join(TEST_IMAGES, "image.jpg")
68+
with open(image_path, "rb") as image_fd:
69+
70+
crop_area = CropArea(top=0.1,bottom=0.5,left=0.1,right=0.9)
71+
knowledge_request = VisualSearchRequest(image_info=ImageInfo(crop_area=crop_area))
72+
73+
# You need to pass the serialized form of the model
74+
knowledge_request = json.dumps(knowledge_request.serialize())
75+
76+
print("Search visual search request with binary of dog image")
77+
result = client.images.visual_search(image=image_fd, knowledge_request=knowledge_request)
78+
79+
if not result:
80+
print("No visual search result data.")
81+
return
82+
83+
# Visual Search results
84+
if result.image.image_insights_token:
85+
print("Uploaded image insights token: {}".format(result.image.image_insights_token))
86+
else:
87+
print("Couldn't find image insights token!")
88+
89+
# List of tags
90+
if result.tags:
91+
first_tag = result.tags[0]
92+
print("Visual search tag count: {}".format(len(result.tags)))
93+
94+
# List of actions in first tag
95+
if first_tag.actions:
96+
first_tag_action = first_tag.actions[0]
97+
print("First tag action count: {}".format(len(first_tag.actions)))
98+
print("First tag action type: {}".format(first_tag_action.action_type))
99+
else:
100+
print("Couldn't find tag actions!")
101+
else:
102+
print("Couldn't find image tags!")
103+
104+
def search_url_with_filters(subscription_key):
105+
"""VisualSearchUrlWithFilters.
106+
107+
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.
108+
"""
109+
client = VisualSearchAPI(CognitiveServicesCredentials(subscription_key))
110+
111+
image_url = "https://images.unsplash.com/photo-1512546148165-e50d714a565a?w=600&q=80"
112+
filters = Filters(site="www.bing.com")
113+
114+
knowledge_request = VisualSearchRequest(
115+
image_info=ImageInfo(url=image_url),
116+
knowledge_request=KnowledgeRequest(filters=filters)
117+
)
118+
119+
# You need to pass the serialized form of the model
120+
knowledge_request = json.dumps(knowledge_request.serialize())
121+
122+
print("Search visual search request with url of dog image")
123+
result = client.images.visual_search(knowledge_request=knowledge_request)
124+
125+
if not result:
126+
print("No visual search result data.")
127+
return
128+
129+
# Visual Search results
130+
if result.image.image_insights_token:
131+
print("Uploaded image insights token: {}".format(result.image.image_insights_token))
132+
else:
133+
print("Couldn't find image insights token!")
134+
135+
# List of tags
136+
if result.tags:
137+
first_tag = result.tags[0]
138+
print("Visual search tag count: {}".format(len(result.tags)))
139+
140+
# List of actions in first tag
141+
if first_tag.actions:
142+
first_tag_action = first_tag.actions[0]
143+
print("First tag action count: {}".format(len(first_tag.actions)))
144+
print("First tag action type: {}".format(first_tag_action.action_type))
145+
else:
146+
print("Couldn't find tag actions!")
147+
else:
148+
print("Couldn't find image tags!")
149+
150+
def search_insights_token_with_crop_area(subscription_key):
151+
"""VisualSearchInsightsTokenWithCropArea.
152+
153+
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.
154+
"""
155+
client = VisualSearchAPI(CognitiveServicesCredentials(subscription_key))
156+
157+
image_insights_token = "bcid_113F29C079F18F385732D8046EC80145*ccid_oV/QcH95*mid_687689FAFA449B35BC11A1AE6CEAB6F9A9B53708*thid_R.113F29C079F18F385732D8046EC80145"
158+
crop_area = CropArea(top=0.1,bottom=0.5,left=0.1,right=0.9)
159+
160+
knowledge_request = VisualSearchRequest(
161+
image_info=ImageInfo(
162+
image_insights_token=image_insights_token,
163+
crop_area=crop_area
164+
),
165+
)
166+
167+
# You need to pass the serialized form of the model
168+
knowledge_request = json.dumps(knowledge_request.serialize())
169+
170+
print("Search visual search request with url of dog image")
171+
result = client.images.visual_search(knowledge_request=knowledge_request)
172+
173+
if not result:
174+
print("No visual search result data.")
175+
return
176+
177+
# Visual Search results
178+
if result.image.image_insights_token:
179+
print("Uploaded image insights token: {}".format(result.image.image_insights_token))
180+
else:
181+
print("Couldn't find image insights token!")
182+
183+
# List of tags
184+
if result.tags:
185+
first_tag = result.tags[0]
186+
print("Visual search tag count: {}".format(len(result.tags)))
187+
188+
# List of actions in first tag
189+
if first_tag.actions:
190+
first_tag_action = first_tag.actions[0]
191+
print("First tag action count: {}".format(len(first_tag.actions)))
192+
print("First tag action type: {}".format(first_tag_action.action_type))
193+
else:
194+
print("Couldn't find tag actions!")
195+
else:
196+
print("Couldn't find image tags!")
197+
198+
if __name__ == "__main__":
199+
import sys, os.path
200+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
201+
from tools import execute_samples
202+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

0 commit comments

Comments
 (0)