mirror of
https://github.com/unclshura/splitter.git
synced 2026-06-22 00:22:01 +00:00
Compare commits
No commits in common. "master" and "v0.1.1" have entirely different histories.
@ -1,4 +1,5 @@
|
|||||||
using Avalonia;
|
using System;
|
||||||
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Input;
|
using Avalonia.Input;
|
||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
|
|||||||
@ -12,7 +12,10 @@ internal sealed class Program
|
|||||||
[STAThread]
|
[STAThread]
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
BuildAvaloniaApp()
|
var services = ConfigureServices();
|
||||||
|
var provider = services.BuildServiceProvider();
|
||||||
|
|
||||||
|
BuildAvaloniaApp(provider)
|
||||||
.StartWithClassicDesktopLifetime(args);
|
.StartWithClassicDesktopLifetime(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,11 +37,8 @@ internal sealed class Program
|
|||||||
// splitter services
|
// splitter services
|
||||||
services.AddSingleton<UltraFaceDetector>();
|
services.AddSingleton<UltraFaceDetector>();
|
||||||
services.AddSingleton<YoloV10ObjectDetector>();
|
services.AddSingleton<YoloV10ObjectDetector>();
|
||||||
services.AddSingleton<DummyDetector>();
|
|
||||||
services.AddSingleton<OSNetEmbeddingExtractor>();
|
services.AddSingleton<OSNetEmbeddingExtractor>();
|
||||||
services.AddSingleton<IObjectTracker, ObjectTracker>();
|
services.AddSingleton<IObjectTracker, ObjectTracker>();
|
||||||
services.AddSingleton<IBufferPool, BufferPool>();
|
|
||||||
services.AddSingleton<IMatToBitmapConverter, MatToBitmapConverter>();
|
|
||||||
services.AddKeyedSingleton<IObjectDetector>("face", (x,_) => new SingleThreadedDetector<UltraFaceDetector>(x.GetRequiredService<UltraFaceDetector>()));
|
services.AddKeyedSingleton<IObjectDetector>("face", (x,_) => new SingleThreadedDetector<UltraFaceDetector>(x.GetRequiredService<UltraFaceDetector>()));
|
||||||
services.AddKeyedSingleton<IObjectDetector>("body", (x,_) => new SingleThreadedDetector<YoloV10ObjectDetector>(x.GetRequiredService<YoloV10ObjectDetector>()));
|
services.AddKeyedSingleton<IObjectDetector>("body", (x,_) => new SingleThreadedDetector<YoloV10ObjectDetector>(x.GetRequiredService<YoloV10ObjectDetector>()));
|
||||||
services.AddKeyedSingleton<IObjectDetector>("none", (x,_) => new SingleThreadedDetector<DummyDetector>(x.GetRequiredService<DummyDetector>()));
|
services.AddKeyedSingleton<IObjectDetector>("none", (x,_) => new SingleThreadedDetector<DummyDetector>(x.GetRequiredService<DummyDetector>()));
|
||||||
@ -64,12 +64,8 @@ internal sealed class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Avalonia configuration, don't remove; also used by visual designer.
|
// Avalonia configuration, don't remove; also used by visual designer.
|
||||||
public static AppBuilder BuildAvaloniaApp()
|
public static AppBuilder BuildAvaloniaApp(ServiceProvider provider)
|
||||||
{
|
=> AppBuilder.Configure<App>(() => new App(provider))
|
||||||
var services = ConfigureServices();
|
|
||||||
var provider = services.BuildServiceProvider();
|
|
||||||
|
|
||||||
return AppBuilder.Configure<App>(() => new App(provider))
|
|
||||||
.UsePlatformDetect()
|
.UsePlatformDetect()
|
||||||
.With(new FontManagerOptions
|
.With(new FontManagerOptions
|
||||||
{
|
{
|
||||||
@ -85,4 +81,3 @@ internal sealed class Program
|
|||||||
.WithInterFont()
|
.WithInterFont()
|
||||||
.LogToTrace();
|
.LogToTrace();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
@ -1,59 +0,0 @@
|
|||||||
namespace Splitter_UI.Services;
|
|
||||||
|
|
||||||
public sealed class BufferPool : IBufferPool
|
|
||||||
{
|
|
||||||
private readonly int _capacity;
|
|
||||||
|
|
||||||
public sealed class Entry
|
|
||||||
{
|
|
||||||
public readonly int Width;
|
|
||||||
public readonly int Height;
|
|
||||||
public readonly byte[] Bgr;
|
|
||||||
public readonly byte[] Bgra;
|
|
||||||
|
|
||||||
public Entry(int w, int h)
|
|
||||||
{
|
|
||||||
Width = w;
|
|
||||||
Height = h;
|
|
||||||
Bgr = new byte[w * h * 3];
|
|
||||||
Bgra = new byte[w * h * 4];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly Dictionary<(int w, int h), LinkedListNode<Entry>> _map;
|
|
||||||
private readonly LinkedList<Entry> _lru;
|
|
||||||
|
|
||||||
public BufferPool()
|
|
||||||
{
|
|
||||||
_capacity = 8;
|
|
||||||
_map = new Dictionary<(int w, int h), LinkedListNode<Entry>>(_capacity);
|
|
||||||
_lru = new LinkedList<Entry>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Entry Get(int w, int h)
|
|
||||||
{
|
|
||||||
var key = (w, h);
|
|
||||||
|
|
||||||
if (_map.TryGetValue(key, out var node))
|
|
||||||
{
|
|
||||||
_lru.Remove(node);
|
|
||||||
_lru.AddLast(node);
|
|
||||||
return node.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
var created = new Entry(w, h);
|
|
||||||
var newNode = new LinkedListNode<Entry>(created);
|
|
||||||
|
|
||||||
_lru.AddLast(newNode);
|
|
||||||
_map[key] = newNode;
|
|
||||||
|
|
||||||
if (_lru.Count > _capacity)
|
|
||||||
{
|
|
||||||
var first = _lru.First!;
|
|
||||||
_lru.RemoveFirst();
|
|
||||||
_map.Remove((first.Value.Width, first.Value.Height));
|
|
||||||
}
|
|
||||||
|
|
||||||
return created;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
namespace Splitter_UI.Services;
|
|
||||||
|
|
||||||
public interface IBufferPool
|
|
||||||
{
|
|
||||||
BufferPool.Entry Get(int w, int h);
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
using Avalonia.Media.Imaging;
|
|
||||||
|
|
||||||
namespace Splitter_UI.Services;
|
|
||||||
|
|
||||||
public interface IMatToBitmapConverter
|
|
||||||
{
|
|
||||||
Bitmap Convert(Mat mat, Bitmap? existing = null);
|
|
||||||
Bitmap Convert(byte[] bgr, int width, int height, Bitmap? existing = null);
|
|
||||||
}
|
|
||||||
@ -1,136 +0,0 @@
|
|||||||
using Avalonia;
|
|
||||||
using Avalonia.Media.Imaging;
|
|
||||||
using Avalonia.Platform;
|
|
||||||
|
|
||||||
namespace Splitter_UI.Services;
|
|
||||||
|
|
||||||
public sealed class MatToBitmapConverter(IBufferPool _pool) : IMatToBitmapConverter
|
|
||||||
{
|
|
||||||
private readonly object _sync = new();
|
|
||||||
|
|
||||||
public Bitmap Convert(Mat mat, Bitmap? existing = null)
|
|
||||||
{
|
|
||||||
if (mat.Empty())
|
|
||||||
throw new ArgumentException("Mat is empty.", nameof(mat));
|
|
||||||
|
|
||||||
var w = mat.Width;
|
|
||||||
var h = mat.Height;
|
|
||||||
var channels = mat.Channels();
|
|
||||||
|
|
||||||
if (channels != 3 && channels != 4)
|
|
||||||
throw new NotSupportedException($"Only 3 or 4 channel Mats are supported. Got {channels}.");
|
|
||||||
|
|
||||||
lock (_sync)
|
|
||||||
{
|
|
||||||
var entry = _pool.Get(w, h);
|
|
||||||
|
|
||||||
var src = mat;
|
|
||||||
if (!src.IsContinuous())
|
|
||||||
src = src.Clone();
|
|
||||||
|
|
||||||
unsafe
|
|
||||||
{
|
|
||||||
var srcPtr = (byte*)src.DataPointer;
|
|
||||||
var totalBytes = w * h * channels;
|
|
||||||
|
|
||||||
if (channels == 3)
|
|
||||||
{
|
|
||||||
fixed (byte* dstBgr = entry.Bgr)
|
|
||||||
{
|
|
||||||
Buffer.MemoryCopy(srcPtr, dstBgr, entry.Bgr.Length, totalBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
ConvertBgrToBgra(entry.Bgr, entry.Bgra, w, h);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fixed (byte* dstBgra = entry.Bgra)
|
|
||||||
{
|
|
||||||
Buffer.MemoryCopy(srcPtr, dstBgra, entry.Bgra.Length, totalBytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (existing is WriteableBitmap wb &&
|
|
||||||
wb.PixelSize.Width == w &&
|
|
||||||
wb.PixelSize.Height == h)
|
|
||||||
{
|
|
||||||
UpdateWriteableBitmap(wb, entry.Bgra, w, h);
|
|
||||||
return wb;
|
|
||||||
}
|
|
||||||
|
|
||||||
return CreateBitmap(entry.Bgra, w, h);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Bitmap Convert(byte[] bgr, int width, int height, Bitmap? existing = null)
|
|
||||||
{
|
|
||||||
var entry = _pool.Get(width, height);
|
|
||||||
ConvertBgrToBgra(bgr, entry.Bgra, width, height);
|
|
||||||
|
|
||||||
if (existing is WriteableBitmap wb &&
|
|
||||||
wb.PixelSize.Width == width &&
|
|
||||||
wb.PixelSize.Height == height)
|
|
||||||
{
|
|
||||||
UpdateWriteableBitmap(wb, entry.Bgra, width, height);
|
|
||||||
return wb;
|
|
||||||
}
|
|
||||||
|
|
||||||
return CreateBitmap(entry.Bgra, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void ConvertBgrToBgra(byte[] bgr, byte[] bgra, int width, int height)
|
|
||||||
{
|
|
||||||
var si = 0;
|
|
||||||
var di = 0;
|
|
||||||
var totalPixels = width * height;
|
|
||||||
|
|
||||||
for (var i = 0; i < totalPixels; i++)
|
|
||||||
{
|
|
||||||
bgra[di + 0] = bgr[si + 0];
|
|
||||||
bgra[di + 1] = bgr[si + 1];
|
|
||||||
bgra[di + 2] = bgr[si + 2];
|
|
||||||
bgra[di + 3] = 255;
|
|
||||||
|
|
||||||
si += 3;
|
|
||||||
di += 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static unsafe void UpdateWriteableBitmap(WriteableBitmap wb, byte[] bgra, int width, int height)
|
|
||||||
{
|
|
||||||
using var fb = wb.Lock();
|
|
||||||
|
|
||||||
var dstPtr = (byte*)fb.Address;
|
|
||||||
var dstStride = fb.RowBytes;
|
|
||||||
var srcStride = width * 4;
|
|
||||||
|
|
||||||
fixed (byte* srcPtr = bgra)
|
|
||||||
{
|
|
||||||
for (var y = 0; y < height; y++)
|
|
||||||
{
|
|
||||||
var srcRow = srcPtr + y * srcStride;
|
|
||||||
var dstRow = dstPtr + y * dstStride;
|
|
||||||
|
|
||||||
Buffer.MemoryCopy(srcRow, dstRow, dstStride, srcStride);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static unsafe Bitmap CreateBitmap(byte[] bgra, int width, int height)
|
|
||||||
{
|
|
||||||
var stride = width * 4;
|
|
||||||
|
|
||||||
fixed (byte* p = bgra)
|
|
||||||
{
|
|
||||||
return new WriteableBitmap(
|
|
||||||
PixelFormat.Bgra8888,
|
|
||||||
AlphaFormat.Premul,
|
|
||||||
(nint)p,
|
|
||||||
new PixelSize(width, height),
|
|
||||||
new Vector(96, 96),
|
|
||||||
stride);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +1,7 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using Avalonia;
|
||||||
using Avalonia.Media.Imaging;
|
using Avalonia.Media.Imaging;
|
||||||
|
using Avalonia.Platform;
|
||||||
|
|
||||||
namespace Splitter_UI.Services;
|
namespace Splitter_UI.Services;
|
||||||
|
|
||||||
@ -8,18 +10,10 @@ public sealed class ThumbnailService : IThumbnailService
|
|||||||
public const int ThumbWidth = 160;
|
public const int ThumbWidth = 160;
|
||||||
public const int ThumbHeight = 90;
|
public const int ThumbHeight = 90;
|
||||||
|
|
||||||
private readonly IMatToBitmapConverter _converter;
|
private readonly byte [] _bgrBuffer = new byte[ThumbWidth * ThumbHeight * 3];
|
||||||
private readonly IBufferPool _pool;
|
private readonly byte [] _bgraBuffer = new byte[ThumbWidth * ThumbHeight * 4];
|
||||||
|
|
||||||
private SemaphoreSlim _lock = new(1,1);
|
public SemaphoreSlim _lock = new(1,1);
|
||||||
|
|
||||||
public ThumbnailService(
|
|
||||||
IMatToBitmapConverter converter,
|
|
||||||
IBufferPool pool)
|
|
||||||
{
|
|
||||||
_converter = converter;
|
|
||||||
_pool = pool;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<Bitmap?> CreateThumbnailAsync(
|
public async Task<Bitmap?> CreateThumbnailAsync(
|
||||||
string file,
|
string file,
|
||||||
@ -59,33 +53,32 @@ public sealed class ThumbnailService : IThumbnailService
|
|||||||
height ??= ThumbHeight;
|
height ??= ThumbHeight;
|
||||||
skip ??= TimeSpan.Zero;
|
skip ??= TimeSpan.Zero;
|
||||||
|
|
||||||
var entry = _pool.Get(width.Value, height.Value);
|
// buffer for BGR24 → 3 bytes per pixel
|
||||||
|
|
||||||
var ok = await DecodeFrameAsync(
|
var canUseStaticBuffers =
|
||||||
entry.Bgr,
|
width.Value == ThumbWidth &&
|
||||||
file,
|
height.Value == ThumbHeight;
|
||||||
skip.Value,
|
|
||||||
width.Value,
|
|
||||||
height.Value,
|
|
||||||
rotateDegree
|
|
||||||
);
|
|
||||||
|
|
||||||
|
var bgrBuffer = canUseStaticBuffers ? _bgrBuffer : new byte[width.Value * height.Value * 3];
|
||||||
|
var bgraBuffer = canUseStaticBuffers ? _bgraBuffer : new byte[width.Value * height.Value * 4];
|
||||||
|
|
||||||
|
// Decode a single frame using ffmpeg → raw BGR24 into _bgrBuffer
|
||||||
|
var ok = await DecodeFrameAsync(bgrBuffer, file, skip.Value, width.Value, height.Value, rotateDegree);
|
||||||
if (!ok)
|
if (!ok)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return _converter.Convert(entry.Bgr, width.Value, height.Value);
|
// Convert BGR24 → BGRA32
|
||||||
|
ConvertBgrToBgra(bgrBuffer, bgraBuffer, width.Value, height.Value);
|
||||||
|
|
||||||
|
// Create Avalonia Bitmap
|
||||||
|
return CreateBitmap(bgraBuffer, width.Value, height.Value, rotateDegree == 90 || rotateDegree == 270);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<bool> DecodeFrameAsync(
|
private static async Task<bool> DecodeFrameAsync(byte [] bgrBuffer, string file, TimeSpan skip, int width, int height, int? rotateDegree)
|
||||||
byte[] bgrBuffer,
|
|
||||||
string file,
|
|
||||||
TimeSpan skip,
|
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
int? rotateDegree)
|
|
||||||
{
|
{
|
||||||
var rotationStr = TrackingSplitter.GetRorationArg(rotateDegree);
|
var rotationStr = TrackingSplitter.GetRorationArg(rotateDegree);
|
||||||
|
|
||||||
|
// ffmpeg command: decode one frame, resize, output raw BGR24
|
||||||
var args =
|
var args =
|
||||||
$"-ss {skip.TotalSeconds} -t 0.1 -i \"{file}\" " +
|
$"-ss {skip.TotalSeconds} -t 0.1 -i \"{file}\" " +
|
||||||
"-an -sn " +
|
"-an -sn " +
|
||||||
@ -130,4 +123,45 @@ public sealed class ThumbnailService : IThumbnailService
|
|||||||
{
|
{
|
||||||
try { p.Kill(); } catch { }
|
try { p.Kill(); } catch { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ConvertBgrToBgra(byte[] bgr, byte[] bgra, int width, int height)
|
||||||
|
{
|
||||||
|
var si = 0;
|
||||||
|
var di = 0;
|
||||||
|
|
||||||
|
var totalPixels = width * height;
|
||||||
|
|
||||||
|
for (var i = 0; i < totalPixels; i++)
|
||||||
|
{
|
||||||
|
bgra[di + 0] = bgr[si + 0]; // B
|
||||||
|
bgra[di + 1] = bgr[si + 1]; // G
|
||||||
|
bgra[di + 2] = bgr[si + 2]; // R
|
||||||
|
bgra[di + 3] = 255; // A
|
||||||
|
|
||||||
|
si += 3;
|
||||||
|
di += 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static unsafe Bitmap CreateBitmap(byte[] bgra, int width, int height, bool isRotated)
|
||||||
|
{
|
||||||
|
if (isRotated)
|
||||||
|
{
|
||||||
|
(height, width) = (width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
var stride = width * 4;
|
||||||
|
|
||||||
|
fixed (byte* p = bgra)
|
||||||
|
{
|
||||||
|
return new Bitmap(
|
||||||
|
PixelFormat.Bgra8888,
|
||||||
|
AlphaFormat.Premul,
|
||||||
|
(nint)p,
|
||||||
|
new PixelSize(width, height),
|
||||||
|
new Vector(96, 96),
|
||||||
|
stride);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,17 +18,17 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Avalonia" Version="12.0.4" />
|
<PackageReference Include="Avalonia" Version="12.0.3" />
|
||||||
<PackageReference Include="Avalonia.Controls.DataGrid" Version="12.0.0" />
|
<PackageReference Include="Avalonia.Controls.DataGrid" Version="12.0.0" />
|
||||||
<PackageReference Include="Avalonia.Desktop" Version="12.0.4" />
|
<PackageReference Include="Avalonia.Desktop" Version="12.0.3" />
|
||||||
<PackageReference Include="Avalonia.Themes.Fluent" Version="12.0.4" />
|
<PackageReference Include="Avalonia.Themes.Fluent" Version="12.0.3" />
|
||||||
<PackageReference Include="Avalonia.Fonts.Inter" Version="12.0.4" />
|
<PackageReference Include="Avalonia.Fonts.Inter" Version="12.0.3" />
|
||||||
<PackageReference Include="AvaloniaUI.DiagnosticsSupport" Version="2.2.3">
|
<PackageReference Include="AvaloniaUI.DiagnosticsSupport" Version="2.2.1">
|
||||||
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
|
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
|
||||||
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
|
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.2" />
|
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.9" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.8" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -8,10 +8,11 @@ using CommunityToolkit.Mvvm.Input;
|
|||||||
|
|
||||||
namespace Splitter_UI.ViewModels;
|
namespace Splitter_UI.ViewModels;
|
||||||
|
|
||||||
|
public record Segment(double Start, double End);
|
||||||
|
|
||||||
public partial class JobViewModel : ObservableObject
|
public partial class JobViewModel : ObservableObject
|
||||||
{
|
{
|
||||||
private SingleJob Job => _f.Model;
|
private SingleJob Job { get; }
|
||||||
private ViewModelForwarder<SingleJob> _f;
|
|
||||||
|
|
||||||
public SingleJob GetJob() => Job;
|
public SingleJob GetJob() => Job;
|
||||||
|
|
||||||
@ -54,7 +55,6 @@ public partial class JobViewModel : ObservableObject
|
|||||||
|
|
||||||
public IRelayCommand StepForwardCommand { get; }
|
public IRelayCommand StepForwardCommand { get; }
|
||||||
public IRelayCommand StepBackwardCommand { get; }
|
public IRelayCommand StepBackwardCommand { get; }
|
||||||
public IRelayCommand PlayPreviewCommand { get; }
|
|
||||||
|
|
||||||
private readonly IThumbnailService _thumbnails;
|
private readonly IThumbnailService _thumbnails;
|
||||||
private readonly DispatcherTimer _debounceTimer;
|
private readonly DispatcherTimer _debounceTimer;
|
||||||
@ -129,6 +129,115 @@ public partial class JobViewModel : ObservableObject
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string? Detect
|
||||||
|
{
|
||||||
|
get => Job.Detect;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (Job.Detect == value)
|
||||||
|
return;
|
||||||
|
Job.Detect = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public float ScoreThreshold
|
||||||
|
{
|
||||||
|
get => Job.ScoreThreshold;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (Math.Abs(Job.ScoreThreshold - value) < 0.001)
|
||||||
|
return;
|
||||||
|
Job.ScoreThreshold = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
Task.Run(CreatePreview);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public float IdentityThreshold
|
||||||
|
{
|
||||||
|
get => Job.IdentityThreshold;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (Math.Abs(Job.IdentityThreshold - value) < 0.001)
|
||||||
|
return;
|
||||||
|
Job.IdentityThreshold = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
Task.Run(CreatePreview);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string? Mask
|
||||||
|
{
|
||||||
|
get => Job.Mask;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (Job.Mask == value)
|
||||||
|
return;
|
||||||
|
Job.Mask = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string OutputFolder
|
||||||
|
{
|
||||||
|
get => Job.OutputFolder;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (Job.OutputFolder == value)
|
||||||
|
return;
|
||||||
|
Job.OutputFolder = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ForceFixed
|
||||||
|
{
|
||||||
|
get => Job.ForceFixed;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (Job.ForceFixed == value)
|
||||||
|
return;
|
||||||
|
Job.ForceFixed = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Debug
|
||||||
|
{
|
||||||
|
get => Job.Debug;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (Job.Debug == value)
|
||||||
|
return;
|
||||||
|
Job.Debug = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Enhance
|
||||||
|
{
|
||||||
|
get => Job.Enhance;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (Job.Enhance == value)
|
||||||
|
return;
|
||||||
|
Job.Enhance = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int? Rotate
|
||||||
|
{
|
||||||
|
get => Job.Rotate;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
Job.Rotate = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
Task.Run(CreatePreview);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Point2f GravitateTo
|
public Point2f GravitateTo
|
||||||
{
|
{
|
||||||
get => Job.GravitateTo;
|
get => Job.GravitateTo;
|
||||||
@ -143,22 +252,47 @@ public partial class JobViewModel : ObservableObject
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string? Detect { get => Job.Detect; set => _f.Forward(value); }
|
public float DetectAbove
|
||||||
public string? Mask { get => Job.Mask; set => _f.Forward(value); }
|
{
|
||||||
public string OutputFolder { get => Job.OutputFolder; set => _f.Forward(value); }
|
get => Job.DetectAbove;
|
||||||
public bool ForceFixed { get => Job.ForceFixed; set => _f.Forward(value); }
|
set
|
||||||
public bool Debug { get => Job.Debug; set => _f.Forward(value); }
|
{
|
||||||
public bool Enhance { get => Job.Enhance; set => _f.Forward(value); }
|
if (Math.Abs(Job.DetectAbove - value) < 0.001 )
|
||||||
public double? OverrideTargetDuration { get => Job.OverrideTargetDuration; set => _f.Forward(value); }
|
return;
|
||||||
public float ScoreThreshold { get => Job.ScoreThreshold; set { _f.Forward(value); Task.Run(CreatePreview); } }
|
Job.DetectAbove = value;
|
||||||
public float IdentityThreshold { get => Job.IdentityThreshold; set { _f.Forward(value); Task.Run(CreatePreview); } }
|
OnPropertyChanged();
|
||||||
public int? Rotate { get => Job.Rotate; set { _f.Forward(value); Task.Run(CreatePreview); } }
|
Task.Run(CreatePreview);
|
||||||
public float DetectAbove { get => Job.DetectAbove; set { _f.Forward(value); Task.Run(CreatePreview); } }
|
}
|
||||||
public ulong? DetectId { get => Job.DetectId; set { _f.Forward(value); Task.Run(CreatePreview); } }
|
}
|
||||||
|
|
||||||
|
public ulong? DetectId
|
||||||
|
{
|
||||||
|
get => Job.DetectId;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (DetectId == value)
|
||||||
|
return;
|
||||||
|
Job.DetectId = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
Task.Run(CreatePreview);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public double? OverrideTargetDuration
|
||||||
|
{
|
||||||
|
get => Job.OverrideTargetDuration;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (Job.OverrideTargetDuration != null && value != null && Math.Abs(Job.OverrideTargetDuration.Value - value.Value) < 0.01)
|
||||||
|
return;
|
||||||
|
Job.OverrideTargetDuration = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public JobViewModel(SingleJob job, IThumbnailService thumbnails, Func<string, IObjectTracker> trackerFactory, ILogger log)
|
public JobViewModel(SingleJob job, IThumbnailService thumbnails, Func<string, IObjectTracker> trackerFactory, ILogger log)
|
||||||
{
|
{
|
||||||
_f = new ViewModelForwarder<SingleJob>(job, this.OnPropertyChanged);
|
Job = job;
|
||||||
_thumbnails = thumbnails;
|
_thumbnails = thumbnails;
|
||||||
_trackerFactory = trackerFactory;
|
_trackerFactory = trackerFactory;
|
||||||
_log = log;
|
_log = log;
|
||||||
@ -190,7 +324,6 @@ public partial class JobViewModel : ObservableObject
|
|||||||
|
|
||||||
StepForwardCommand = new RelayCommand(StepForward);
|
StepForwardCommand = new RelayCommand(StepForward);
|
||||||
StepBackwardCommand = new RelayCommand(StepBackward);
|
StepBackwardCommand = new RelayCommand(StepBackward);
|
||||||
PlayPreviewCommand = new RelayCommand(PlayPreview);
|
|
||||||
|
|
||||||
_debounceTimer = new DispatcherTimer
|
_debounceTimer = new DispatcherTimer
|
||||||
{
|
{
|
||||||
@ -357,11 +490,6 @@ public partial class JobViewModel : ObservableObject
|
|||||||
SliderLiveValue = Segments[current - 1].Start;
|
SliderLiveValue = Segments[current - 1].Start;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PlayPreview()
|
|
||||||
{
|
|
||||||
// Implementation for playing preview
|
|
||||||
}
|
|
||||||
|
|
||||||
private int GetCurrentSegment()
|
private int GetCurrentSegment()
|
||||||
{
|
{
|
||||||
double pos = SliderLiveValue;
|
double pos = SliderLiveValue;
|
||||||
|
|||||||
@ -68,7 +68,7 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
|
|
||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
{
|
{
|
||||||
var fileJobs = await _processor.GenerateJobs(file.GetJob(), false, file.Segments, _cancellationTokenSource.Token);
|
var fileJobs = await _processor.GenerateJobs(file.GetJob(), false, _cancellationTokenSource.Token);
|
||||||
jobs.AddRange(fileJobs);
|
jobs.AddRange(fileJobs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,34 +0,0 @@
|
|||||||
using System.Reflection;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Splitter_UI.ViewModels;
|
|
||||||
|
|
||||||
internal class ViewModelForwarder<TModel>
|
|
||||||
{
|
|
||||||
public readonly TModel Model;
|
|
||||||
private readonly Action<string> _onPropertyChanged;
|
|
||||||
|
|
||||||
public ViewModelForwarder(TModel model, Action<string> onPropertyChanged)
|
|
||||||
{
|
|
||||||
Model = model;
|
|
||||||
_onPropertyChanged = onPropertyChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Forward<T>(
|
|
||||||
T newValue,
|
|
||||||
[CallerMemberName] string? propertyName = null)
|
|
||||||
{
|
|
||||||
var modelType = typeof(TModel);
|
|
||||||
var prop = modelType.GetProperty(propertyName!, BindingFlags.Public | BindingFlags.Instance);
|
|
||||||
if (prop == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var oldValue = (T)prop.GetValue(Model)!;
|
|
||||||
|
|
||||||
if (EqualityComparer<T>.Default.Equals(oldValue, newValue))
|
|
||||||
return;
|
|
||||||
|
|
||||||
prop.SetValue(Model, newValue);
|
|
||||||
_onPropertyChanged(propertyName!);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -8,7 +8,7 @@
|
|||||||
x:DataType="vm:PreviewPaneViewModel">
|
x:DataType="vm:PreviewPaneViewModel">
|
||||||
|
|
||||||
<Border Background="#202020" Padding="10">
|
<Border Background="#202020" Padding="10">
|
||||||
<Grid RowDefinitions="*,Auto,Auto">
|
<Grid RowDefinitions="*,Auto">
|
||||||
|
|
||||||
<local:PreviewCanvas
|
<local:PreviewCanvas
|
||||||
Grid.Row="0"
|
Grid.Row="0"
|
||||||
@ -21,24 +21,6 @@
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
<Grid Grid.Row="1"
|
<Grid Grid.Row="1"
|
||||||
ColumnDefinitions="Auto"
|
|
||||||
Margin="0,10,0,0">
|
|
||||||
<Button Grid.Column="1"
|
|
||||||
HorizontalAlignment="Left"
|
|
||||||
Width="24" Height="24"
|
|
||||||
Padding="0"
|
|
||||||
Margin="0,0,5,0"
|
|
||||||
Command="{Binding Selected.PlayPreviewCommand}">
|
|
||||||
<TextBlock FontFamily="{StaticResource FontAwesome}"
|
|
||||||
Text=""
|
|
||||||
FontSize="12"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
HorizontalAlignment="Center" />
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<Grid Grid.Row="2"
|
|
||||||
ColumnDefinitions="Auto,*,Auto"
|
ColumnDefinitions="Auto,*,Auto"
|
||||||
Margin="0,10,0,0">
|
Margin="0,10,0,0">
|
||||||
|
|
||||||
@ -54,7 +36,20 @@
|
|||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
HorizontalAlignment="Center" />
|
HorizontalAlignment="Center" />
|
||||||
</Button>
|
</Button>
|
||||||
|
<!--
|
||||||
|
<Slider Grid.Column="1"
|
||||||
|
Minimum="0"
|
||||||
|
Maximum="{Binding Selected.DurationSeconds}"
|
||||||
|
Value="{Binding Selected.SliderLiveValue, Mode=TwoWay}"
|
||||||
|
Margin="5,0,5,0" />
|
||||||
|
|
||||||
|
<controls:PreviewSlider Grid.Column="1"
|
||||||
|
Minimum="0"
|
||||||
|
Maximum="{Binding Selected.DurationSeconds}"
|
||||||
|
Value="{Binding Selected.SliderLiveValue, Mode=TwoWay}"
|
||||||
|
SegmentDuration="{Binding Selected.SegmentDuration}"
|
||||||
|
Margin="5,0,5,0" />
|
||||||
|
-->
|
||||||
<controls:TimelinePreviewSlider Grid.Column="1"
|
<controls:TimelinePreviewSlider Grid.Column="1"
|
||||||
ViewModel="{Binding Selected}"
|
ViewModel="{Binding Selected}"
|
||||||
Margin="5,0,5,0" />
|
Margin="5,0,5,0" />
|
||||||
|
|||||||
@ -1,35 +0,0 @@
|
|||||||
namespace splitter;
|
|
||||||
|
|
||||||
public static class DebugOverlay
|
|
||||||
{
|
|
||||||
public static void DrawDebug(
|
|
||||||
Mat frame,
|
|
||||||
List<DetectedPerson> objects,
|
|
||||||
CameraController camera,
|
|
||||||
KalmanTracker kalman)
|
|
||||||
{
|
|
||||||
if (camera.ObjectBox.HasValue)
|
|
||||||
{
|
|
||||||
var fb = camera.ObjectBox.Value;
|
|
||||||
Cv2.Rectangle(frame, fb, Scalar.LimeGreen, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
Cv2.Circle(frame,
|
|
||||||
new Point((int)camera.SmoothedCenter.X, (int)camera.SmoothedCenter.Y),
|
|
||||||
6, Scalar.LimeGreen, -1);
|
|
||||||
|
|
||||||
Cv2.Rectangle(frame, camera.Roi,
|
|
||||||
camera.ObjectCenter.HasValue ? Scalar.Yellow : Scalar.Red, 3);
|
|
||||||
|
|
||||||
DrawText(frame, $"Faces: {objects.Count}", 20, 40, Scalar.White);
|
|
||||||
DrawText(frame, $"LostFrames: {camera.LostFrames}", 20, 70, Scalar.White);
|
|
||||||
DrawText(frame, $"Noise: {kalman.CurrentNoise:F3}", 20, 130, Scalar.White);
|
|
||||||
DrawText(frame, $"Camera: {camera.CameraCenter.X:F1},{camera.CameraCenter.Y:F1}", 20, 160, Scalar.White);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void DrawText(Mat img, string text, int x, int y, Scalar color)
|
|
||||||
{
|
|
||||||
Cv2.PutText(img, text, new Point(x, y),
|
|
||||||
HersheyFonts.HersheySimplex, 0.6, color, 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
public interface IJobProcessor
|
public interface IJobProcessor
|
||||||
{
|
{
|
||||||
Task<List<SingleTask>> GenerateJobs(SingleJob job, bool estimateOnly, IReadOnlyCollection<Segment> predefinedSegments, CancellationToken token);
|
Task<List<SingleTask>> GenerateJobs(SingleJob job, bool estimateOnly, CancellationToken token);
|
||||||
Task<bool> ProcessJobs(List<SingleTask> tasks, bool singleThreaded, CancellationToken token);
|
Task<bool> ProcessJobs(List<SingleTask> tasks, bool singleThreaded, CancellationToken token);
|
||||||
}
|
}
|
||||||
@ -5,7 +5,7 @@ namespace splitter;
|
|||||||
|
|
||||||
public class JobProcessor(ILogger logger) : LoggingBase(logger, 0), IJobProcessor
|
public class JobProcessor(ILogger logger) : LoggingBase(logger, 0), IJobProcessor
|
||||||
{
|
{
|
||||||
public async Task<List<SingleTask>> GenerateJobs(SingleJob job, bool estimateOnly, IReadOnlyCollection<Segment> predefinedSegments, CancellationToken token)
|
public async Task<List<SingleTask>> GenerateJobs(SingleJob job, bool estimateOnly, CancellationToken token)
|
||||||
{
|
{
|
||||||
var baseName = Path.GetFileNameWithoutExtension(job.InputFile);
|
var baseName = Path.GetFileNameWithoutExtension(job.InputFile);
|
||||||
|
|
||||||
@ -78,31 +78,24 @@ public class JobProcessor(ILogger logger) : LoggingBase(logger, 0), IJobProcesso
|
|||||||
processorFactory = i => new SimpleSplitter(i, _logger);
|
processorFactory = i => new SimpleSplitter(i, _logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
var segmentsToUse = predefinedSegments;
|
var jobs = Enumerable.Range(0, segments)
|
||||||
|
.Select(i => new SingleTask
|
||||||
if (predefinedSegments.Count == 0)
|
|
||||||
{
|
|
||||||
segmentsToUse = Enumerable.Range(0, segments).Select(i => new Segment
|
|
||||||
(
|
|
||||||
Start: i * segmentLength,
|
|
||||||
End : (i == segments - 1)
|
|
||||||
? Math.Max(0.1, info.Duration)
|
|
||||||
: (i + 1) * segmentLength
|
|
||||||
)).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
return segmentsToUse.Select((s, i) => new SingleTask
|
|
||||||
(
|
(
|
||||||
Job : job,
|
Job : job,
|
||||||
Info: info,
|
Info: info,
|
||||||
OutputFileName : BuildOutputFileName(job, i),
|
OutputFileName : BuildOutputFileName(job, i),
|
||||||
SegmentIndex : i,
|
SegmentIndex : i,
|
||||||
TotalSegments : predefinedSegments.Count,
|
TotalSegments : segments,
|
||||||
SegmentStart : s.Start,
|
SegmentStart : i * segmentLength,
|
||||||
SegmentLength : s.End - s.Start,
|
SegmentLength : (i == segments - 1)
|
||||||
|
? Math.Max(0.1, info.Duration - i * segmentLength)
|
||||||
|
: segmentLength,
|
||||||
ProcessorFactory : processorFactory
|
ProcessorFactory : processorFactory
|
||||||
)
|
)
|
||||||
).ToList();
|
)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return jobs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> ProcessJobs(List<SingleTask> tasks, bool singleThreaded, CancellationToken token)
|
public async Task<bool> ProcessJobs(List<SingleTask> tasks, bool singleThreaded, CancellationToken token)
|
||||||
|
|||||||
@ -3,189 +3,9 @@ using System.Globalization;
|
|||||||
|
|
||||||
namespace splitter;
|
namespace splitter;
|
||||||
|
|
||||||
public sealed class SimpleSplitter : LoggingBase, ISegmentProcessor
|
public class SimpleSplitter(int segmentNo, ILogger logger) : LoggingBase(logger, segmentNo), ISegmentProcessor
|
||||||
{
|
{
|
||||||
// ------------------------------------------------------------
|
|
||||||
// Internal state (opaque to caller)
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
|
|
||||||
private sealed class State : IFrameProcessingState
|
|
||||||
{
|
|
||||||
public Process? DecodeProcess { get; set; }
|
|
||||||
public Stream? DecodeStdout { get; set; }
|
|
||||||
|
|
||||||
public string InputFile { get; }
|
|
||||||
public double Start { get; }
|
|
||||||
public double Length { get; }
|
|
||||||
public int? Rotate { get; }
|
|
||||||
public string[] Passthrough { get; }
|
|
||||||
public VideoInfo Info { get; }
|
|
||||||
public bool PlainText { get; }
|
|
||||||
|
|
||||||
public State(SingleTask job)
|
|
||||||
{
|
|
||||||
InputFile = job.Job.InputFile;
|
|
||||||
Start = job.SegmentStart;
|
|
||||||
Length = job.SegmentLength;
|
|
||||||
Rotate = job.Job.Rotate;
|
|
||||||
Passthrough = job.Job.Passthrough;
|
|
||||||
Info = job.Info;
|
|
||||||
PlainText = job.Job.PlainText;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public SimpleSplitter(int segmentNo, ILogger logger)
|
|
||||||
: base(logger, segmentNo)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// InitSegment
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
public IFrameProcessingState InitSegment(SingleTask job, CancellationToken token)
|
|
||||||
{
|
|
||||||
var state = new State(job);
|
|
||||||
|
|
||||||
var decode = StartDecode(job, token);
|
|
||||||
state.DecodeProcess = decode;
|
|
||||||
state.DecodeStdout = decode.StandardOutput.BaseStream;
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// GetNextProcessedFrame
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
public Mat? GetNextProcessedFrame(IFrameProcessingState processorState, CancellationToken token)
|
|
||||||
{
|
|
||||||
var state = (State)processorState;
|
|
||||||
|
|
||||||
if (state.DecodeStdout == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
// SimpleSplitter does not modify frames; it only copies or rotates.
|
|
||||||
// For preview, we decode raw frames and return them as-is.
|
|
||||||
|
|
||||||
// Determine expected frame size
|
|
||||||
var w = state.Info.Width;
|
|
||||||
var h = state.Info.Height;
|
|
||||||
var bytes = w * h * 3;
|
|
||||||
|
|
||||||
var buffer = new byte[bytes];
|
|
||||||
var read = state.DecodeStdout.Read(buffer, 0, bytes);
|
|
||||||
if (read != bytes)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var mat = new Mat(h, w, MatType.CV_8UC3);
|
|
||||||
System.Runtime.InteropServices.Marshal.Copy(buffer, 0, mat.Data, bytes);
|
|
||||||
|
|
||||||
return mat;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// FinishSegment
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
public void FinishSegment(IFrameProcessingState processorState)
|
|
||||||
{
|
|
||||||
var state = (State)processorState;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (state.DecodeProcess != null && !state.DecodeProcess.HasExited)
|
|
||||||
state.DecodeProcess.Kill(entireProcessTree: true);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (state.DecodeProcess != null && !state.DecodeProcess.HasExited)
|
|
||||||
state.DecodeProcess.WaitForExit();
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// ProcessSegment (now uses preview API)
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
public async Task ProcessSegment(SingleTask job, CancellationToken token)
|
public async Task ProcessSegment(SingleTask job, CancellationToken token)
|
||||||
{
|
|
||||||
var state = (State)InitSegment(job, token);
|
|
||||||
|
|
||||||
var encode = StartEncode(job);
|
|
||||||
using var encodeStdin = encode.StandardInput.BaseStream;
|
|
||||||
|
|
||||||
var name = Path.GetFileNameWithoutExtension(job.OutputFileName);
|
|
||||||
var sw = Stopwatch.StartNew();
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
token.ThrowIfCancellationRequested();
|
|
||||||
|
|
||||||
var frame = GetNextProcessedFrame(state, token);
|
|
||||||
if (frame == null)
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Write raw frame to encoder
|
|
||||||
var bytes = frame.Width * frame.Height * 3;
|
|
||||||
var buffer = new byte[bytes];
|
|
||||||
System.Runtime.InteropServices.Marshal.Copy(frame.Data, buffer, 0, bytes);
|
|
||||||
encodeStdin.Write(buffer, 0, bytes);
|
|
||||||
|
|
||||||
frame.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
encodeStdin.Flush();
|
|
||||||
encodeStdin.Close();
|
|
||||||
|
|
||||||
await encode.WaitForExitAsync(token);
|
|
||||||
|
|
||||||
FinishSegment(state);
|
|
||||||
|
|
||||||
ClearProgress(name);
|
|
||||||
|
|
||||||
if (encode.ExitCode != 0)
|
|
||||||
LogError($"Segment {name} FFmpeg encoding failed");
|
|
||||||
else
|
|
||||||
LogInfo($"Segment {name} processing completed");
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// FFmpeg helpers
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
private Process StartDecode(SingleTask job, CancellationToken token)
|
|
||||||
{
|
|
||||||
var ss = job.SegmentStart.ToString("0.###", CultureInfo.InvariantCulture);
|
|
||||||
var t = job.SegmentLength.ToString("0.###", CultureInfo.InvariantCulture);
|
|
||||||
|
|
||||||
var rotate = GetRotationFilter(job.Job.Rotate);
|
|
||||||
var vf = rotate != null ? $"-vf format=bgr24,{rotate}" : "-vf format=bgr24";
|
|
||||||
|
|
||||||
var args =
|
|
||||||
$"-i \"{job.Job.InputFile}\" -ss {ss} -t {t} " +
|
|
||||||
"-an -sn " +
|
|
||||||
$"{vf} " +
|
|
||||||
"-f rawvideo -";
|
|
||||||
|
|
||||||
var psi = new ProcessStartInfo
|
|
||||||
{
|
|
||||||
FileName = "ffmpeg",
|
|
||||||
Arguments = args,
|
|
||||||
RedirectStandardOutput = true,
|
|
||||||
RedirectStandardError = true,
|
|
||||||
UseShellExecute = false,
|
|
||||||
CreateNoWindow = true
|
|
||||||
};
|
|
||||||
|
|
||||||
var p = Process.Start(psi) ?? throw new Exception("Failed to start ffmpeg decode.");
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Process StartEncode(SingleTask job)
|
|
||||||
{
|
{
|
||||||
var inputFile = job.Job.InputFile;
|
var inputFile = job.Job.InputFile;
|
||||||
var outputFile = job.OutputFileName;
|
var outputFile = job.OutputFileName;
|
||||||
@ -198,6 +18,7 @@ public sealed class SimpleSplitter : LoggingBase, ISegmentProcessor
|
|||||||
|
|
||||||
if (rotation == null)
|
if (rotation == null)
|
||||||
{
|
{
|
||||||
|
// Copy path: keep original SAR/DAR exactly as in source
|
||||||
args =
|
args =
|
||||||
$"-ss {start.ToString(CultureInfo.InvariantCulture)} " +
|
$"-ss {start.ToString(CultureInfo.InvariantCulture)} " +
|
||||||
$"-i \"{inputFile}\" " +
|
$"-i \"{inputFile}\" " +
|
||||||
@ -210,27 +31,33 @@ public sealed class SimpleSplitter : LoggingBase, ISegmentProcessor
|
|||||||
var sarArg = "";
|
var sarArg = "";
|
||||||
var darArg = "";
|
var darArg = "";
|
||||||
|
|
||||||
var sar = job.Info.SampleAspectRatio;
|
var sar = job.Info.SampleAspectRatio; // e.g. "4:3"
|
||||||
if (sar != null)
|
if (sar != null)
|
||||||
{
|
{
|
||||||
|
// Rotation path: must re-encode and recompute DAR
|
||||||
|
|
||||||
var sarNum = Convert.ToInt64(job.Info.Sar.X);
|
var sarNum = Convert.ToInt64(job.Info.Sar.X);
|
||||||
var sarDen = Convert.ToInt64(job.Info.Sar.Y);
|
var sarDen = Convert.ToInt64(job.Info.Sar.Y);
|
||||||
|
|
||||||
|
// After rotation, width/height swap
|
||||||
var w = job.Info.Width;
|
var w = job.Info.Width;
|
||||||
var h = job.Info.Height;
|
var h = job.Info.Height;
|
||||||
|
|
||||||
if (job.Job.Rotate == 90 || job.Job.Rotate == 270)
|
if (job.Job.Rotate == 90 || job.Job.Rotate == 270)
|
||||||
|
{
|
||||||
(w, h) = (h, w);
|
(w, h) = (h, w);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute DAR = (w * sarNum) : (h * sarDen)
|
||||||
var darNum = w * sarNum;
|
var darNum = w * sarNum;
|
||||||
var darDen = h * sarDen;
|
var darDen = h * sarDen;
|
||||||
|
|
||||||
|
// Reduce fraction
|
||||||
long Gcd(long a, long b)
|
long Gcd(long a, long b)
|
||||||
{
|
{
|
||||||
while (b != 0) (a, b) = (b, a % b);
|
while (b != 0) (a, b) = (b, a % b);
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
var g = Gcd(darNum, darDen);
|
var g = Gcd(darNum, darDen);
|
||||||
darNum /= g;
|
darNum /= g;
|
||||||
darDen /= g;
|
darDen /= g;
|
||||||
@ -251,21 +78,32 @@ public sealed class SimpleSplitter : LoggingBase, ISegmentProcessor
|
|||||||
$"{string.Join(" ", job.Job.Passthrough)} " +
|
$"{string.Join(" ", job.Job.Passthrough)} " +
|
||||||
$"\"{outputFile}\" -y";
|
$"\"{outputFile}\" -y";
|
||||||
}
|
}
|
||||||
|
|
||||||
var psi = new ProcessStartInfo
|
var psi = new ProcessStartInfo
|
||||||
{
|
{
|
||||||
FileName = "ffmpeg",
|
FileName = "ffmpeg",
|
||||||
Arguments = args,
|
Arguments = args,
|
||||||
RedirectStandardInput = true,
|
|
||||||
RedirectStandardError = true,
|
RedirectStandardError = true,
|
||||||
UseShellExecute = false,
|
UseShellExecute = false,
|
||||||
CreateNoWindow = true
|
CreateNoWindow = true
|
||||||
};
|
};
|
||||||
|
|
||||||
return Process.Start(psi) ?? throw new Exception("Failed to start ffmpeg encode.");
|
using var proc = Process.Start(psi) ?? throw new Exception("Failed to start ffmpeg.");
|
||||||
|
|
||||||
|
var name = Path.GetFileNameWithoutExtension(outputFile);
|
||||||
|
await ShowFFMpegProgress(length, proc, name, token);
|
||||||
|
|
||||||
|
await proc.WaitForExitAsync(token);
|
||||||
|
|
||||||
|
ClearProgress(name);
|
||||||
|
|
||||||
|
if (proc.ExitCode != 0)
|
||||||
|
LogError($"Segment {name} FFmpeg encoding failed");
|
||||||
|
else
|
||||||
|
LogInfo($"Segment {name} processing completed");
|
||||||
}
|
}
|
||||||
|
|
||||||
private string? GetRotationFilter(int? degrees) =>
|
|
||||||
|
string? GetRotationFilter(int? degrees) =>
|
||||||
degrees switch
|
degrees switch
|
||||||
{
|
{
|
||||||
90 => "transpose=1",
|
90 => "transpose=1",
|
||||||
@ -273,4 +111,80 @@ public sealed class SimpleSplitter : LoggingBase, ISegmentProcessor
|
|||||||
270 => "transpose=2",
|
270 => "transpose=2",
|
||||||
_ => null
|
_ => null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static long Gcd(long a, long b)
|
||||||
|
{
|
||||||
|
a = Math.Abs(a);
|
||||||
|
b = Math.Abs(b);
|
||||||
|
|
||||||
|
while (b != 0)
|
||||||
|
{
|
||||||
|
var t = b;
|
||||||
|
b = a % b;
|
||||||
|
a = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ShowFFMpegProgress(double length, Process proc, string name, CancellationToken token)
|
||||||
|
{
|
||||||
|
var sw = Stopwatch.StartNew();
|
||||||
|
|
||||||
|
string? line;
|
||||||
|
while ((line = await proc.StandardError.ReadLineAsync(token)) != null)
|
||||||
|
{
|
||||||
|
// Look for "time=00:00:03.52"
|
||||||
|
var idx = line.IndexOf("time=", StringComparison.OrdinalIgnoreCase);
|
||||||
|
if (idx < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var timeStr = ExtractTimestamp(line, idx + 5);
|
||||||
|
if (timeStr == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!TryParseFfmpegTime(timeStr, out var current))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var progress = current.TotalSeconds / length;
|
||||||
|
if (progress < 0) progress = 0;
|
||||||
|
if (progress > 1) progress = 1;
|
||||||
|
|
||||||
|
var elapsed = sw.Elapsed;
|
||||||
|
var speed = current.TotalSeconds > 0
|
||||||
|
? current.TotalSeconds / elapsed.TotalSeconds
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
var remaining = length - current.TotalSeconds;
|
||||||
|
var etaSeconds = speed > 0 ? remaining / speed : remaining;
|
||||||
|
var eta = TimeSpan.FromSeconds(etaSeconds);
|
||||||
|
|
||||||
|
DrawProgress(name, progress, eta, speed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string? ExtractTimestamp(string line, int startIndex)
|
||||||
|
{
|
||||||
|
// FFmpeg formats: HH:MM:SS.xx
|
||||||
|
// We read until whitespace
|
||||||
|
var end = startIndex;
|
||||||
|
while (end < line.Length && !char.IsWhiteSpace(line[end]))
|
||||||
|
end++;
|
||||||
|
|
||||||
|
if (end <= startIndex)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return line[startIndex..end];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryParseFfmpegTime(string s, out TimeSpan ts)
|
||||||
|
{
|
||||||
|
// FFmpeg uses "00:00:03.52"
|
||||||
|
return TimeSpan.TryParseExact(
|
||||||
|
s,
|
||||||
|
@"hh\:mm\:ss\.ff",
|
||||||
|
CultureInfo.InvariantCulture,
|
||||||
|
out ts);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace splitter;
|
namespace splitter;
|
||||||
|
|
||||||
public record Segment(double Start, double End);
|
|
||||||
|
|
||||||
public class SingleJob
|
public class SingleJob
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -4,58 +4,10 @@ using System.Runtime.InteropServices;
|
|||||||
|
|
||||||
namespace splitter;
|
namespace splitter;
|
||||||
|
|
||||||
public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
public class TrackingSplitter : LoggingBase, ISegmentProcessor
|
||||||
{
|
{
|
||||||
private readonly IObjectTracker _tracker;
|
private readonly IObjectTracker _tracker;
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
// Internal state (never exposed)
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
|
|
||||||
private sealed class FrameProcessingState : IFrameProcessingState
|
|
||||||
{
|
|
||||||
public SingleTask Job { get; }
|
|
||||||
public KalmanTracker Kalman { get; }
|
|
||||||
public CameraController Camera { get; }
|
|
||||||
|
|
||||||
public Mat FrameMat { get; }
|
|
||||||
public Mat OutMat { get; }
|
|
||||||
public byte[] InBuffer { get; }
|
|
||||||
public byte[] OutBuffer { get; }
|
|
||||||
|
|
||||||
public IVideoEnhancer? Enhancer { get; }
|
|
||||||
|
|
||||||
public int InBytes { get; }
|
|
||||||
public int OutBytes { get; }
|
|
||||||
|
|
||||||
public Process? DecodeProcess { get; set; }
|
|
||||||
public Stream? DecodeStdout { get; set; }
|
|
||||||
|
|
||||||
public FrameProcessingState(
|
|
||||||
SingleTask job,
|
|
||||||
KalmanTracker kalman,
|
|
||||||
CameraController camera,
|
|
||||||
Mat frameMat,
|
|
||||||
Mat outMat,
|
|
||||||
byte[] inBuffer,
|
|
||||||
byte[] outBuffer,
|
|
||||||
IVideoEnhancer? enhancer,
|
|
||||||
int inBytes,
|
|
||||||
int outBytes)
|
|
||||||
{
|
|
||||||
Job = job;
|
|
||||||
Kalman = kalman;
|
|
||||||
Camera = camera;
|
|
||||||
FrameMat = frameMat;
|
|
||||||
OutMat = outMat;
|
|
||||||
InBuffer = inBuffer;
|
|
||||||
OutBuffer = outBuffer;
|
|
||||||
Enhancer = enhancer;
|
|
||||||
InBytes = inBytes;
|
|
||||||
OutBytes = outBytes;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public TrackingSplitter(
|
public TrackingSplitter(
|
||||||
int progressLine,
|
int progressLine,
|
||||||
IObjectTracker tracker,
|
IObjectTracker tracker,
|
||||||
@ -66,131 +18,144 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
_tracker = tracker;
|
_tracker = tracker;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// PUBLIC PREVIEW API
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
// InitSegment
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
|
|
||||||
public IFrameProcessingState InitSegment(SingleTask job, CancellationToken token)
|
|
||||||
{
|
|
||||||
var state = (FrameProcessingState)CreateFrameState(job);
|
|
||||||
|
|
||||||
if (state.Enhancer != null)
|
|
||||||
state.Enhancer.InitializeAsync(
|
|
||||||
state.OutMat.Width,
|
|
||||||
state.OutMat.Height,
|
|
||||||
5,
|
|
||||||
token).Wait(token);
|
|
||||||
|
|
||||||
var decode = StartFfmpegDecode(
|
|
||||||
job.Job.InputFile,
|
|
||||||
job.SegmentStart,
|
|
||||||
job.SegmentLength,
|
|
||||||
job.Job.Rotate,
|
|
||||||
job.Job.PlainText,
|
|
||||||
token).Result;
|
|
||||||
|
|
||||||
state.DecodeProcess = decode;
|
|
||||||
state.DecodeStdout = decode.StandardOutput.BaseStream;
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
// GetNextProcessedFrame
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
|
|
||||||
public Mat? GetNextProcessedFrame(
|
|
||||||
IFrameProcessingState processorState,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
var state = (FrameProcessingState)processorState;
|
|
||||||
|
|
||||||
if (state.DecodeStdout == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
if (!TryReadNextFrame(state.DecodeStdout, state, token))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return ProcessFrame(state.FrameMat, state, state.Job, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
// FinishSegment
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
|
|
||||||
public void FinishSegment(IFrameProcessingState processorState)
|
|
||||||
{
|
|
||||||
var state = (FrameProcessingState)processorState;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (state.DecodeProcess != null && !state.DecodeProcess.HasExited)
|
|
||||||
state.DecodeProcess.Kill(entireProcessTree: true);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (state.DecodeProcess != null && !state.DecodeProcess.HasExited)
|
|
||||||
state.DecodeProcess.WaitForExit();
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
|
|
||||||
if (state.Enhancer is IAsyncDisposable ad)
|
|
||||||
ad.DisposeAsync().AsTask().Wait();
|
|
||||||
else if (state.Enhancer is IDisposable d)
|
|
||||||
d.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// PROCESSSEGMENT (full pipeline)
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
public async Task ProcessSegment(SingleTask job, CancellationToken token)
|
public async Task ProcessSegment(SingleTask job, CancellationToken token)
|
||||||
{
|
{
|
||||||
var name = Path.GetFileNameWithoutExtension(job.OutputFileName);
|
var inputFile = job.Job.InputFile;
|
||||||
|
var outputFile = job.OutputFileName;
|
||||||
|
var start = job.SegmentStart;
|
||||||
|
var length = job.SegmentLength;
|
||||||
|
var videoWidth = job.Info.Width;
|
||||||
|
var videoHeight = job.Info.Height;
|
||||||
var fps = job.Info.Fps;
|
var fps = job.Info.Fps;
|
||||||
|
var bitrate = job.Info.Bitrate;
|
||||||
|
var ffmpegPassthroughParameters = job.Job.Passthrough;
|
||||||
|
|
||||||
var state = (FrameProcessingState)InitSegment(job, token);
|
var name = Path.GetFileNameWithoutExtension(outputFile);
|
||||||
|
|
||||||
|
if (videoWidth <= 0 || videoHeight <= 0 || fps <= 0)
|
||||||
|
{
|
||||||
|
LogError($"{name}: ffprobe failed to get metadata");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (job.Job.Crop == null)
|
||||||
|
{
|
||||||
|
LogError($"{name}: Crop parameters are required");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Processing size (what you crop / feed into enhancer)
|
||||||
|
var procWidth = job.Job.Debug ? videoWidth : job.Job.Crop.Value.width;
|
||||||
|
var procHeight = job.Job.Debug ? videoHeight : job.Job.Crop.Value.height;
|
||||||
|
|
||||||
|
IVideoEnhancer? enhancer = null;
|
||||||
|
|
||||||
|
const int window = 5;
|
||||||
|
|
||||||
|
if (job.Job.Enhance)
|
||||||
|
{
|
||||||
|
enhancer = new RealBasicVsr2xDmlEnhancer();
|
||||||
|
await enhancer.InitializeAsync(procWidth, procHeight, window, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encoding size (what FFmpeg encoder expects)
|
||||||
|
var encWidth = enhancer != null ? procWidth * enhancer.ResolutionMultiplier : procWidth;
|
||||||
|
var encHeight = enhancer != null ? procHeight * enhancer.ResolutionMultiplier : procHeight;
|
||||||
|
|
||||||
|
LogInfo($"{name}: src={videoWidth}x{videoHeight} @ {fps:F3}fps, seg=[{start:F3},{length:F3}] proc={procWidth}x{procHeight} enc={encWidth}x{encHeight}");
|
||||||
|
|
||||||
|
var decode = await StartFfmpegDecode(inputFile, start, length, job.Job.Rotate, job.Job.PlainText, token);
|
||||||
|
using var decodeStdout = decode.StandardOutput.BaseStream;
|
||||||
|
|
||||||
var encode = await StartFfmpegEncode(
|
var encode = await StartFfmpegEncode(
|
||||||
job.Job.InputFile,
|
inputFile,
|
||||||
job.OutputFileName,
|
outputFile,
|
||||||
job.SegmentStart,
|
start,
|
||||||
job.SegmentLength,
|
length,
|
||||||
state.OutMat.Width,
|
encWidth,
|
||||||
state.OutMat.Height,
|
encHeight,
|
||||||
job.Info,
|
job.Info,
|
||||||
job.Job.Passthrough,
|
ffmpegPassthroughParameters,
|
||||||
job.Job.PlainText,
|
job.Job.PlainText,
|
||||||
token);
|
token);
|
||||||
|
|
||||||
using var encodeStdin = encode.StandardInput.BaseStream;
|
using var encodeStdin = encode.StandardInput.BaseStream;
|
||||||
|
|
||||||
var totalFrames = (int)Math.Round(job.SegmentLength * fps);
|
// Input: always full frame
|
||||||
var frameIndex = 0;
|
var inBytes = videoWidth * videoHeight * 3;
|
||||||
var startTime = DateTime.UtcNow;
|
|
||||||
|
|
||||||
|
// Output: encoded frame size (may be 4x if enhancement enabled)
|
||||||
|
var outBytes = encWidth * encHeight * 3;
|
||||||
|
|
||||||
|
var inBuffer = new byte[inBytes];
|
||||||
|
var outBuffer = new byte[outBytes];
|
||||||
|
|
||||||
|
using var frameMat = new Mat(videoHeight, videoWidth, MatType.CV_8UC3);
|
||||||
|
|
||||||
|
// outMat is processing size (crop), not necessarily encoding size
|
||||||
|
using var outMat = new Mat(procHeight, procWidth, MatType.CV_8UC3);
|
||||||
|
|
||||||
|
var kalman = new KalmanTracker();
|
||||||
|
var camera = new CameraController(
|
||||||
|
videoWidth,
|
||||||
|
videoHeight,
|
||||||
|
job.Job.Crop.Value.width,
|
||||||
|
job.Job.Crop.Value.height,
|
||||||
|
kalman,
|
||||||
|
job.Job);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var startTime = DateTime.UtcNow;
|
||||||
|
var totalFrames = (int)Math.Round(length * fps);
|
||||||
|
var frameIndex = 0;
|
||||||
|
|
||||||
|
var enhancedOutput = new Mat[window];
|
||||||
|
//totalFrames = 10;
|
||||||
while (frameIndex < totalFrames)
|
while (frameIndex < totalFrames)
|
||||||
{
|
{
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
var frame = GetNextProcessedFrame(state, token);
|
|
||||||
if (frame == null)
|
|
||||||
break;
|
|
||||||
|
|
||||||
frameIndex++;
|
frameIndex++;
|
||||||
|
|
||||||
EncodeFrame(frame, state, encodeStdin);
|
var read = await ReadExact(decodeStdout, inBuffer, 0, inBytes, token);
|
||||||
|
if (read != inBytes)
|
||||||
|
break;
|
||||||
|
|
||||||
|
Marshal.Copy(inBuffer, 0, frameMat.Data, inBytes);
|
||||||
|
|
||||||
|
var (objects, primary) = _tracker.SelectTrackedObject(job, frameMat, kalman.LastMeasurement);
|
||||||
|
|
||||||
|
camera.Update(primary);
|
||||||
|
var roi = camera.Roi;
|
||||||
|
|
||||||
|
if (job.Job.Debug)
|
||||||
|
{
|
||||||
|
DrawDebug(frameMat, objects, camera, kalman);
|
||||||
|
frameMat.CopyTo(outMat); // outMat: procWidth x procHeight == full frame in debug
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
using var cropped = new Mat(frameMat, roi);
|
||||||
|
cropped.CopyTo(outMat); // outMat: procWidth x procHeight == crop
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat frameToWrite = outMat;
|
||||||
|
|
||||||
|
if (enhancer != null)
|
||||||
|
{
|
||||||
|
if (enhancer.TryProcessFrame(outMat, out var enhanced, token))
|
||||||
|
frameToWrite = enhanced; // enhanced: encWidth x encHeight
|
||||||
|
else
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Marshal.Copy(frameToWrite.Data, outBuffer, 0, outBytes);
|
||||||
|
encodeStdin.Write(outBuffer, 0, outBytes);
|
||||||
|
|
||||||
var elapsed = DateTime.UtcNow - startTime;
|
var elapsed = DateTime.UtcNow - startTime;
|
||||||
var progress = totalFrames > 0 ? (double)frameIndex / totalFrames : 0.0;
|
var progress = totalFrames > 0 ? (double)frameIndex / totalFrames : 0.0;
|
||||||
var speed = elapsed.TotalSeconds > 0 ? (frameIndex / elapsed.TotalSeconds) / fps : 0.0;
|
var speed = elapsed.TotalSeconds > 0 ? (frameIndex / elapsed.TotalSeconds) / fps : 0.0;
|
||||||
|
|
||||||
var remainingFrames = Math.Max(totalFrames - frameIndex, 0);
|
var remainingFrames = Math.Max(totalFrames - frameIndex, 0);
|
||||||
var etaSeconds = speed > 0 ? remainingFrames / speed : 0.0;
|
var etaSeconds = speed > 0 ? remainingFrames / speed : 0.0;
|
||||||
var eta = TimeSpan.FromSeconds(etaSeconds);
|
var eta = TimeSpan.FromSeconds(etaSeconds);
|
||||||
@ -198,10 +163,32 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
DrawProgress(name, progress, eta, speed);
|
DrawProgress(name, progress, eta, speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (enhancer != null)
|
||||||
|
{
|
||||||
|
int count = enhancer.Flush(enhancedOutput, token);
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
var mat = enhancedOutput[i]; // encWidth x encHeight
|
||||||
|
Marshal.Copy(mat.Data, outBuffer, 0, outBytes);
|
||||||
|
encodeStdin.Write(outBuffer, 0, outBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
encodeStdin.Flush();
|
encodeStdin.Flush();
|
||||||
encodeStdin.Close();
|
encodeStdin.Close();
|
||||||
|
|
||||||
await encode.WaitForExitAsync();
|
await encode.WaitForExitAsync();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (enhancer is IAsyncDisposable asyncDisp)
|
||||||
|
await asyncDisp.DisposeAsync();
|
||||||
|
else if (enhancer is IDisposable disp)
|
||||||
|
disp?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
try { if (!decode.HasExited) decode.Kill(entireProcessTree: true); } catch { }
|
||||||
|
try { if (!decode.HasExited) await decode.WaitForExitAsync(); } catch { }
|
||||||
|
|
||||||
ClearProgress(name);
|
ClearProgress(name);
|
||||||
|
|
||||||
@ -209,123 +196,12 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
LogError($"{name}: FFmpeg encoding failed");
|
LogError($"{name}: FFmpeg encoding failed");
|
||||||
else
|
else
|
||||||
LogInfo($"{name}: Segment processing completed");
|
LogInfo($"{name}: Segment processing completed");
|
||||||
|
|
||||||
FinishSegment(state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// INTERNAL HELPERS
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
private object CreateFrameState(SingleTask job)
|
// ---------- FFmpeg decode / encode ----------
|
||||||
{
|
|
||||||
var w = job.Info.Width;
|
|
||||||
var h = job.Info.Height;
|
|
||||||
var cw = job.Job.Debug ? w : job.Job.Crop!.Value.width;
|
|
||||||
var ch = job.Job.Debug ? h : job.Job.Crop!.Value.height;
|
|
||||||
|
|
||||||
var kalman = new KalmanTracker();
|
private async Task<Process> StartFfmpegDecode(string inputFile, double start, double length, int? rotate, bool plainText, CancellationToken token)
|
||||||
var camera = new CameraController(w, h, cw, ch, kalman, job.Job);
|
|
||||||
|
|
||||||
var frameMat = new Mat(h, w, MatType.CV_8UC3);
|
|
||||||
var outMat = new Mat(ch, cw, MatType.CV_8UC3);
|
|
||||||
|
|
||||||
var inBytes = w * h * 3;
|
|
||||||
var outBytes = cw * ch * 3;
|
|
||||||
|
|
||||||
var inBuffer = new byte[inBytes];
|
|
||||||
var outBuffer = new byte[outBytes];
|
|
||||||
|
|
||||||
IVideoEnhancer? enhancer = job.Job.Enhance
|
|
||||||
? new RealBasicVsr2xDmlEnhancer()
|
|
||||||
: null;
|
|
||||||
|
|
||||||
return new FrameProcessingState(
|
|
||||||
job,
|
|
||||||
kalman,
|
|
||||||
camera,
|
|
||||||
frameMat,
|
|
||||||
outMat,
|
|
||||||
inBuffer,
|
|
||||||
outBuffer,
|
|
||||||
enhancer,
|
|
||||||
inBytes,
|
|
||||||
outBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool TryReadNextFrame(
|
|
||||||
Stream decodeStdout,
|
|
||||||
FrameProcessingState state,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
var read = ReadExact(
|
|
||||||
decodeStdout,
|
|
||||||
state.InBuffer,
|
|
||||||
0,
|
|
||||||
state.InBytes,
|
|
||||||
token).Result;
|
|
||||||
|
|
||||||
if (read != state.InBytes)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
Marshal.Copy(state.InBuffer, 0, state.FrameMat.Data, state.InBytes);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Mat ProcessFrame(
|
|
||||||
Mat inputFrame,
|
|
||||||
FrameProcessingState state,
|
|
||||||
SingleTask job,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
var (objects, primary) =
|
|
||||||
_tracker.SelectTrackedObject(job, inputFrame, state.Kalman.LastMeasurement);
|
|
||||||
|
|
||||||
state.Camera.Update(primary);
|
|
||||||
var roi = state.Camera.Roi;
|
|
||||||
|
|
||||||
if (job.Job.Debug)
|
|
||||||
{
|
|
||||||
DebugOverlay.DrawDebug(inputFrame, objects, state.Camera, state.Kalman);
|
|
||||||
inputFrame.CopyTo(state.OutMat);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
using var cropped = new Mat(inputFrame, roi);
|
|
||||||
cropped.CopyTo(state.OutMat);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state.Enhancer != null)
|
|
||||||
{
|
|
||||||
if (state.Enhancer.TryProcessFrame(state.OutMat, out var enhanced, token))
|
|
||||||
return enhanced;
|
|
||||||
|
|
||||||
return state.OutMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
return state.OutMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EncodeFrame(
|
|
||||||
Mat frame,
|
|
||||||
FrameProcessingState state,
|
|
||||||
Stream encodeStdin)
|
|
||||||
{
|
|
||||||
Marshal.Copy(frame.Data, state.OutBuffer, 0, state.OutBytes);
|
|
||||||
encodeStdin.Write(state.OutBuffer, 0, state.OutBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
// FFmpeg helpers
|
|
||||||
// ------------------------------------------------------------
|
|
||||||
|
|
||||||
private async Task<Process> StartFfmpegDecode(
|
|
||||||
string inputFile,
|
|
||||||
double start,
|
|
||||||
double length,
|
|
||||||
int? rotate,
|
|
||||||
bool plainText,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
{
|
||||||
var ss = start .ToString("0.###", CultureInfo.InvariantCulture);
|
var ss = start .ToString("0.###", CultureInfo.InvariantCulture);
|
||||||
var t = length.ToString("0.###", CultureInfo.InvariantCulture);
|
var t = length.ToString("0.###", CultureInfo.InvariantCulture);
|
||||||
@ -380,6 +256,7 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
case 270: rotateStr = ",transpose=2"; break;
|
case 270: rotateStr = ",transpose=2"; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rotateStr;
|
return rotateStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,8 +265,7 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
string outputFile,
|
string outputFile,
|
||||||
double start,
|
double start,
|
||||||
double length,
|
double length,
|
||||||
int width,
|
int width, int height,
|
||||||
int height,
|
|
||||||
VideoInfo info,
|
VideoInfo info,
|
||||||
string[] passthrough,
|
string[] passthrough,
|
||||||
bool plainText,
|
bool plainText,
|
||||||
@ -399,17 +275,19 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
var fpsStr = info.Fps.ToString("0.###", CultureInfo.InvariantCulture);
|
var fpsStr = info.Fps.ToString("0.###", CultureInfo.InvariantCulture);
|
||||||
var ss = start.ToString("0.###", CultureInfo.InvariantCulture);
|
var ss = start.ToString("0.###", CultureInfo.InvariantCulture);
|
||||||
var t = length.ToString("0.###", CultureInfo.InvariantCulture);
|
var t = length.ToString("0.###", CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
var sarArg = !string.IsNullOrWhiteSpace(info.SampleAspectRatio)
|
var sarArg = !string.IsNullOrWhiteSpace(info.SampleAspectRatio)
|
||||||
? $"-vf setsar={info.SampleAspectRatio} "
|
? $"-vf setsar={info.SampleAspectRatio} "
|
||||||
: "";
|
: "";
|
||||||
|
|
||||||
var darArg = "";
|
var darArg = "";
|
||||||
|
|
||||||
if (info.Sar is { } s)
|
if (info.Sar is { } s)
|
||||||
{
|
{
|
||||||
|
// compute DAR from output size and SAR
|
||||||
var darNum = width * s.X;
|
var darNum = width * s.X;
|
||||||
var darDen = height * s.Y;
|
var darDen = height * s.Y;
|
||||||
|
|
||||||
|
// clamp to int and reduce
|
||||||
var dn = (int)Math.Min(int.MaxValue, Math.Max(int.MinValue, darNum));
|
var dn = (int)Math.Min(int.MaxValue, Math.Max(int.MinValue, darNum));
|
||||||
var dd = (int)Math.Min(int.MaxValue, Math.Max(int.MinValue, darDen));
|
var dd = (int)Math.Min(int.MaxValue, Math.Max(int.MinValue, darDen));
|
||||||
ReduceFraction(ref dn, ref dd);
|
ReduceFraction(ref dn, ref dd);
|
||||||
@ -428,6 +306,9 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
"-c:a copy " +
|
"-c:a copy " +
|
||||||
pass + $" \"{outputFile}\"";
|
pass + $" \"{outputFile}\"";
|
||||||
|
|
||||||
|
// "-c:a aac -b:a 192k " +
|
||||||
|
|
||||||
|
|
||||||
var psi = new ProcessStartInfo
|
var psi = new ProcessStartInfo
|
||||||
{
|
{
|
||||||
FileName = "ffmpeg",
|
FileName = "ffmpeg",
|
||||||
@ -449,15 +330,19 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
{
|
{
|
||||||
string? line;
|
string? line;
|
||||||
while ((line = await p.StandardError.ReadLineAsync(token)) != null)
|
while ((line = await p.StandardError.ReadLineAsync(token)) != null)
|
||||||
|
{
|
||||||
if (plainText)
|
if (plainText)
|
||||||
LogInfo($"[ffmpeg-encode] {fileName}: {line}");
|
LogInfo($"[ffmpeg-encode] {fileName}: {line}");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch { }
|
catch { }
|
||||||
});
|
});
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------- helpers ----------
|
||||||
|
|
||||||
private static void ReduceFraction(ref int num, ref int den)
|
private static void ReduceFraction(ref int num, ref int den)
|
||||||
{
|
{
|
||||||
int Gcd(int a, int b)
|
int Gcd(int a, int b)
|
||||||
@ -478,13 +363,7 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
den /= g;
|
den /= g;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private static async Task<int> ReadExact(Stream s, byte[] buffer, int offset, int count, CancellationToken token)
|
||||||
private static async Task<int> ReadExact(
|
|
||||||
Stream s,
|
|
||||||
byte[] buffer,
|
|
||||||
int offset,
|
|
||||||
int count,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
{
|
||||||
var total = 0;
|
var total = 0;
|
||||||
while (total < count)
|
while (total < count)
|
||||||
@ -497,5 +376,35 @@ public sealed class TrackingSplitter : LoggingBase, ISegmentProcessor
|
|||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void DrawDebug(
|
||||||
|
Mat frame,
|
||||||
|
List<DetectedPerson> objects,
|
||||||
|
CameraController camera,
|
||||||
|
KalmanTracker kalman)
|
||||||
|
{
|
||||||
|
if (camera.ObjectBox.HasValue)
|
||||||
|
{
|
||||||
|
var fb = camera.ObjectBox.Value;
|
||||||
|
Cv2.Rectangle(frame, fb, Scalar.LimeGreen, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
Cv2.Circle(frame,
|
||||||
|
new Point((int)camera.SmoothedCenter.X, (int)camera.SmoothedCenter.Y),
|
||||||
|
6, Scalar.LimeGreen, -1);
|
||||||
|
|
||||||
|
Cv2.Rectangle(frame, camera.Roi,
|
||||||
|
camera.ObjectCenter.HasValue ? Scalar.Yellow : Scalar.Red, 3);
|
||||||
|
|
||||||
|
DrawText(frame, $"Faces: {objects.Count}", 20, 40, Scalar.White);
|
||||||
|
DrawText(frame, $"LostFrames: {camera.LostFrames}", 20, 70, Scalar.White);
|
||||||
|
DrawText(frame, $"Noise: {kalman.CurrentNoise:F3}", 20, 130, Scalar.White);
|
||||||
|
DrawText(frame, $"Camera: {camera.CameraCenter.X:F1},{camera.CameraCenter.Y:F1}", 20, 160, Scalar.White);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawText(Mat img, string text, int x, int y, Scalar color)
|
||||||
|
{
|
||||||
|
Cv2.PutText(img, text, new Point(x, y),
|
||||||
|
HersheyFonts.HersheySimplex, 0.6, color, 2);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,6 @@
|
|||||||
namespace splitter.algo;
|
namespace splitter.algo;
|
||||||
|
|
||||||
public interface IFrameProcessingState
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface ISegmentProcessor
|
public interface ISegmentProcessor
|
||||||
{
|
{
|
||||||
IFrameProcessingState InitSegment(SingleTask job, CancellationToken token);
|
|
||||||
Mat? GetNextProcessedFrame( IFrameProcessingState processorState, CancellationToken token);
|
|
||||||
void FinishSegment(IFrameProcessingState processorState);
|
|
||||||
|
|
||||||
Task ProcessSegment( SingleTask job, CancellationToken token);
|
Task ProcessSegment( SingleTask job, CancellationToken token);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,8 @@
|
|||||||
namespace splitter.algo;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace splitter.algo;
|
||||||
|
|
||||||
public sealed class IdentityCache
|
public sealed class IdentityCache
|
||||||
{
|
{
|
||||||
|
|||||||
@ -12,6 +12,7 @@ public sealed class YoloV10ObjectDetector : LoggingBase, IObjectDetector, IDispo
|
|||||||
|
|
||||||
private const int _inputWidth = 640;
|
private const int _inputWidth = 640;
|
||||||
private const int _inputHeight = 640;
|
private const int _inputHeight = 640;
|
||||||
|
private const float _scoreThreshold = 0.35f;
|
||||||
private const float _nmsThreshold = 0.45f;
|
private const float _nmsThreshold = 0.45f;
|
||||||
private const int _personClassIndex = 0;
|
private const int _personClassIndex = 0;
|
||||||
|
|
||||||
|
|||||||
@ -38,7 +38,7 @@ static partial class Program
|
|||||||
var allJobs = new List<SingleTask>();
|
var allJobs = new List<SingleTask>();
|
||||||
foreach ( var job in cmd.Jobs )
|
foreach ( var job in cmd.Jobs )
|
||||||
{
|
{
|
||||||
var jobs = await processor.GenerateJobs(job, cmd.Master.EstimateOnly, [], CancellationToken.None);
|
var jobs = await processor.GenerateJobs(job, cmd.Master.EstimateOnly, CancellationToken.None);
|
||||||
allJobs.AddRange(jobs);
|
allJobs.AddRange(jobs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -59,10 +59,10 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FFmpeg.AutoGen" Version="8.1.0" />
|
<PackageReference Include="FFmpeg.AutoGen" Version="8.1.0" />
|
||||||
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.24.4" />
|
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.24.4" />
|
||||||
<PackageReference Include="Onnxify" Version="0.3.4" />
|
<PackageReference Include="Onnxify" Version="0.1.4" />
|
||||||
<PackageReference Include="OpenCvSharp4" Version="4.13.0.20260602" />
|
<PackageReference Include="OpenCvSharp4" Version="4.13.0.20260602" />
|
||||||
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.13.0.20260602" />
|
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.13.0.20260602" />
|
||||||
<PackageReference Include="Spectre.Console" Version="0.57.0" />
|
<PackageReference Include="Spectre.Console" Version="0.56.0" />
|
||||||
<PackageReference Include="UltraFaceDotNet" Version="1.0.0.2" />
|
<PackageReference Include="UltraFaceDotNet" Version="1.0.0.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@ -375,7 +375,7 @@ public sealed class SpectreConsoleLogger : ILogger, IDisposable
|
|||||||
return new Measurement(width, width);
|
return new Measurement(width, width);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Spectre.Console.Rendering.Segment> Render(RenderOptions options, int maxWidth)
|
public IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
|
||||||
{
|
{
|
||||||
var width = Math.Max(1, maxWidth);
|
var width = Math.Max(1, maxWidth);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user