mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 22:26:31 +00:00
0c66607ed7
add user config queue offsets throttle improvements
57 lines
2.0 KiB
C#
57 lines
2.0 KiB
C#
using System.Text;
|
|
using Azaion.Common.Database;
|
|
using Azaion.Common.DTO.Config;
|
|
using Azaion.CommonSecurity;
|
|
using Azaion.CommonSecurity.DTO.Commands;
|
|
using Azaion.CommonSecurity.Services;
|
|
using MessagePack;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Azaion.Common.Services;
|
|
|
|
public interface IInferenceService
|
|
{
|
|
Task RunInference(List<string> mediaPaths, Func<AnnotationImage, Task> processAnnotation, CancellationToken detectToken = default);
|
|
void StopInference();
|
|
}
|
|
|
|
public class InferenceService(ILogger<InferenceService> logger, IInferenceClient client, IAzaionApi azaionApi, IOptions<AIRecognitionConfig> aiConfigOptions) : IInferenceService
|
|
{
|
|
public async Task RunInference(List<string> mediaPaths, Func<AnnotationImage, Task> processAnnotation, CancellationToken detectToken = default)
|
|
{
|
|
client.Send(RemoteCommand.Create(CommandType.Login, azaionApi.Credentials));
|
|
var aiConfig = aiConfigOptions.Value;
|
|
|
|
aiConfig.Paths = mediaPaths;
|
|
client.Send(RemoteCommand.Create(CommandType.Inference, aiConfig));
|
|
|
|
while (!detectToken.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
var bytes = client.GetBytes(ct: detectToken);
|
|
if (bytes == null)
|
|
throw new Exception("Can't get bytes from inference client");
|
|
|
|
if (bytes.Length == 4 && Encoding.UTF8.GetString(bytes) == "DONE")
|
|
return;
|
|
|
|
var annotationImage = MessagePackSerializer.Deserialize<AnnotationImage>(bytes, cancellationToken: detectToken);
|
|
|
|
await processAnnotation(annotationImage);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, e.Message);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void StopInference()
|
|
{
|
|
client.Send(RemoteCommand.Create(CommandType.StopInference));
|
|
}
|
|
} |