Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

BingImageSearchService.cs 3.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. namespace SimilarProducts.Services
  2. {
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10. using System.Web.Configuration;
  11. using Newtonsoft.Json;
  12. /// <summary>
  13. /// A client that handles the interactions with the Bing Image Search API.
  14. /// <remarks>
  15. /// </remarks>
  16. /// </summary>
  17. public class BingImageSearchService : IImageSearchService
  18. {
  19. /// <summary>
  20. /// Microsoft Computer Vision API key.
  21. /// </summary>
  22. private static readonly string ApiKey = WebConfigurationManager.AppSettings["BingSearchApiKey"];
  23. /// <summary>
  24. /// The bing API URL.
  25. /// </summary>
  26. private static readonly string BingApiUrl = "https://api.cognitive.microsoft.com/bing/v5.0/images/search?modulesRequested=SimilarProducts&mkt=en-us&form=BCSPRD";
  27. /// <summary>
  28. /// Gets a list of visually similar products from an image URL.
  29. /// </summary>
  30. /// <param name="url">The URL of an image.</param>
  31. /// <returns>List of visually similar products' images.</returns>
  32. public async Task<IList<ImageResult>> GetSimilarProductImagesAsync(string url)
  33. {
  34. using (var httpClient = new HttpClient())
  35. {
  36. httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ApiKey);
  37. string apiUrl = BingApiUrl + $"&imgUrl={HttpUtility.UrlEncode(url)}";
  38. var text = await httpClient.GetStringAsync(apiUrl);
  39. var response = JsonConvert.DeserializeObject<BingImageResponse>(text);
  40. return response?.VisuallySimilarProducts?.Select(i => new ImageResult
  41. {
  42. HostPageDisplayUrl = i.HostPageDisplayUrl,
  43. HostPageUrl = i.HostPageUrl,
  44. Name = i.Name,
  45. ThumbnailUrl = i.ThumbnailUrl,
  46. WebSearchUrl = i.WebSearchUrl
  47. })
  48. .ToList();
  49. }
  50. }
  51. /// <summary>
  52. /// Gets a list of visually similar products from an image stream.
  53. /// </summary>
  54. /// <param name="stream">The stream to an image.</param>
  55. /// <returns>List of visually similar images.</returns>
  56. public async Task<IList<ImageResult>> GetSimilarProductImagesAsync(Stream stream)
  57. {
  58. using (var httpClient = new HttpClient())
  59. {
  60. httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ApiKey);
  61. var strContent = new StreamContent(stream);
  62. strContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { FileName = "Any-Name-Works" };
  63. var content = new MultipartFormDataContent();
  64. content.Add(strContent);
  65. var postResponse = await httpClient.PostAsync(BingApiUrl, content);
  66. var text = await postResponse.Content.ReadAsStringAsync();
  67. var response = JsonConvert.DeserializeObject<BingImageResponse>(text);
  68. return response?.VisuallySimilarProducts?.Select(i => new ImageResult
  69. {
  70. HostPageDisplayUrl = i.HostPageDisplayUrl,
  71. HostPageUrl = i.HostPageUrl,
  72. Name = i.Name,
  73. ThumbnailUrl = i.ThumbnailUrl,
  74. WebSearchUrl = i.WebSearchUrl
  75. })
  76. .ToList();
  77. }
  78. }
  79. }
  80. }
Tip!

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

Comments

Loading...