Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel
Ezequiel Jadib 6b3df2c0b3
Update documentation links to point to new docs (#118)
7 years ago
..
8d69caed53
[CSharp-SimilarProducts] Update to Microsoft.Bot.Builder v3.5.0
7 years ago
8d69caed53
[CSharp-SimilarProducts] Update to Microsoft.Bot.Builder v3.5.0
7 years ago
764b4f820b
[CSharp] Update screenshots emulator on all samples
7 years ago
f8ce953f42
Adding CSharp SimilarProducts bot sample.
7 years ago
8d69caed53
[CSharp-SimilarProducts] Update to Microsoft.Bot.Builder v3.5.0
7 years ago
f8ce953f42
Adding CSharp SimilarProducts bot sample.
7 years ago
8d69caed53
[CSharp-SimilarProducts] Update to Microsoft.Bot.Builder v3.5.0
7 years ago
6b3df2c0b3
Update documentation links to point to new docs (#118)
7 years ago
1cac51aaa5
Merge pull request #119
7 years ago
8d69caed53
[CSharp-SimilarProducts] Update to Microsoft.Bot.Builder v3.5.0
7 years ago
4801b87362
Merge pull request #50 from southworkscom/samples-updates
7 years ago
f8ce953f42
Adding CSharp SimilarProducts bot sample.
7 years ago
f8ce953f42
Adding CSharp SimilarProducts bot sample.
7 years ago
1cac51aaa5
Merge pull request #119
7 years ago

README.md

You have to be logged in to leave a comment. Sign In

# Similar Products Bot Sample

A sample bot that illustrates how to use the Bing Image Search API to find visually similar products from an image stream or a URL.Here's a demo of this bot in a web chat.

Deploy to Azure

Prerequisites

The minimum prerequisites to run this sample are:

  • The latest update of Visual Studio 2015. You can download the community version here for free.
  • The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.
  • This sample currently uses a free trial Microsoft Bing Search API key with limited QPS. Please subscribe here to obtain your own key and update the BingSearchApiKey key in Web.config file to try it out further.

Code Highlights

The main logic that handles calling the Bing Image Search API can be found in BingImageSearchService.cs.

The first method GetSimilarProductImagesAsync(string url) illustrates how to get the list of visually similar product from a URL. The image URL is encoded and sent in the get request query parameters (imgUrl):

/// <summary>
/// Gets a list of visually similar products from an image URL.
/// </summary>
/// <param name="url">The URL of an image.</param>
/// <returns>List of visually similar images.</returns>
public async Task<IList<ImageResult>> GetSimilarProductImagesAsync(string url)
{
    using (var httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ApiKey);

        string apiUrl = BingApiUrl + $"&imgUrl={HttpUtility.UrlEncode(url)}";

        var text = await httpClient.GetStringAsync(apiUrl);
        var response = JsonConvert.DeserializeObject<BingImageResponse>(text);

        return response?.VisuallySimilarProducts?.Select(i => new ImageResult
            {
                HostPageDisplayUrl = i.HostPageDisplayUrl,
                HostPageUrl = i.HostPageUrl,
                Name = i.Name,
                ThumbnailUrl = i.ThumbnailUrl,
                WebSearchUrl = i.WebSearchUrl
            })
            .ToList();
    }
}

The second method GetSimilarProductImagesAsync(Stream stream) illustrates how to get the list of visually similar product from a stream. The main difference here is that the image content is posted in the request body:

/// <summary>
/// Gets a list of visually similar products from an image stream.
/// </summary>
/// <param name="stream">The stream to an image.</param>
/// <returns>List of visually similar images.</returns>
public async Task<IList<ImageResult>> GetSimilarProductImagesAsync(Stream stream)
{
    using (var httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ApiKey);

        var strContent = new StreamContent(stream);
        strContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { FileName = "Any-Name-Works" };

        var content = new MultipartFormDataContent();
        content.Add(strContent);

        var postResponse = await httpClient.PostAsync(BingApiUrl, content);
        var text = await postResponse.Content.ReadAsStringAsync();
        var response = JsonConvert.DeserializeObject<BingImageResponse>(text);

        return response?.VisuallySimilarProducts?.Select(i => new ImageResult
            {
                HostPageDisplayUrl = i.HostPageDisplayUrl,
                HostPageUrl = i.HostPageUrl,
                Name = i.Name,
                ThumbnailUrl = i.ThumbnailUrl,
                WebSearchUrl = i.WebSearchUrl
            })
            .ToList();
    }
}

Outcome

You will see the following when connecting the Bot to the Emulator and send it an image or a URL:

Input:

Sample Outcome

Output:

Sample Outcome

More Information

To get more information about how to get started in Bot Builder for .NET and Microsoft Bing Images Search API please review the following resources:

Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...