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

caption-service.js 2.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
  1. // The exported functions in this module makes a call to Microsoft Cognitive Service Computer Vision API and return caption
  2. // description if found. Note: you can do more advanced functionalities like checking
  3. // the confidence score of the caption. For more info checkout the API documentation:
  4. // https://www.microsoft.com/cognitive-services/en-us/Computer-Vision-API/documentation/AnalyzeImage
  5. var request = require('request').defaults({ encoding: null });
  6. var VISION_URL = 'https://api.projectoxford.ai/vision/v1.0/analyze/?visualFeatures=Description&form=BCSIMG&subscription-key=' + process.env.MICROSOFT_VISION_API_KEY;
  7. /**
  8. * Gets the caption of the image from an image stream
  9. * @param {stream} stream The stream to an image.
  10. * @return {Promise} Promise with caption string if succeeded, error otherwise
  11. */
  12. exports.getCaptionFromStream = function (stream) {
  13. return new Promise(
  14. function (resolve, reject) {
  15. var requestData = {
  16. url: VISION_URL,
  17. encoding: 'binary',
  18. headers: { 'content-type': 'application/octet-stream' }
  19. };
  20. stream.pipe(request.post(requestData, function (error, response, body) {
  21. if (error) {
  22. reject(error);
  23. }
  24. else if (response.statusCode !== 200) {
  25. reject(body);
  26. }
  27. else {
  28. resolve(extractCaption(JSON.parse(body)));
  29. }
  30. }));
  31. }
  32. );
  33. };
  34. /**
  35. * Gets the caption of the image from an image URL
  36. * @param {string} url The URL to an image.
  37. * @return {Promise} Promise with caption string if succeeded, error otherwise
  38. */
  39. exports.getCaptionFromUrl = function (url) {
  40. return new Promise(
  41. function (resolve, reject) {
  42. var requestData = {
  43. url: VISION_URL,
  44. json: { 'url': url }
  45. };
  46. request.post(requestData, function (error, response, body) {
  47. if (error) {
  48. reject(error);
  49. }
  50. else if (response.statusCode !== 200) {
  51. reject(body);
  52. }
  53. else {
  54. resolve(extractCaption(body));
  55. }
  56. });
  57. }
  58. );
  59. };
  60. /**
  61. * Extracts the caption description from the response of the Vision API
  62. * @param {Object} body Response of the Vision API
  63. * @return {string} Description if caption found, null otherwise.
  64. */
  65. function extractCaption(body) {
  66. if (body && body.description && body.description.captions && body.description.captions.length) {
  67. return body.description.captions[0].text;
  68. }
  69. return null;
  70. }
Tip!

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

Comments

Loading...