feat: Implement face encoder functionality with GPU support and recursive option

This commit is contained in:
MaddoScientisto 2026-05-09 12:09:05 +02:00
commit 988a3d94e1
10 changed files with 790 additions and 219 deletions

View file

@ -9,6 +9,7 @@ using System.Diagnostics;
#if WINDOWS
using System.Drawing.Text;
#endif
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
@ -40,6 +41,8 @@ namespace ImageCatalog_2
public ICommand SelectModelsFolderCommand { get; }
public ICommand SelectCsvOutputCommand { get; }
public ICommand StartAiCommand { get; }
public ICommand StartFaceEncoderCommand { get; }
public ICommand StopFaceEncoderCommand { get; }
private readonly ITestService _service;
private readonly ILogger<DataModel> _logger;
@ -54,6 +57,14 @@ namespace ImageCatalog_2
private readonly VisualSettingsViewModel _visual;
private readonly PicSettings _picSettings;
private readonly IMapper _mapper;
private readonly AsyncCommand _startFaceEncoderCommand;
private readonly AsyncCommand _stopFaceEncoderCommand;
private readonly object _faceEncoderProcessLock = new();
private Process? _faceEncoderProcess;
private CancellationTokenSource? _faceEncoderWatcherTokenSource;
private Task? _faceEncoderWatcherTask;
private bool _hasStartedFaceEncoderInSession;
private bool _isUpdatingFaceExecutableSelection;
// ComboBox collections
public List<string> AvailableFonts { get; }
@ -93,6 +104,10 @@ namespace ImageCatalog_2
SelectModelsFolderCommand = new RelayCommand(SelectModelsFolder);
SelectCsvOutputCommand = new RelayCommand(SelectCsvOutput);
StartAiCommand = new AsyncCommand(StartAiAsync);
_startFaceEncoderCommand = new AsyncCommand(RunFaceEncoderAsync, CanRunFaceEncoder);
_stopFaceEncoderCommand = new AsyncCommand(() => StopFaceEncoderAsync("Arresto richiesto dall'utente."), CanStopFaceEncoder);
StartFaceEncoderCommand = _startFaceEncoderCommand;
StopFaceEncoderCommand = _stopFaceEncoderCommand;
SelectSourceFolderCommand = new RelayCommand(SelectSourceFolder);
SelectDestinationFolderCommand = new RelayCommand(SelectDestinationFolder);
@ -104,6 +119,7 @@ namespace ImageCatalog_2
// Load available fonts
AvailableFonts = LoadAvailableFonts();
RefreshFaceExecutableCapabilities();
}
private async Task StartAiAsync()
@ -192,7 +208,18 @@ namespace ImageCatalog_2
public string FaceExecutablePath
{
get => _ai.FaceExecutablePath;
set => _ai.FaceExecutablePath = value;
set
{
var normalizedValue = value ?? string.Empty;
if (string.Equals(_ai.FaceExecutablePath, normalizedValue, StringComparison.Ordinal))
{
RefreshFaceExecutableCapabilities();
return;
}
_ai.FaceExecutablePath = normalizedValue;
RefreshFaceExecutableCapabilities();
}
}
public string FaceOutputFolderPath
@ -201,6 +228,38 @@ namespace ImageCatalog_2
set => _ai.FaceOutputFolderPath = value;
}
public bool FaceRecursive
{
get => _ai.FaceRecursive;
set => _ai.FaceRecursive = value;
}
public bool FaceGpuOptionEnabled => _ai.FaceGpuOptionEnabled;
public bool UseFaceGpu
{
get => _ai.UseFaceGpu;
set => SetUseFaceGpu(value);
}
public bool IsFaceEncoderRunning
{
get => _ai.IsFaceEncoderRunning;
private set => _ai.IsFaceEncoderRunning = value;
}
public string FaceStatusMessage
{
get => _ai.FaceStatusMessage;
private set => _ai.FaceStatusMessage = value;
}
public string FaceCommandOutput
{
get => _ai.FaceCommandOutput;
private set => _ai.FaceCommandOutput = value;
}
// Race upload settings
public string ApiLogin
{
@ -393,6 +452,7 @@ namespace ImageCatalog_2
}
NotifyPropertyChanged(e.PropertyName);
UpdateFaceEncoderCommandStates();
}
private void OnRaceUploadPropertyChanged(object? sender, PropertyChangedEventArgs e)
@ -1272,6 +1332,549 @@ namespace ImageCatalog_2
}
}
public async Task StopFaceEncoderAsync(string reason, bool waitForExit = true)
{
var trackedProcess = GetTrackedFaceEncoderProcess();
var ownsProcess = false;
var process = trackedProcess;
if (process is null)
{
process = FindConfiguredFaceEncoderProcess();
ownsProcess = process is not null;
}
if (process is null)
{
await InvokeOnUiThreadAsync(() =>
{
IsFaceEncoderRunning = false;
FaceStatusMessage = "Face encoder non in esecuzione.";
}).ConfigureAwait(false);
return;
}
try
{
await InvokeOnUiThreadAsync(() => FaceStatusMessage = reason).ConfigureAwait(false);
var gracefulStopRequested = TryRequestFaceEncoderStop(process);
if (waitForExit)
{
var exited = await WaitForProcessExitAsync(process, TimeSpan.FromSeconds(5)).ConfigureAwait(false);
if (!exited)
{
try
{
process.Kill(entireProcessTree: true);
exited = await WaitForProcessExitAsync(process, TimeSpan.FromSeconds(5)).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Unable to terminate face encoder process {ProcessId}", process.Id);
}
}
await InvokeOnUiThreadAsync(() =>
{
IsFaceEncoderRunning = !exited && IsProcessAlive(process);
FaceStatusMessage = exited
? "Face encoder arrestato."
: gracefulStopRequested
? "Segnale di arresto inviato al face encoder."
: "Arresto forzato del face encoder richiesto.";
}).ConfigureAwait(false);
}
}
finally
{
if (ownsProcess)
{
process.Dispose();
}
}
}
private bool CanRunFaceEncoder()
{
return !IsFaceEncoderRunning;
}
private bool CanStopFaceEncoder()
{
return IsFaceEncoderRunning;
}
private void UpdateFaceEncoderCommandStates()
{
_startFaceEncoderCommand?.RaiseCanExecuteChanged();
_stopFaceEncoderCommand?.RaiseCanExecuteChanged();
}
private async Task RunFaceEncoderAsync()
{
if (IsFaceEncoderRunning)
{
FaceStatusMessage = "Face encoder gia in esecuzione.";
return;
}
var executablePath = NormalizeFilePathArgument(FaceExecutablePath);
var outputFilePath = NormalizeFilePathArgument(FaceOutputFolderPath);
var imagesFolder = NormalizeDirectoryPathArgument(DestinationPath);
if (string.IsNullOrWhiteSpace(executablePath) || !File.Exists(executablePath))
{
FaceStatusMessage = "Percorso eseguibile non valido.";
return;
}
if (string.IsNullOrWhiteSpace(imagesFolder) || !Directory.Exists(imagesFolder))
{
FaceStatusMessage = "Cartella Destinazione non valida.";
return;
}
if (string.IsNullOrWhiteSpace(outputFilePath))
{
FaceStatusMessage = "Inserisci il file di output .pkl.";
return;
}
if (!string.Equals(Path.GetExtension(outputFilePath), ".pkl", StringComparison.OrdinalIgnoreCase))
{
FaceStatusMessage = "Il file di output deve avere estensione .pkl.";
return;
}
try
{
var outputDirectory = Path.GetDirectoryName(outputFilePath);
if (!string.IsNullOrWhiteSpace(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Unable to create face output directory for file: {OutputFilePath}", outputFilePath);
FaceStatusMessage = "Impossibile creare la cartella del file di output.";
return;
}
FaceExecutablePath = executablePath;
FaceOutputFolderPath = outputFilePath;
FaceCommandOutput = string.Empty;
FaceStatusMessage = "Esecuzione face encoder in corso...";
var outputLines = new StringBuilder();
var errorLines = new StringBuilder();
try
{
var processStartInfo = new ProcessStartInfo
{
FileName = executablePath,
WorkingDirectory = Path.GetDirectoryName(executablePath) ?? Environment.CurrentDirectory,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = false,
};
processStartInfo.ArgumentList.Add("--images");
processStartInfo.ArgumentList.Add(imagesFolder);
processStartInfo.ArgumentList.Add("--out");
processStartInfo.ArgumentList.Add(outputFilePath);
if (FaceRecursive)
{
processStartInfo.ArgumentList.Add("--recursive");
}
using var process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };
process.OutputDataReceived += (_, args) => AppendFaceProcessOutput(outputLines, args.Data);
process.ErrorDataReceived += (_, args) => AppendFaceProcessOutput(errorLines, args.Data);
if (!process.Start())
{
throw new InvalidOperationException("Avvio face encoder fallito.");
}
_hasStartedFaceEncoderInSession = true;
EnsureFaceEncoderWatcherStarted();
TrackFaceEncoderProcess(process);
await InvokeOnUiThreadAsync(() => IsFaceEncoderRunning = true).ConfigureAwait(false);
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.WaitForExitAsync().ConfigureAwait(false);
var summary = BuildFaceEncoderSummary(process.ExitCode, outputLines, errorLines);
await InvokeOnUiThreadAsync(() =>
{
FaceCommandOutput = summary;
FaceStatusMessage = process.ExitCode == 0
? "Face encoder completato."
: $"Face encoder terminato con errore (code {process.ExitCode}).";
}).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogError(ex, "Face encoder execution failed.");
await InvokeOnUiThreadAsync(() =>
{
FaceCommandOutput = ex.ToString();
FaceStatusMessage = "Errore durante esecuzione face encoder.";
}).ConfigureAwait(false);
}
finally
{
ClearTrackedFaceEncoderProcess();
await InvokeOnUiThreadAsync(() => IsFaceEncoderRunning = ComputeIsFaceEncoderRunning()).ConfigureAwait(false);
}
}
private async Task WatchFaceEncoderProcessAsync(CancellationToken token)
{
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
try
{
while (await timer.WaitForNextTickAsync(token).ConfigureAwait(false))
{
if (!_hasStartedFaceEncoderInSession)
{
continue;
}
var isRunning = ComputeIsFaceEncoderRunning();
if (isRunning != IsFaceEncoderRunning)
{
await InvokeOnUiThreadAsync(() => IsFaceEncoderRunning = isRunning).ConfigureAwait(false);
}
}
}
catch (OperationCanceledException)
{
// App shutdown.
}
}
private void EnsureFaceEncoderWatcherStarted()
{
if (_faceEncoderWatcherTask is not null)
{
return;
}
_faceEncoderWatcherTokenSource = new CancellationTokenSource();
_faceEncoderWatcherTask = WatchFaceEncoderProcessAsync(_faceEncoderWatcherTokenSource.Token);
}
private void RefreshFaceExecutableCapabilities()
{
if (_isUpdatingFaceExecutableSelection)
{
return;
}
var executableName = Path.GetFileNameWithoutExtension(NormalizeFilePathArgument(_ai.FaceExecutablePath));
var supportsGpu = executableName.EndsWith("_cpu", StringComparison.OrdinalIgnoreCase)
|| executableName.EndsWith("_gpu", StringComparison.OrdinalIgnoreCase);
var useGpu = supportsGpu && executableName.EndsWith("_gpu", StringComparison.OrdinalIgnoreCase);
_ai.FaceGpuOptionEnabled = supportsGpu;
_ai.UseFaceGpu = supportsGpu && useGpu;
}
private void SetUseFaceGpu(bool value)
{
if (!FaceGpuOptionEnabled)
{
if (_ai.UseFaceGpu)
{
_ai.UseFaceGpu = false;
}
return;
}
if (_ai.UseFaceGpu == value)
{
return;
}
_ai.UseFaceGpu = value;
var currentPath = NormalizeFilePathArgument(_ai.FaceExecutablePath);
if (string.IsNullOrWhiteSpace(currentPath))
{
return;
}
var extension = Path.GetExtension(currentPath);
var fileName = Path.GetFileNameWithoutExtension(currentPath);
var baseName = fileName.EndsWith("_cpu", StringComparison.OrdinalIgnoreCase)
? fileName[..^4]
: fileName.EndsWith("_gpu", StringComparison.OrdinalIgnoreCase)
? fileName[..^4]
: fileName;
if (string.Equals(baseName, fileName, StringComparison.Ordinal))
{
_ai.UseFaceGpu = false;
_ai.FaceGpuOptionEnabled = false;
return;
}
var updatedFileName = $"{baseName}{(value ? "_gpu" : "_cpu")}{extension}";
var directory = Path.GetDirectoryName(currentPath);
var updatedPath = string.IsNullOrWhiteSpace(directory)
? updatedFileName
: Path.Combine(directory, updatedFileName);
if (string.Equals(updatedPath, currentPath, StringComparison.OrdinalIgnoreCase))
{
return;
}
_isUpdatingFaceExecutableSelection = true;
try
{
_ai.FaceExecutablePath = updatedPath;
}
finally
{
_isUpdatingFaceExecutableSelection = false;
}
}
private void TrackFaceEncoderProcess(Process process)
{
lock (_faceEncoderProcessLock)
{
_faceEncoderProcess = process;
}
}
private void ClearTrackedFaceEncoderProcess()
{
lock (_faceEncoderProcessLock)
{
if (_faceEncoderProcess is not null && _faceEncoderProcess.HasExited)
{
_faceEncoderProcess = null;
return;
}
_faceEncoderProcess = null;
}
}
private Process? GetTrackedFaceEncoderProcess()
{
lock (_faceEncoderProcessLock)
{
if (_faceEncoderProcess is null)
{
return null;
}
if (_faceEncoderProcess.HasExited)
{
_faceEncoderProcess = null;
return null;
}
return _faceEncoderProcess;
}
}
private Process? FindConfiguredFaceEncoderProcess()
{
var configuredExecutablePath = NormalizeFilePathArgument(FaceExecutablePath);
if (string.IsNullOrWhiteSpace(configuredExecutablePath))
{
return null;
}
var processName = Path.GetFileNameWithoutExtension(configuredExecutablePath);
foreach (var process in Process.GetProcessesByName(processName))
{
if (!IsProcessAlive(process))
{
process.Dispose();
continue;
}
if (IsMatchingProcessPath(process, configuredExecutablePath))
{
return process;
}
process.Dispose();
}
return null;
}
private bool ComputeIsFaceEncoderRunning()
{
var trackedProcess = GetTrackedFaceEncoderProcess();
if (trackedProcess is not null)
{
return true;
}
if (!_hasStartedFaceEncoderInSession)
{
return false;
}
using var discoveredProcess = FindConfiguredFaceEncoderProcess();
return discoveredProcess is not null;
}
private static bool IsProcessAlive(Process process)
{
try
{
return !process.HasExited;
}
catch
{
return false;
}
}
private static bool IsMatchingProcessPath(Process process, string configuredExecutablePath)
{
try
{
var processPath = process.MainModule?.FileName;
return !string.IsNullOrWhiteSpace(processPath)
&& string.Equals(Path.GetFullPath(processPath), Path.GetFullPath(configuredExecutablePath), StringComparison.OrdinalIgnoreCase);
}
catch
{
return false;
}
}
private bool TryRequestFaceEncoderStop(Process process)
{
if (!IsProcessAlive(process))
{
return true;
}
#if WINDOWS
if (Program.TrySendConsoleInterrupt(process.Id))
{
return true;
}
#endif
try
{
return process.CloseMainWindow();
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Unable to request graceful stop for face encoder process {ProcessId}", process.Id);
return false;
}
}
private static async Task<bool> WaitForProcessExitAsync(Process process, TimeSpan timeout)
{
if (!IsProcessAlive(process))
{
return true;
}
using var cancellationTokenSource = new CancellationTokenSource(timeout);
try
{
await process.WaitForExitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
return true;
}
catch (OperationCanceledException)
{
return !IsProcessAlive(process);
}
}
private static void AppendFaceProcessOutput(StringBuilder builder, string? line)
{
if (string.IsNullOrWhiteSpace(line))
{
return;
}
lock (builder)
{
builder.AppendLine(line);
}
}
private static string BuildFaceEncoderSummary(int exitCode, StringBuilder outputLines, StringBuilder errorLines)
{
var summary = new StringBuilder();
summary.AppendLine($"Exit code: {exitCode}");
lock (outputLines)
{
if (outputLines.Length > 0)
{
summary.AppendLine();
summary.AppendLine("STDOUT:");
summary.Append(outputLines);
}
}
lock (errorLines)
{
if (errorLines.Length > 0)
{
summary.AppendLine();
summary.AppendLine("STDERR:");
summary.Append(errorLines);
}
}
return summary.ToString();
}
private static string NormalizeDirectoryPathArgument(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return string.Empty;
}
var normalized = value.Trim().Trim('"');
var root = Path.GetPathRoot(normalized);
if (!string.IsNullOrEmpty(root) && normalized.Length > root.Length)
{
normalized = normalized.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}
return normalized;
}
private static string NormalizeFilePathArgument(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return string.Empty;
}
return value.Trim().Trim('"');
}
// Note: These commands will trigger events that the View will handle to show dialogs
// since dialogs require UI context
public event EventHandler? SelectSourceFolderRequested;