Skip to content

Commit b8a5cca

Browse files
iscai-msftlmazuel
authored andcommitted
Cognitive services python visual search samples (#23)
* added working samples for common and detection * edited common and detection parameters and completed find_similar sample * edited find_similar sample and added group sample * added identify sample * added samples for verify * finished samples and edited retrieval method of environment variables * cleaned up samples, getting rid of unused imports and variables * working on visual search samples, trying out s9 key * added visual search samples * added .idea folder to gitignore * updated visual search to address requested changes * edited sample * updated visual search samples to use VisualSearchClient * Update requirements.txt
1 parent 3499739 commit b8a5cca

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ ENV/
105105

106106
#DS store
107107
.DS_Store
108-
samples/DS_Store
108+
samples/DS_Store

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ azure-cognitiveservices-search-entitysearch
88
azure-cognitiveservices-search-imagesearch
99
azure-cognitiveservices-search-newssearch
1010
azure-cognitiveservices-search-videosearch
11-
azure-cognitiveservices-search-visualsearch
11+
azure-cognitiveservices-search-visualsearch>=0.2.0 # sample won't work with previous versions
1212
azure-cognitiveservices-search-websearch
1313
azure-cognitiveservices-vision-computervision>=0.2.0 # sample won't work with previous versions
1414
azure-cognitiveservices-vision-contentmoderator

samples/search/visual_search_samples.py

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import os.path
33

4-
from azure.cognitiveservices.search.visualsearch import VisualSearchAPI
4+
from azure.cognitiveservices.search.visualsearch import VisualSearchClient
55
from azure.cognitiveservices.search.visualsearch.models import (
66
VisualSearchRequest,
77
CropArea,
@@ -21,7 +21,8 @@ def search_image_binary(subscription_key):
2121
2222
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.
2323
"""
24-
client = VisualSearchAPI(CognitiveServicesCredentials(subscription_key))
24+
25+
client = VisualSearchClient(endpoint="https://api.cognitive.microsoft.com", credentials=CognitiveServicesCredentials(subscription_key))
2526

2627
image_path = os.path.join(TEST_IMAGES, "image.jpg")
2728
with open(image_path, "rb") as image_fd:
@@ -62,12 +63,12 @@ def search_image_binary_with_crop_area(subscription_key):
6263
6364
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.
6465
"""
65-
client = VisualSearchAPI(CognitiveServicesCredentials(subscription_key))
66+
67+
client = VisualSearchClient(endpoint="https://api.cognitive.microsoft.com", credentials=CognitiveServicesCredentials(subscription_key))
6668

6769
image_path = os.path.join(TEST_IMAGES, "image.jpg")
6870
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+
crop_area = CropArea(top=0.1, bottom=0.5, left=0.1, right=0.9)
7172
knowledge_request = VisualSearchRequest(image_info=ImageInfo(crop_area=crop_area))
7273

7374
# You need to pass the serialized form of the model
@@ -106,7 +107,8 @@ def search_url_with_filters(subscription_key):
106107
107108
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.
108109
"""
109-
client = VisualSearchAPI(CognitiveServicesCredentials(subscription_key))
110+
111+
client = VisualSearchClient(endpoint="https://api.cognitive.microsoft.com", credentials=CognitiveServicesCredentials(subscription_key))
110112

111113
image_url = "https://images.unsplash.com/photo-1512546148165-e50d714a565a?w=600&q=80"
112114
filters = Filters(site="www.bing.com")
@@ -152,7 +154,8 @@ def search_insights_token_with_crop_area(subscription_key):
152154
153155
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.
154156
"""
155-
client = VisualSearchAPI(CognitiveServicesCredentials(subscription_key))
157+
158+
client = VisualSearchClient(endpoint="https://api.cognitive.microsoft.com", credentials=CognitiveServicesCredentials(subscription_key))
156159

157160
image_insights_token = "bcid_113F29C079F18F385732D8046EC80145*ccid_oV/QcH95*mid_687689FAFA449B35BC11A1AE6CEAB6F9A9B53708*thid_R.113F29C079F18F385732D8046EC80145"
158161
crop_area = CropArea(top=0.1,bottom=0.5,left=0.1,right=0.9)
@@ -195,6 +198,69 @@ def search_insights_token_with_crop_area(subscription_key):
195198
else:
196199
print("Couldn't find image tags!")
197200

201+
202+
def search_url_with_json(subscription_key):
203+
"""VisualSearchURLWithJSON.
204+
205+
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.
206+
"""
207+
208+
client = VisualSearchClient(endpoint="https://api.cognitive.microsoft.com", credentials=CognitiveServicesCredentials(subscription_key))
209+
try:
210+
"""
211+
The visual search request can be passed in as a JSON string
212+
The image is specified via URL in the ImageInfo object, along with a crop area as shown below:
213+
{
214+
"imageInfo": {
215+
"url": "https://images.unsplash.com/photo-1512546148165-e50d714a565a?w=600&q=80",
216+
"cropArea": {
217+
"top": 0.1,
218+
"bottom": 0.5,
219+
"left": 0.1,
220+
"right": 0.9
221+
}
222+
},
223+
"knowledgeRequest": {
224+
"filters": {
225+
"site": "www.bing.com"
226+
}
227+
}
228+
}
229+
"""
230+
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\"}}}"
231+
232+
# An image binary is not necessary here, as the image is specified via URL
233+
visual_search_results = client.images.visual_search(knowledge_request=visual_search_request_json)
234+
print("Search visual search request with url of dog image")
235+
236+
if not visual_search_results:
237+
print("No visual search result data.")
238+
else:
239+
# Visual Search results
240+
if visual_search_results.image.image_insights_token:
241+
print("Uploaded image insights token: {}".format(visual_search_results.image.image_insights_token))
242+
else:
243+
print("Couldn't find image insights token!")
244+
245+
# List of tags
246+
if visual_search_results.tags:
247+
first_tag_result = visual_search_results.tags[0]
248+
print("Visual search tag count: {}".format(len(visual_search_results.tags)))
249+
250+
# List of actions in first tag
251+
if first_tag_result.actions:
252+
first_action_result = first_tag_result.actions[0]
253+
print("First tag action count: {}".format(len(first_tag_result.actions)))
254+
print("First tag action type: {}".format(first_action_result.action_type))
255+
else:
256+
print("Couldn't find tag actions!")
257+
else:
258+
print("Couldn't find image tags!")
259+
260+
except Exception as e:
261+
print("Encountered exception. " + str(e))
262+
263+
198264
if __name__ == "__main__":
199265
import sys, os.path
200266
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))

0 commit comments

Comments
 (0)