azure computer vision integration, and a new command to describe images

This commit is contained in:
13xforever 2020-03-08 17:29:17 +05:00
parent 3575e605cd
commit 5ef02db745
4 changed files with 62 additions and 1 deletions

View File

@ -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> {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");
}
}
}
}

View File

@ -28,11 +28,12 @@
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.0.0-nightly-00662" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.0.0-nightly-00662" />
<PackageReference Include="DuoVia.FuzzyStrings" Version="2.0.1" />
<PackageReference Include="Google.Apis.Drive.v3" Version="1.44.0.1863" />
<PackageReference Include="Google.Apis.Drive.v3" Version="1.44.1.1876" />
<PackageReference Include="ksemenenko.ColorThief" Version="1.1.1.4" />
<PackageReference Include="MathParser.org-mXparser" Version="4.4.2" />
<PackageReference Include="MegaApiClient" Version="1.7.1" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.Azure.CognitiveServices.Vision.ComputerVision" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>

View File

@ -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/");

View File

@ -153,6 +153,9 @@ namespace CompatBot
commands.RegisterCommands<Syscall>();
commands.RegisterCommands<ForcedNicknames>();
if (!string.IsNullOrEmpty(Config.AzureComputerVisionKey))
commands.RegisterCommands<Vision>();
commands.CommandErrored += UnknownCommandHandler.OnError;
var interactivityConfig = new InteractivityConfiguration { };