Files
annotations/Azaion.Annotator/AIDetector.cs
T
2024-10-25 00:17:24 +03:00

55 lines
1.8 KiB
C#

using System.Diagnostics;
using System.IO;
using Azaion.Annotator.DTO;
using Azaion.Annotator.Extensions;
using Compunet.YoloV8;
using LibVLCSharp.Shared;
using MediatR;
namespace Azaion.Annotator;
public class AIDetector(Config config, MediaPlayer mediaPlayer, VLCFrameExtractor frameExtractor)
: IRequestHandler<AIDetectEvent, List<YoloLabel>>
{
public async Task<List<YoloLabel>> Handle(AIDetectEvent request, CancellationToken cancellationToken)
{
using var predictor = new YoloPredictor(config.AIModelPath);
await frameExtractor.Start(async stream =>
{
stream.Seek(0, SeekOrigin.Begin);
var sw = Stopwatch.StartNew();
var result = await predictor.DetectAsync(stream);
sw.Stop();
var log = string.Join("|", result.Select(det =>
$"{det.Name.Id}.{det.Name.Name}: xy=({det.Bounds.X},{det.Bounds.Y}), size=({det.Bounds.Width}, {det.Bounds.Height}), Prob: {det.Confidence*100:F1}%"));
log += $". Inf time: {sw.ElapsedMilliseconds} ms";
Console.WriteLine(log);
});
while (mediaPlayer.IsPlaying)
{
try
{
// using var thumbnail = await mediaPlayer.Media.GenerateThumbnail(time: 200,
// speed: ThumbnailerSeekSpeed.Fast,
// width: 1280,
// height: resultHeight,
// crop: false,
// pictureType: PictureType.Argb)
//
// mediaPlayer.TakeSnapshot(0, TEMP_IMG, 1280, resultHeight);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
//var result = predictor.Detect();
}
return new List<YoloLabel>();
}
}