diff --git a/CompatBot/Commands/Vision.cs b/CompatBot/Commands/Vision.cs new file mode 100644 index 00000000..0f0cc21f --- /dev/null +++ b/CompatBot/Commands/Vision.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using CompatBot.Commands.Attributes; +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Attributes; +using Microsoft.Azure.CognitiveServices.Vision.ComputerVision; +using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models; + +namespace CompatBot.Commands +{ + internal sealed class Vision: BaseCommandModuleCustom + { + [Command("describe"), RequiresBotModRole] + [Description("Generates an image description")] + public async Task Describe(CommandContext ctx, string imageUrl) + { + try + { + var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Config.AzureComputerVisionKey)) {Endpoint = Config.AzureComputerVisionEndpoint}; + var result = await client.AnalyzeImageAsync(imageUrl, new List {VisualFeatureTypes.Description}, cancellationToken: Config.Cts.Token).ConfigureAwait(false); + var captions = result.Description.Captions.OrderByDescending(c => c.Confidence).ToList(); + string msg; + if (captions.Any()) + { + var confidence = captions[0].Confidence switch + { + double v when v > 0.98 => "It is", + double v when v > 0.95 => "I'm pretty sure it is", + double v when v > 0.9 => "I'm quite sure it is", + double v when v > 0.8 => "I think it's", + double v when v > 0.5 => "I'm not very smart, so my best guess it's", + _ => "Ugh, idk? Might be", + }; + msg = $"{confidence} {captions[0].Text}"; +#if DEBUG + if (captions.Count > 1) + { + msg += "\nHowever, here are more guesses:\n"; + msg += string.Join('\n', captions.Skip(1).Select(c => c.Text)); + } +#endif + } + else + msg = "An image so weird, I have no words to describe it"; + await ctx.RespondAsync(msg).ConfigureAwait(false); + } + catch (Exception e) + { + Config.Log.Warn(e, "Failed to get image description"); + } + } + } +} diff --git a/CompatBot/CompatBot.csproj b/CompatBot/CompatBot.csproj index c7b2c070..d12fffff 100644 --- a/CompatBot/CompatBot.csproj +++ b/CompatBot/CompatBot.csproj @@ -28,11 +28,12 @@ - + + all diff --git a/CompatBot/Config.cs b/CompatBot/Config.cs index 38ff4b82..cbe2e501 100644 --- a/CompatBot/Config.cs +++ b/CompatBot/Config.cs @@ -60,6 +60,8 @@ namespace CompatBot public static TimeSpan IncomingMessageCheckIntervalInMinutes => TimeSpan.FromMinutes(config.GetValue(nameof(IncomingMessageCheckIntervalInMinutes), 10)); public static string Token => config.GetValue(nameof(Token), ""); public static string AzureDevOpsToken => config.GetValue(nameof(AzureDevOpsToken), ""); + public static string AzureComputerVisionKey => config.GetValue(nameof(AzureComputerVisionKey), ""); + public static string AzureComputerVisionEndpoint => config.GetValue(nameof(AzureComputerVisionEndpoint), "https://westeurope.api.cognitive.microsoft.com/"); public static string LogPath => config.GetValue(nameof(LogPath), "./logs/"); // paths are relative to the working directory public static string IrdCachePath => config.GetValue(nameof(IrdCachePath), "./ird/"); diff --git a/CompatBot/Program.cs b/CompatBot/Program.cs index 8c550fb8..72433666 100644 --- a/CompatBot/Program.cs +++ b/CompatBot/Program.cs @@ -153,6 +153,9 @@ namespace CompatBot commands.RegisterCommands(); commands.RegisterCommands(); + if (!string.IsNullOrEmpty(Config.AzureComputerVisionKey)) + commands.RegisterCommands(); + commands.CommandErrored += UnknownCommandHandler.OnError; var interactivityConfig = new InteractivityConfiguration { };