Skip to content

Commit 3499739

Browse files
iscai-msftlmazuel
authored andcommitted
Cognitive services python Custom Image Search samples (#22)
* 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 * consolidated the face samples into a single python file face_samples.py * edited Custom Search Samples, as CustomSearchAPI has been switched to CustomSearchClient * got rid of changes I accidentally made to text analytics and visual search samples * working on custom image search * added custom image search samples and updated the README * added .idea folder to gitignore * about to test whether style changes break the sample * updated custom image search to adress requested changes * slight spacing formatting * Update requirements.txt
1 parent f9c4caf commit 3499739

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This project framework provides examples for the following services:
2222

2323
* Using the **Bing Autosuggest SDK** [azure-cognitiveservices-search-autosuggest](http://pypi.python.org/pypi/azure-cognitiveservices-search-autosuggest) for the [Autosuggest API](https://azure.microsoft.com/services/cognitive-services/autosuggest/)
2424
* Using the **Bing Custom Search SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customsearch) for the [Custom Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/)
25+
* Using the **Bing Custom Image Search SDK** [azure-cognitiveservices-search-customimagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customimagesearch) for the [Custom Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/)
2526
* Using the **Bing Entity Search SDK** [azure-cognitiveservices-search-entitysearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-entitysearch) for the [Entity Search API](https://azure.microsoft.com/services/cognitive-services/bing-entity-search-api/)
2627
* 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/)
2728
* 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/)
@@ -85,6 +86,7 @@ We provide several meta-packages to help you install several packages at a time.
8586
4. Set up the environment variable `TEXTANALYTICS_SUBSCRIPTION_KEY` with your key if you want to execute TextAnalytics tests. You might override too `TEXTANALYTICS_LOCATION` (westcentralus by default).
8687
3. Set up the environment variable `AUTOSUGGEST_SUBSCRIPTION_KEY` with your key if you want to execute Autosuggest tests.
8788
3. Set up the environment variable `CUSTOMSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomSearch tests.
89+
3. Set up the environment variable `CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomImageSearch tests.
8890
3. Set up the environment variable `ENTITYSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute EntitySearch tests.
8991
4. Set up the environment variable `IMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute ImageSearch tests.
9092
4. Set up the environment variable `NEWSSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute NewsSearch tests.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ azure-cognitiveservices-language-luis
22
azure-cognitiveservices-language-spellcheck
33
azure-cognitiveservices-language-textanalytics
44
azure-cognitiveservices-search-autosuggest
5+
azure-cognitiveservices-search-customimagesearch
56
azure-cognitiveservices-search-customsearch>=0.2.0 # sample won't work with previous versions
67
azure-cognitiveservices-search-entitysearch
78
azure-cognitiveservices-search-imagesearch
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from azure.cognitiveservices.search.customimagesearch import CustomImageSearchAPI
2+
from msrest.authentication import CognitiveServicesCredentials
3+
4+
SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY"
5+
6+
7+
def custom_image_search_result_lookup(subscription_key):
8+
"""CustomImageSearchResultLookup.
9+
10+
This will look up a single query (Xbox) and print out number of results, insights token, thumbnail url, content url for the first image result
11+
"""
12+
13+
client = CustomImageSearchAPI(credentials=CognitiveServicesCredentials(subscription_key))
14+
try:
15+
image_results = client.custom_instance.image_search(query="Xbox", custom_config=1)
16+
print("Searched for Query \" Xbox \"")
17+
18+
# WebPages
19+
if image_results.value:
20+
# find the first web page
21+
first_image_result = image_results.value[0]
22+
23+
if first_image_result:
24+
print("Image result count: {}".format(len(image_results.value)))
25+
print("First image insights token: {}".format(first_image_result.image_insights_token))
26+
print("First image thumbnail url: {}".format(first_image_result.thumbnail_url))
27+
print("First image content url: {}".format(first_image_result.content_url))
28+
else:
29+
print("Couldn't find image results!")
30+
else:
31+
print("Couldn't find image results!")
32+
except Exception as e:
33+
print("encountered exception. " + str(e))
34+
35+
36+
if __name__ == "__main__":
37+
import sys, os.path
38+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
39+
from tools import execute_samples
40+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

0 commit comments

Comments
 (0)