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

image-service.js 2.5 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
  1. // The exported functions in this module makes a call to Bing Image Search API returns similar products description if found.
  2. // Note: you can do more functionalities like recognizing entities. For more info checkout the API reference:
  3. // https://msdn.microsoft.com/en-us/library/dn760791.aspx
  4. var request = require('request').defaults({ encoding: null });
  5. var BING_API_URL = 'https://api.cognitive.microsoft.com/bing/v5.0/images/search?modulesRequested=SimilarProducts&mkt=en-us&form=BCSPRD';
  6. var BING_SEARCH_API_KEY = process.env.BING_SEARCH_API_KEY;
  7. /**
  8. * Gets the similar products of the image from an image stream
  9. * @param {stream} stream The stream to an image.
  10. * @return {Promise} Promise with visuallySimilarProducts array if succeeded, error otherwise
  11. */
  12. exports.getSimilarProductsFromStream = function (stream) {
  13. return new Promise(
  14. function (resolve, reject) {
  15. var requestData = {
  16. url: BING_API_URL,
  17. encoding: 'binary',
  18. formData: {
  19. file: stream
  20. },
  21. headers: {
  22. 'Ocp-Apim-Subscription-Key': BING_SEARCH_API_KEY
  23. }
  24. };
  25. request.post(requestData, function (error, response, body) {
  26. if (error) {
  27. reject(error);
  28. }
  29. else if (response.statusCode !== 200) {
  30. reject(body);
  31. }
  32. else {
  33. resolve(JSON.parse(body).visuallySimilarProducts);
  34. }
  35. });
  36. }
  37. );
  38. };
  39. /**
  40. * Gets the similar products of the image from an image URL
  41. * @param {string} url The URL to an image.
  42. * @return {Promise} Promise with visuallySimilarProducts array if succeeded, error otherwise
  43. */
  44. exports.getSimilarProductsFromUrl = function (url) {
  45. return new Promise(
  46. function (resolve, reject) {
  47. var requestData = {
  48. url: BING_API_URL + '&imgurl=' + url,
  49. headers: {
  50. 'Ocp-Apim-Subscription-Key': BING_SEARCH_API_KEY
  51. },
  52. json: true
  53. };
  54. request.get(requestData, function (error, response, body) {
  55. if (error) {
  56. reject(error);
  57. }
  58. else if (response.statusCode !== 200) {
  59. reject(body);
  60. }
  61. else {
  62. resolve(body.visuallySimilarProducts);
  63. }
  64. });
  65. }
  66. );
  67. };
Tip!

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

Comments

Loading...