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

MicrosoftCognitiveCaptionService.cs 3.1 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
  1. namespace ImageCaption.Services
  2. {
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Web.Configuration;
  7. using Microsoft.ProjectOxford.Vision;
  8. using Microsoft.ProjectOxford.Vision.Contract;
  9. /// <summary>
  10. /// A wrapper around the Microsoft Cognitive Computer Vision API Service.
  11. /// <remarks>
  12. /// This class makes use of the Microsoft Computer Vision SDK.
  13. /// SDK: https://github.com/Microsoft/ProjectOxford-ClientSDK/blob/master/Vision/Windows/ClientLibrary"
  14. /// </remarks>
  15. /// </summary>
  16. public class MicrosoftCognitiveCaptionService : ICaptionService
  17. {
  18. /// <summary>
  19. /// Microsoft Computer Vision API key.
  20. /// </summary>
  21. private static readonly string ApiKey = WebConfigurationManager.AppSettings["MicrosoftVisionApiKey"];
  22. /// <summary>
  23. /// The set of visual features we want from the Vision API.
  24. /// </summary>
  25. private static readonly VisualFeature[] VisualFeatures = { VisualFeature.Description };
  26. /// <summary>
  27. /// Gets the caption of an image URL.
  28. /// <remarks>
  29. /// This method calls <see cref="IVisionServiceClient.AnalyzeImageAsync(string, string[])"/> and
  30. /// returns the first caption from the returned <see cref="AnalysisResult.Description"/>
  31. /// </remarks>
  32. /// </summary>
  33. /// <param name="url">The URL to an image.</param>
  34. /// <returns>Description if caption found, null otherwise.</returns>
  35. public async Task<string> GetCaptionAsync(string url)
  36. {
  37. var client = new VisionServiceClient(ApiKey);
  38. var result = await client.AnalyzeImageAsync(url, VisualFeatures);
  39. return ProcessAnalysisResult(result);
  40. }
  41. /// <summary>
  42. /// Gets the caption of the image from an image stream.
  43. /// <remarks>
  44. /// This method calls <see cref="IVisionServiceClient.AnalyzeImageAsync(Stream, string[])"/> and
  45. /// returns the first caption from the returned <see cref="AnalysisResult.Description"/>
  46. /// </remarks>
  47. /// </summary>
  48. /// <param name="stream">The stream to an image.</param>
  49. /// <returns>Description if caption found, null otherwise.</returns>
  50. public async Task<string> GetCaptionAsync(Stream stream)
  51. {
  52. var client = new VisionServiceClient(ApiKey);
  53. var result = await client.AnalyzeImageAsync(stream, VisualFeatures);
  54. return ProcessAnalysisResult(result);
  55. }
  56. /// <summary>
  57. /// Processes the analysis result.
  58. /// </summary>
  59. /// <param name="result">The result.</param>
  60. /// <returns>The caption if found, error message otherwise.</returns>
  61. private static string ProcessAnalysisResult(AnalysisResult result)
  62. {
  63. string message = result?.Description?.Captions.FirstOrDefault()?.Text;
  64. return string.IsNullOrEmpty(message) ?
  65. "Couldn't find a caption for this one" :
  66. "I think it's " + message;
  67. }
  68. }
  69. }
Tip!

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

Comments

Loading...