Files
annotations/Azaion.Common/Services/InferenceService.cs
T
Alex Bezdieniezhnykh 739759628a fixed inference bugs
add DONE during inference, correct handling on C# side
2025-02-01 02:09:11 +02:00

48 lines
1.6 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.Logging;
using Microsoft.Extensions.Options;
using NetMQ;
using NetMQ.Sockets;
namespace Azaion.Common.Services;
public interface IInferenceService
{
Task RunInference(string mediaPath, Func<AnnotationImage, Task> processAnnotation);
}
public class PythonInferenceService(ILogger<PythonInferenceService> logger, IOptions<AIRecognitionConfig> aiConfigOptions) : IInferenceService
{
public async Task RunInference(string mediaPath, Func<AnnotationImage, Task> processAnnotation)
{
using var dealer = new DealerSocket();
var clientId = Guid.NewGuid();
dealer.Options.Identity = Encoding.UTF8.GetBytes(clientId.ToString("N"));
dealer.Connect($"tcp://{SecurityConstants.ZMQ_HOST}:{SecurityConstants.ZMQ_PORT}");
var data = MessagePackSerializer.Serialize(aiConfigOptions.Value);
dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.Inference, mediaPath, data)));
while (true)
{
try
{
var annotationStream = dealer.Get<AnnotationImage>(bytes => bytes.Length == 4 && Encoding.UTF8.GetString(bytes) == "DONE");
if (annotationStream == null)
break;
await processAnnotation(annotationStream);
}
catch (Exception e)
{
logger.LogError(e, e.Message);
}
}
}
}