Add required item editing controls and target detection overlays
This commit is contained in:
parent
ca7930b9af
commit
6a5bc1a6b4
@ -337,12 +337,20 @@
|
||||
</StackPanel>
|
||||
|
||||
<!-- 必在物料 / 完成时必须存在 -->
|
||||
<TextBlock Style="{StaticResource Title}" FontSize="13" Margin="0,10,0,6"
|
||||
Text="{Binding MustKeep.Title}" />
|
||||
<DockPanel Margin="0,10,0,6">
|
||||
<Button DockPanel.Dock="Right" Content="加必留物品" Style="{StaticResource Mini}"
|
||||
Command="{Binding MustKeep.AddCommand}" />
|
||||
<TextBlock Style="{StaticResource Title}" FontSize="13"
|
||||
Text="{Binding MustKeep.Title}" />
|
||||
</DockPanel>
|
||||
<ItemsControl ItemsSource="{Binding MustKeep.Items}" />
|
||||
|
||||
<TextBlock Style="{StaticResource Title}" FontSize="13" Margin="0,10,0,6"
|
||||
Text="{Binding RequiredObjects.Title}" />
|
||||
<DockPanel Margin="0,10,0,6">
|
||||
<Button DockPanel.Dock="Right" Content="加完成必需物品" Style="{StaticResource Mini}"
|
||||
Command="{Binding RequiredObjects.AddCommand}" />
|
||||
<TextBlock Style="{StaticResource Title}" FontSize="13"
|
||||
Text="{Binding RequiredObjects.Title}" />
|
||||
</DockPanel>
|
||||
<ItemsControl ItemsSource="{Binding RequiredObjects.Items}" />
|
||||
|
||||
<DockPanel Margin="0,10,0,6">
|
||||
|
||||
65
src/SopHelper.App/VVMs/SopPage/DetectionOverlay.cs
Normal file
65
src/SopHelper.App/VVMs/SopPage/DetectionOverlay.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using SopHelper.Engine.Common;
|
||||
|
||||
namespace SopHelper.VVMs.SopPage;
|
||||
|
||||
/// <summary>按原图像素绘制目标框与标签,与视频一起缩放。</summary>
|
||||
public sealed class DetectionOverlay : FrameworkElement
|
||||
{
|
||||
public static readonly DependencyProperty ResultProperty = DependencyProperty.Register(
|
||||
nameof(Result), typeof(FrameResult), typeof(DetectionOverlay),
|
||||
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
|
||||
|
||||
public FrameResult? Result
|
||||
{
|
||||
get => (FrameResult?)GetValue(ResultProperty);
|
||||
set => SetValue(ResultProperty, value);
|
||||
}
|
||||
|
||||
protected override void OnRender(DrawingContext drawing)
|
||||
{
|
||||
base.OnRender(drawing);
|
||||
var result = Result;
|
||||
if (result is null || result.Size.W <= 0 || result.Size.H <= 0 ||
|
||||
ActualWidth <= 0 || ActualHeight <= 0) return;
|
||||
|
||||
drawing.PushClip(new RectangleGeometry(new Rect(RenderSize)));
|
||||
drawing.PushTransform(new ScaleTransform(ActualWidth / result.Size.W, ActualHeight / result.Size.H));
|
||||
var imageBounds = new Rect(0, 0, result.Size.W, result.Size.H);
|
||||
var unit = Math.Max(result.Size.W, result.Size.H) / 700.0;
|
||||
var pen = new Pen(Brushes.Orange, 2 * unit);
|
||||
var background = new SolidColorBrush(Color.FromArgb(220, 12, 18, 24));
|
||||
var typeface = new Typeface("Microsoft YaHei");
|
||||
var padding = 3 * unit;
|
||||
|
||||
foreach (var detection in result.Detections)
|
||||
{
|
||||
var box = detection.Rect;
|
||||
if (!float.IsFinite(box.X) || !float.IsFinite(box.Y) ||
|
||||
!float.IsFinite(box.Right) || !float.IsFinite(box.Bottom) ||
|
||||
!float.IsFinite(detection.Confidence) || box.W <= 0 || box.H <= 0) continue;
|
||||
var bounds = Rect.Intersect(imageBounds, new Rect(box.X, box.Y, box.W, box.H));
|
||||
if (bounds.IsEmpty || bounds.Width <= 0 || bounds.Height <= 0) continue;
|
||||
drawing.DrawRectangle(null, pen, bounds);
|
||||
|
||||
var label = new FormattedText($"{detection.Label} {detection.Confidence:P0}",
|
||||
CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface,
|
||||
12 * unit, Brushes.White, VisualTreeHelper.GetDpi(this).PixelsPerDip)
|
||||
{
|
||||
MaxTextWidth = Math.Max(1, imageBounds.Width - 2 * padding),
|
||||
MaxLineCount = 1,
|
||||
Trimming = TextTrimming.CharacterEllipsis,
|
||||
};
|
||||
var labelSize = new Size(label.Width + 2 * padding, label.Height + 2 * padding);
|
||||
var left = Math.Clamp(bounds.Left, 0, Math.Max(0, imageBounds.Width - labelSize.Width));
|
||||
var top = bounds.Top >= labelSize.Height ? bounds.Top - labelSize.Height : bounds.Top;
|
||||
top = Math.Clamp(top, 0, Math.Max(0, imageBounds.Height - labelSize.Height));
|
||||
drawing.DrawRectangle(background, null, new Rect(new Point(left, top), labelSize));
|
||||
drawing.DrawText(label, new Point(left + padding, top + padding));
|
||||
}
|
||||
drawing.Pop();
|
||||
drawing.Pop();
|
||||
}
|
||||
}
|
||||
@ -47,6 +47,7 @@
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
|
||||
<local:DetectionOverlay Result="{Binding HandResult}" IsHitTestVisible="False" />
|
||||
<local:HandOverlay Result="{Binding HandResult}" IsHitTestVisible="False" />
|
||||
|
||||
</Grid>
|
||||
|
||||
@ -163,7 +163,7 @@ public class SopPageViewModel : ViewModelBase
|
||||
set { SetProperty(ref _frame, value); }
|
||||
}
|
||||
|
||||
/// <summary>最近一次手部检测结果,交给图像上方的绘制层。</summary>
|
||||
/// <summary>最近一次推理结果,供图像上的手部与目标检测绘制层使用。</summary>
|
||||
public FrameResult? HandResult
|
||||
{
|
||||
get => _handResult;
|
||||
@ -597,7 +597,7 @@ public class SopPageViewModel : ViewModelBase
|
||||
|
||||
_engine.ProcessFrame(result);
|
||||
|
||||
// 无手结果也要更新,立即清除上一帧手部标记。
|
||||
// 每帧均更新,无检测结果时清除上一帧的手部标记和目标框。
|
||||
HandResult = result;
|
||||
|
||||
InferenceText = $"{result.InferenceMs:F1} ms";
|
||||
|
||||
@ -127,6 +127,32 @@ internal static class Program
|
||||
if (SopConfigLoader.Load(savePath).Sop.Steps.Single().ViolationRules.Count != 0)
|
||||
throw new Exception("删除违规规则未正确保存");
|
||||
Console.WriteLine("PASS: 界面新增违规规则、编辑字段、保存回读和删除");
|
||||
foreach (var requiredAtEnd in new[] { true, false })
|
||||
{
|
||||
var buttonText = requiredAtEnd ? "加完成必需物品" : "加必留物品";
|
||||
var addItem = Descendants(stepView).OfType<Button>().Single(b => Equals(b.Content, buttonText));
|
||||
if (addItem.Command is null || !addItem.Command.CanExecute(addItem.CommandParameter))
|
||||
throw new Exception(buttonText + "按钮未绑定可执行命令");
|
||||
addItem.Command.Execute(addItem.CommandParameter);
|
||||
stepView.UpdateLayout();
|
||||
var step = reopenedVm.Recipe.SelectedStep!;
|
||||
var item = (requiredAtEnd ? step.RequiredObjects : step.MustKeep).Items.Single();
|
||||
var input = Descendants(stepView).OfType<TextBox>().Single(b => ReferenceEquals(b.DataContext, item));
|
||||
input.SetCurrentValue(TextBox.TextProperty, "part_a");
|
||||
reopenedVm.SaveCommand.Execute();
|
||||
var loadedStep = RecipeNode.From(SopConfigLoader.Load(savePath), "repeat-save", false).SelectedStep!;
|
||||
if ((requiredAtEnd ? loadedStep.RequiredObjects : loadedStep.MustKeep).Items.Single().Text != "part_a")
|
||||
throw new Exception(buttonText + "在界面输入后未保存或重新打开后丢失");
|
||||
var deleteItem = Descendants(stepView).OfType<Button>().Single(b =>
|
||||
ReferenceEquals(b.DataContext, item) && Equals(b.Content, "删"));
|
||||
if (deleteItem.Command is null) throw new Exception(buttonText + "缺少删除命令");
|
||||
deleteItem.Command.Execute(deleteItem.CommandParameter);
|
||||
reopenedVm.SaveCommand.Execute();
|
||||
var savedStep = SopConfigLoader.Load(savePath).Sop.Steps.Single();
|
||||
if ((requiredAtEnd ? savedStep.RequiredObjects : savedStep.MustKeep).Count != 0)
|
||||
throw new Exception(buttonText + "删除未保存");
|
||||
}
|
||||
Console.WriteLine("PASS: 完成必需物品和必留物品的界面新增、文字编辑、保存重开及删除");
|
||||
reopenedVm.OnDialogClosed();
|
||||
Console.WriteLine("PASS: 新增步骤和重新打开的空判定步骤,通过界面选择方法后均可保存");
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user