forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BingImageSearchService.cs
91 lines (80 loc) · 3.69 KB
/
BingImageSearchService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
namespace SimilarProducts.Services
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using System.Web.Configuration;
using Newtonsoft.Json;
/// <summary>
/// A client that handles the interactions with the Bing Image Search API.
/// <remarks>
/// </remarks>
/// </summary>
public class BingImageSearchService : IImageSearchService
{
/// <summary>
/// Microsoft Computer Vision API key.
/// </summary>
private static readonly string ApiKey = WebConfigurationManager.AppSettings["BingSearchApiKey"];
/// <summary>
/// The bing API URL.
/// </summary>
private static readonly string BingApiUrl = "https://api.cognitive.microsoft.com/bing/v5.0/images/search?modulesRequested=SimilarProducts&mkt=en-us&form=BCSPRD";
/// <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 products' 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();
}
}
/// <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();
}
}
}
}