Add ONNX model file picker to recipe editor

This commit is contained in:
sophelper 2026-11-06 00:01:02 +08:00
parent da06e5aa77
commit 6397a33644
2 changed files with 39 additions and 2 deletions

View File

@ -533,10 +533,13 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="模型路径" Style="{StaticResource Field}" />
<TextBox Grid.Column="1" Text="{Binding Path, UpdateSourceTrigger=PropertyChanged}" />
<Button Grid.Column="2" Content="选择模型…" Margin="8,0,0,0" Style="{StaticResource Mini}"
Command="{Binding DataContext.BrowseModelCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" />
</Grid>
<Grid Style="{StaticResource Row}">
@ -558,8 +561,8 @@
</StackPanel>
</StackPanel>
<TextBlock Margin="0,10,0,0" Foreground="{StaticResource FgDim}"
Text="路径相对程序目录;当前用的 yolo11n 是 AGPL-3.0 血统,量产前换掉(见 README)" />
<TextBlock Margin="0,10,0,0" Foreground="{StaticResource FgDim}" TextWrapping="Wrap"
Text="可选择 .onnx 模型文件,或手填相对程序目录的路径;当前用的 yolo11n 是 AGPL-3.0 血统,量产前换掉(见 README)" />
</StackPanel>
</TabItem>
</TabControl>

View File

@ -44,12 +44,14 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
if (!SetProperty(ref _isCapturing, value)) return;
CaptureImageCommand.RaiseCanExecuteChanged();
OpenImageCommand.RaiseCanExecuteChanged();
BrowseModelCommand.RaiseCanExecuteChanged();
SaveCommand.RaiseCanExecuteChanged();
CloseCommand.RaiseCanExecuteChanged();
}
}
public DelegateCommand CaptureImageCommand { get; }
public DelegateCommand OpenImageCommand { get; }
public DelegateCommand BrowseModelCommand { get; }
/// <summary>当前编辑的配方目录(configs/sop)</summary>
private string _directory = "";
@ -94,6 +96,7 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
CaptureImageCommand = new DelegateCommand(async () => await CaptureImageAsync(),
() => !IsCapturing && _camera is not null && _cameraInfo is not null);
OpenImageCommand = new DelegateCommand(OpenImage, () => !IsCapturing);
BrowseModelCommand = new DelegateCommand(BrowseModel, () => !IsCapturing);
}
/// <inheritdoc />
@ -159,6 +162,37 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
catch (Exception ex) { PreviewStatus = "打开图片失败:" + ex.Message; }
}
private void BrowseModel()
{
if (Recipe.Model is null) return;
var dialog = new OpenFileDialog
{
Title = "选择 ONNX 检测模型",
Filter = "ONNX 模型 (*.onnx)|*.onnx",
DefaultExt = ".onnx",
CheckFileExists = true,
Multiselect = false,
InitialDirectory = AppContext.BaseDirectory,
};
try
{
if (!string.IsNullOrWhiteSpace(Recipe.Model.Path))
{
var currentPath = Path.GetFullPath(Recipe.Model.Path, AppContext.BaseDirectory);
var directory = Path.GetDirectoryName(currentPath);
if (Directory.Exists(directory)) dialog.InitialDirectory = directory;
if (File.Exists(currentPath)) dialog.FileName = Path.GetFileName(currentPath);
}
}
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
{
// 手填路径无效时,仍可从程序目录重新选择。
}
if (dialog.ShowDialog() != true) return;
Recipe.Model.Path = dialog.FileName;
RefreshWarnings();
}
private async Task CaptureImageAsync()
{
if (IsCapturing || _camera is null || _cameraInfo is null) return;