forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicrosoftCognitiveCaptionService.cs
75 lines (69 loc) · 3.15 KB
/
MicrosoftCognitiveCaptionService.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
namespace ImageCaption.Services
{
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Configuration;
using Microsoft.ProjectOxford.Vision;
using Microsoft.ProjectOxford.Vision.Contract;
/// <summary>
/// A wrapper around the Microsoft Cognitive Computer Vision API Service.
/// <remarks>
/// This class makes use of the Microsoft Computer Vision SDK.
/// SDK: https://github.com/Microsoft/ProjectOxford-ClientSDK/blob/master/Vision/Windows/ClientLibrary"
/// </remarks>
/// </summary>
public class MicrosoftCognitiveCaptionService : ICaptionService
{
/// <summary>
/// Microsoft Computer Vision API key.
/// </summary>
private static readonly string ApiKey = WebConfigurationManager.AppSettings["MicrosoftVisionApiKey"];
/// <summary>
/// The set of visual features we want from the Vision API.
/// </summary>
private static readonly VisualFeature[] VisualFeatures = { VisualFeature.Description };
/// <summary>
/// Gets the caption of an image URL.
/// <remarks>
/// This method calls <see cref="IVisionServiceClient.AnalyzeImageAsync(string, string[])"/> and
/// returns the first caption from the returned <see cref="AnalysisResult.Description"/>
/// </remarks>
/// </summary>
/// <param name="url">The URL to an image.</param>
/// <returns>Description if caption found, null otherwise.</returns>
public async Task<string> GetCaptionAsync(string url)
{
var client = new VisionServiceClient(ApiKey);
var result = await client.AnalyzeImageAsync(url, VisualFeatures);
return ProcessAnalysisResult(result);
}
/// <summary>
/// Gets the caption of the image from an image stream.
/// <remarks>
/// This method calls <see cref="IVisionServiceClient.AnalyzeImageAsync(Stream, string[])"/> and
/// returns the first caption from the returned <see cref="AnalysisResult.Description"/>
/// </remarks>
/// </summary>
/// <param name="stream">The stream to an image.</param>
/// <returns>Description if caption found, null otherwise.</returns>
public async Task<string> GetCaptionAsync(Stream stream)
{
var client = new VisionServiceClient(ApiKey);
var result = await client.AnalyzeImageAsync(stream, VisualFeatures);
return ProcessAnalysisResult(result);
}
/// <summary>
/// Processes the analysis result.
/// </summary>
/// <param name="result">The result.</param>
/// <returns>The caption if found, error message otherwise.</returns>
private static string ProcessAnalysisResult(AnalysisResult result)
{
string message = result?.Description?.Captions.FirstOrDefault()?.Text;
return string.IsNullOrEmpty(message) ?
"Couldn't find a caption for this one" :
"I think it's " + message;
}
}
}