<jupyter_start><jupyter_text>Prerequisites- You must have created a Bing Search resource in Azure and obtain the keys.- Make sure the notebook is running on Python 3.4 and higher. Resources- [Quickstart: search for images with Bing Image Search SDK for Python](https://docs.microsoft.com/en-us/azure/cognitive-services/bing-image-search/image-sdk-python-quickstart) Running the walkthrough Create and initialize the applicationInstall the Azure Image Search SDK for Python:<jupyter_code>!pip install azure-cognitiveservices-search-imagesearch<jupyter_output><empty_output><jupyter_text>Import the following:<jupyter_code>from azure.cognitiveservices.search.imagesearch import ImageSearchAPI
from msrest.authentication import CognitiveServicesCredentials
import requests
import os<jupyter_output><empty_output><jupyter_text>Create variables for your subscription key and search term.<jupyter_code>subscription_key = "<Your-Subscription-Key>"
search_term_cat1 = "<Search-Term-Category-1>"
search_term_cat2 = "<Search-Term-Category-2>"<jupyter_output><empty_output><jupyter_text>Create the image search clientCreate an instance of `CognitiveServicesCredentials`, and use it to instantiate the client:<jupyter_code>client = ImageSearchAPI(CognitiveServicesCredentials(subscription_key))<jupyter_output><empty_output><jupyter_text>Send a search query to Bing Image Search API:<jupyter_code>image_results_cat1 = client.images.search(query=search_term_cat1, count=25)
image_results_cat2 = client.images.search(query=search_term_cat2, count=25)<jupyter_output><empty_output><jupyter_text>Process and view the resultsParse the image results returned in the response.If the response contains search results, store the first result and print out its details, such as a thumbnail URL, the original URL,along with the total number of returned images. Check the results for category 1:<jupyter_code>if image_results_cat1.value:
    first_image_result = image_results_cat1.value[0]
    print("Total number of images returned: {}".format(len(image_results_cat1.value)))
    print("First image thumbnail url: {}".format(
        first_image_result.thumbnail_url))
    print("First image content url: {}".format(first_image_result.content_url))
else:
    print("No image results returned!")<jupyter_output><empty_output><jupyter_text>Check the results for category 2:<jupyter_code>if image_results_cat2.value:
    first_image_result = image_results_cat2.value[0]
    print("Total number of images returned: {}".format(len(image_results_cat2.value)))
    print("First image thumbnail url: {}".format(
        first_image_result.thumbnail_url))
    print("First image content url: {}".format(first_image_result.content_url))
else:
    print("No image results returned!")<jupyter_output><empty_output><jupyter_text>Save images to local directoryThe images will be saved in the Azure notebooks environment in the folder "data". In this folder, make sure to have a separate folder for each category.  Createa a folder and save images for category 1:<jupyter_code>for i in range(0,len(image_results_cat1.value)):
    temp_img_url=((image_results_cat1.value[i]).content_url)
    resp = requests.get(temp_img_url,allow_redirects=True)
    filename="data/cat1/<Name-of-first-category>_"+str(i)+".png"
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    open(filename,"wb").write(resp.content)<jupyter_output><empty_output><jupyter_text>Category 2:<jupyter_code>for i in range(0,len(image_results_cat2.value)):
    temp_img_url=((image_results_cat2.value[i]).content_url)
    resp = requests.get(temp_img_url,allow_redirects=True)
    filename="data/cat2/<Name-of-second-category>_"+str(i)+".png"
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    open(filename,"wb").write(resp.content)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1010498687664042, 'fraction_numerical': 0.008923884514435695, 'mean_word_length': 7.108510638297872, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '108720', 'license': 'MIT', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1168, 'n_words': 271, 'path': 'labfiles/Lab 1 - Get data.ipynb', 'repo_name': 'dedvds/ImageClassifierApp', 'size': '3810'}