Fix training runtime to bundled python directory
This commit is contained in:
parent
a5111e69a6
commit
3cdb71bb28
2
.gitignore
vendored
2
.gitignore
vendored
@ -398,3 +398,5 @@ FodyWeavers.xsd
|
||||
/.cache/
|
||||
/workspace/
|
||||
*.pt
|
||||
|
||||
/python/
|
||||
|
||||
@ -117,7 +117,7 @@ powershell -ExecutionPolicy Bypass -File tools/Setup-Python.ps1 -Sam2
|
||||
# CPU 环境附加 -Cpu
|
||||
```
|
||||
|
||||
程序优先使用本项目 `.venv/Scripts/python.exe`,也可在「设置」填写已有 PartTrainer 的 Python 完整路径来复用环境。相机预览和录像不需要 Python。设置保存在 `workspace/settings.json`,录像默认保存在 `workspace/recordings`;API Key 不保存到配置或日志。原 PartTrainer 的 `project.json` 可直接打开,项目中原有的绝对视频、权重路径需要仍然有效。集成后的源码、Python worker 和安装脚本均在本仓库内,构建与发布无需 PartTrainer 目录。
|
||||
程序固定使用可执行文件同目录的 `python/python.exe`,不读取系统 PATH 或设置文件中的 Python 路径,界面不提供路径设置。安装脚本将完整运行时及依赖放在仓库 `python/`,构建和发布会携带该目录。相机预览和录像不需要 Python。设置保存在 `workspace/settings.json`,录像默认保存在 `workspace/recordings`;API Key 不保存到配置或日志。原 PartTrainer 的 `project.json` 可直接打开,项目中原有的绝对视频、权重路径需要仍然有效。集成后的源码、Python worker 和安装脚本均在本仓库内,构建与发布无需 PartTrainer 目录。
|
||||
|
||||
相机录像窗口以模态打开;SOP 正在运行或操作相机时会提示先停止,避免两个流程同时占用设备。关闭采集窗口会结束并封装录像。关闭主窗口时会取消后台 Python 任务、等待进程退出并释放相机;即使已切回 SOP 页面也会清理。
|
||||
|
||||
@ -134,7 +134,7 @@ dotnet run --project tests/Training.Smoke
|
||||
dotnet run --project tests/Training.UiSmoke
|
||||
dotnet run --project tests/Training.CameraSmoke
|
||||
dotnet run --project tests/Training.ShellSmoke
|
||||
.venv/Scripts/python.exe -X utf8 -m unittest discover -s tests -p test_worker.py -v
|
||||
python/python.exe -X utf8 -m unittest discover -s tests -p test_worker.py -v
|
||||
```
|
||||
|
||||
## 配方
|
||||
|
||||
@ -34,6 +34,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="../../python/**/*" Exclude="../../python/**/*.lnk;../../python/**/__pycache__/**;../../python/**/*.pyc" Link="python/%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
|
||||
<Content Include="../../tools/worker.py" Link="tools/worker.py" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
|
||||
<Content Include="../../tools/Setup-Python.ps1" Link="tools/Setup-Python.ps1" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
|
||||
<Content Include="../../tools/requirements.txt" Link="tools/requirements.txt" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
|
||||
|
||||
@ -68,7 +68,7 @@ public sealed class MainViewModel : BindableBase
|
||||
}
|
||||
}
|
||||
// Configuration is saved locally, except the API key which is passed only through the worker environment.
|
||||
public string Python { get; set; } = "python";
|
||||
private static string PythonPath => Path.Combine(AppContext.BaseDirectory, "python", "python.exe");
|
||||
public string Label { get; set; } = "零件";
|
||||
public int ExtractStride { get; set; } = 1;
|
||||
public int MaxFrames { get; set; } = 600;
|
||||
@ -111,8 +111,6 @@ public sealed class MainViewModel : BindableBase
|
||||
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (directory.Parent != null && !File.Exists(Path.Combine(directory.FullName, "SopHelper.slnx"))) directory = directory.Parent;
|
||||
Root = File.Exists(Path.Combine(directory.FullName, "SopHelper.slnx")) ? directory.FullName : AppContext.BaseDirectory;
|
||||
var localPython = Path.Combine(Root, ".venv", "Scripts", "python.exe");
|
||||
if (File.Exists(localPython)) Python = localPython;
|
||||
LoadSettings();
|
||||
ImportCommand = Command(Import);
|
||||
OpenCommand = Command(() =>
|
||||
@ -278,7 +276,9 @@ public sealed class MainViewModel : BindableBase
|
||||
try
|
||||
{
|
||||
SaveSettings();
|
||||
await _worker.RunAsync(Python.Trim(), Path.Combine(Root, "tools", "worker.py"), request, line =>
|
||||
if (!File.Exists(PythonPath))
|
||||
throw new FileNotFoundException("缺少内置 Python 环境,请运行 tools/Setup-Python.ps1 -Sam2 后重新构建程序。", PythonPath);
|
||||
await _worker.RunAsync(PythonPath, Path.Combine(Root, "tools", "worker.py"), request, line =>
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -345,7 +345,7 @@ public sealed class MainViewModel : BindableBase
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(Root, "workspace"));
|
||||
File.WriteAllText(Path.Combine(Root, "workspace", "settings.json"), JsonSerializer.Serialize(new
|
||||
{ Python, Checkpoint, SamConfig, Device, ExportEvery, Epochs, Batch, ImageSize, Model, TrainingOutputDirectory, Endpoint, ApiModel, Description, ExtractStride, MaxFrames }));
|
||||
{ Checkpoint, SamConfig, Device, ExportEvery, Epochs, Batch, ImageSize, Model, TrainingOutputDirectory, Endpoint, ApiModel, Description, ExtractStride, MaxFrames }));
|
||||
}
|
||||
private void LoadSettings()
|
||||
{
|
||||
|
||||
@ -52,8 +52,6 @@
|
||||
<TextBlock Style="{StaticResource Hint}" Text="每次训练重新导出当前复核数据。best.pt、ONNX 和训练指标保存在项目 runs 文件夹。默认 yolo11n,检测框格式便于接入 SopHelper,仍需现场验证。"/>
|
||||
</StackPanel></ScrollViewer></TabItem>
|
||||
<TabItem Header="设置"><ScrollViewer VerticalScrollBarVisibility="Auto"><StackPanel>
|
||||
<TextBlock Text="Python 运行环境" FontWeight="Bold"/>
|
||||
<TextBox Text="{Binding Python}" ToolTip="Python 可执行文件完整路径"/>
|
||||
<Button Content="检查环境与 CUDA" Command="{Binding DoctorCommand}"/>
|
||||
<TextBlock Text="设备:auto / cpu / cuda(训练可填0)"/><TextBox Text="{Binding Device}"/>
|
||||
<TextBlock Text="SAM2 checkpoint(留空自动下载 tiny)"/><TextBox Text="{Binding Checkpoint}"/>
|
||||
|
||||
@ -1,14 +1,21 @@
|
||||
param([switch]$Sam2, [switch]$Cpu)
|
||||
param([switch]$Sam2, [switch]$Cpu)
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$projectRoot = Split-Path $PSScriptRoot -Parent
|
||||
$env:PIP_CACHE_DIR = Join-Path $projectRoot '.cache/pip'
|
||||
$pythonPath = Join-Path $projectRoot '.venv/Scripts/python.exe'
|
||||
$runtimeRoot = Join-Path $projectRoot 'python'
|
||||
$pythonPath = Join-Path $runtimeRoot 'python.exe'
|
||||
function Invoke-Checked([string]$Program, [string[]]$Arguments) {
|
||||
& $Program @Arguments
|
||||
if ($LASTEXITCODE -ne 0) { throw "执行失败: $Program $($Arguments -join ' ')" }
|
||||
}
|
||||
if (!(Test-Path -LiteralPath $pythonPath)) {
|
||||
Invoke-Checked 'python' @('-m', 'venv', (Join-Path $projectRoot '.venv'))
|
||||
# Copy a complete runtime, independent of the system Python and venv home.
|
||||
$baseRoot = & python -c "import sys; print(sys.base_prefix)"
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Cannot locate bootstrap Python' }
|
||||
New-Item -ItemType Directory -Force -Path $runtimeRoot | Out-Null
|
||||
& robocopy $baseRoot $runtimeRoot /E /XD 'site-packages' '__pycache__' /XF '*.pyc' '*.lnk' /NFL /NDL /NJH /NJS
|
||||
if ($LASTEXITCODE -ge 8) { throw 'Failed to copy Python runtime' }
|
||||
Invoke-Checked $pythonPath @('-m', 'ensurepip', '--upgrade')
|
||||
}
|
||||
Invoke-Checked $pythonPath @('-m', 'pip', 'install', '--upgrade', 'pip')
|
||||
if ($Cpu) {
|
||||
@ -23,4 +30,4 @@ if ($Sam2) {
|
||||
Invoke-Checked $pythonPath @('-m', 'pip', 'install', 'wheel', 'huggingface_hub')
|
||||
Invoke-Checked $pythonPath @('-m', 'pip', 'install', '--no-build-isolation', 'git+https://github.com/facebookresearch/sam2.git@2b90b9f5ceec907a1c18123530e92e794ad901a4')
|
||||
}
|
||||
Write-Host "完成。界面 Python 路径填写: $pythonPath"
|
||||
Write-Host "完成。内置环境: $pythonPath;请重新构建/发布程序以携带 python 子目录。"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user