一帧里只认第一个解码成功的条码,两张面单并排时必然只报旧码、漏掉新码 (角点顺序由检测器决定,跟哪张是新面单无关)。改成: - 一帧的每个候选都解,不再解出第一个就停;同帧两个新码都上报,不压帧不投票; - 新增条码历史(旧→新,最多 4 个):本帧解出的每个码都刷新「最后出现时间」; 历史里没有的码立即上报,已有一律只刷新在场、不重复建档/播报; - 多码同框时上报/续期的是「最后出现的那个」,不是检测器先解出来的那个; 一直看得见的码不重排(避免每帧互相顶掉导致上报抖动),离开又回来的挪到末尾; - 超过 45 秒没再出现的码淘汰(与「面单离开」同一阈值),再出现按新码上报。 排障新增 agent-status.json 的 LabelScan.Items[].RecentCodes(历史,旧→新)。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1355 lines
56 KiB
C#
1355 lines
56 KiB
C#
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using YuncangTrace.LocalService.Services.Hik;
|
||
using ZXing;
|
||
using ZXing.Common;
|
||
|
||
namespace YuncangTrace.LocalService.Services.Scan;
|
||
|
||
/// <summary>一路面单摄像头的运行状态(界面展示用)</summary>
|
||
public sealed class LabelCameraRuntime
|
||
{
|
||
public int WorkstationId { get; init; }
|
||
public int CameraId { get; init; }
|
||
public string WorkstationName { get; init; } = string.Empty;
|
||
public string CameraName { get; init; } = string.Empty;
|
||
public string ChannelId { get; init; } = string.Empty;
|
||
public string ChannelName { get; init; } = string.Empty;
|
||
|
||
/// <summary>工作台 → 摄像头 → 上报通道(**不含密码**)</summary>
|
||
public string Summary { get; init; } = string.Empty;
|
||
|
||
/// <summary>stopped / starting / running / error</summary>
|
||
public string State { get; set; } = "stopped";
|
||
|
||
public string StateText => State switch
|
||
{
|
||
"running" => "识别中",
|
||
"starting" => "启动中…",
|
||
"error" => "异常",
|
||
_ => "已停止",
|
||
};
|
||
|
||
/// <summary>最近识别到的条码</summary>
|
||
public string LastCode { get; set; } = string.Empty;
|
||
|
||
/// <summary>
|
||
/// 条码历史(旧 → 新,最多 4 个;末尾是**最后出现**的那个)。
|
||
/// 多张面单同框时靠它判断「这个码是新出现的,还是刚才就已经有了」。
|
||
/// </summary>
|
||
public string RecentCodes { get; set; } = string.Empty;
|
||
|
||
/// <summary>
|
||
/// **首次识别时间**:也就是这条记录真正上报/落库用的时间。
|
||
/// 面单一直摆在拍摄区不会刷新(重复命中不改时间),避免界面看起来「时间一直在跳」。
|
||
/// </summary>
|
||
public DateTime? LastCodeAt { get; set; }
|
||
|
||
/// <summary>最近一次命中的时间(同一张面单每秒都在刷新,只用于排障看摄像头是否在扫)</summary>
|
||
public DateTime? LastHitAt { get; set; }
|
||
|
||
/// <summary>当前用的码流(主/子 + 分辨率),例如「主码流 2304x1296」</summary>
|
||
public string StreamText { get; set; } = string.Empty;
|
||
|
||
/// <summary>最近一帧的解码耗时(毫秒,含透视矫正 + ZXing 解码;用来看机器吃不吃得消)</summary>
|
||
public double LastDecodeMs { get; set; }
|
||
|
||
/// <summary>最近一帧的 CV 定位耗时(毫秒:找条码横带 + 估倾角;不含解码)</summary>
|
||
public double LastCvMs { get; set; }
|
||
|
||
/// <summary>最近一帧走的是哪条路径(ROI / 退避跳过 / 整帧 / 扫角…):用来定位耗时尖峰</summary>
|
||
public string LastPath { get; set; } = string.Empty;
|
||
|
||
/// <summary>ROI 连续解失败次数(≥3 时进入隔帧退避)</summary>
|
||
public int RoiFailStreak { get; set; }
|
||
|
||
/// <summary>同一个码连续解出的次数(≥3 时进入稳态退避,画面一变就清零)</summary>
|
||
public int SteadyHits { get; set; }
|
||
|
||
/// <summary>从管道读到的总帧数(ffmpeg 实际产出,用来核对 `fps=8` 是否生效)</summary>
|
||
public long FramesRead { get; set; }
|
||
|
||
/// <summary>读帧线程按 DecodeFps 限速后丢弃的帧数</summary>
|
||
public long FrameThrottled { get; set; }
|
||
|
||
/// <summary>解码循环总共处理了多少帧</summary>
|
||
public long FramesDecoded { get; set; }
|
||
|
||
/// <summary>等新帧花了多久(毫秒):≈ 1000/实际处理帧率 说明是「帧不够」,否则是处理慢</summary>
|
||
public double LastWaitMs { get; set; }
|
||
|
||
/// <summary>两次取帧之间的完整周期(毫秒):含 CV、解码、上报等全部开销</summary>
|
||
public double LastLoopMs { get; set; }
|
||
|
||
/// <summary>最近一帧自动定位到的条码区域(ROI w x h;定位失败时为「未定位」)</summary>
|
||
public string RoiText { get; set; } = string.Empty;
|
||
|
||
/// <summary>最近一帧「透视矫正 + ZXing 解码」耗时(毫秒)</summary>
|
||
public double RoiRatio { get; set; }
|
||
|
||
/// <summary>最近一次取到帧时积压了几帧(0=实时;>0 说明解码跟不上、这些旧帧被丢了)</summary>
|
||
public long FrameLag { get; set; }
|
||
|
||
/// <summary>累计丢弃的旧帧数(读帧线程只保留最新一帧)</summary>
|
||
public long FrameDropped { get; set; }
|
||
|
||
/// <summary>累计命中次数(含重复帧合并后的上报次数)</summary>
|
||
public long Hits { get; set; }
|
||
|
||
public string? LastError { get; set; }
|
||
|
||
public string LastCodeAtText => LastCodeAt?.ToString("MM-dd HH:mm:ss") ?? "—";
|
||
|
||
/// <summary>最近一次命中的时间(精确到毫秒:面单每秒都在刷新,用来判断刷新是否及时)</summary>
|
||
public string LastHitAtText => LastHitAt?.ToString("HH:mm:ss.fff") ?? "—";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 面单摄像头识别 worker:按服务端下发的配置**拉流 → 解码 → 识别 → 上报**。
|
||
///
|
||
/// 链路(全部在后台服务进程里跑,与扫码枪链路互不影响):
|
||
/// <code>
|
||
/// ffmpeg(RTSP) → 原始灰度帧 → 条带裁剪 → ZXing 解码
|
||
/// → ScanService.ReportCameraScanAsync(写记录 + 立即上报 + 通知安卓播报)
|
||
/// </code>
|
||
///
|
||
/// 为什么用 ffmpeg 而不是 8000 私有协议:录像机/摄像头型号杂,RTSP 最通用,
|
||
/// 且 ffmpeg 已经随安装包发布(回放校验/合并也在用),不需要再引解码器。
|
||
///
|
||
/// 为什么要试多个通道号:RTSP 的通道序号是「1 起的相对序号」,而库里存的是**设备通道号**
|
||
/// (数字通道从 33 起)。换算需要问录像机(SDK 只有取流子进程能做),
|
||
/// 所以这里按候选依次试(`channelNo-32` → `channelNo`),成功后**记住**用过的序号。
|
||
/// </summary>
|
||
public sealed class LabelScanWorker : IDisposable
|
||
{
|
||
/// <summary>
|
||
/// 画面里「连续一段时间没有任何条码」超过这个时长,就认为面单真的被拿走了。
|
||
///
|
||
/// **这是「面单是否离开过」的唯一判据**,两个地方都用它:
|
||
/// - 识别线程:判断重新出现的是不是新一轮打包(<see cref="LabelCameraRunner"/>),
|
||
/// 决定记录时间用新的还是沿用本轮首次;
|
||
/// - <see cref="ScanService"/>:判断要不要重新上报/播报(`CameraPresenceGap`)。
|
||
///
|
||
/// 为什么用「画面里有没有条码」而不是「有没有解出码」:接入 OpenCV 后 无候选 是可信的
|
||
/// (面单在视野里就一定有候选),而「解不出」还会发生在面单明明在场的时候(反光、遮一角)。
|
||
///
|
||
/// 数值 45 秒:正常打包时手挡、侧身取件一般不会让面单整个离开画面这么久;
|
||
/// 而开箱换货再封箱通常要一分钟以上,能识别成新一轮打包。
|
||
/// </summary>
|
||
internal static readonly TimeSpan RepackAbsence = TimeSpan.FromSeconds(45);
|
||
|
||
/// <summary>解码帧率:面单放进去会停住,6fps 足够,也不至于把机器 CPU 吃满</summary>
|
||
private const int DecodeFps = 6;
|
||
|
||
/// <summary>条码合法性:4~64 位、只含常见条码字符、至少 6 位数字(压掉误识别噪声)</summary>
|
||
private static readonly Regex AllowPattern = new("^[0-9A-Za-z][0-9A-Za-z\\-_/]{3,63}$", RegexOptions.Compiled);
|
||
|
||
private readonly Func<string, string, int, bool, Task<CameraScanResult>> _report;
|
||
private readonly Dictionary<string, LabelCameraRunner> _runners = new(StringComparer.OrdinalIgnoreCase);
|
||
private readonly List<LabelCameraRuntime> _runtimes = new();
|
||
private readonly object _gate = new();
|
||
private bool _disposed;
|
||
|
||
/// <param name="report">命中回调:(channelId, barcode, wsType) → 负责写入记录并通知安卓</param>
|
||
public LabelScanWorker(Func<string, string, int, bool, Task<CameraScanResult>> report)
|
||
{
|
||
_report = report;
|
||
}
|
||
|
||
/// <summary>当前所有识别任务的状态(按 target 顺序)</summary>
|
||
public IReadOnlyList<LabelCameraRuntime> Runtimes
|
||
{
|
||
get
|
||
{
|
||
lock (_gate) return _runtimes.ToList();
|
||
}
|
||
}
|
||
|
||
/// <summary>运行时状态变了(界面/状态文件据此刷新)</summary>
|
||
public event Action? Changed;
|
||
|
||
/// <summary>
|
||
/// 按最新配置对账:新增的启动、配置变了的重建、消失的停掉。
|
||
/// 配置没变的不会被打断(拉配置每 5 分钟一次,不能每次都断流)。
|
||
/// </summary>
|
||
public void Apply(IReadOnlyList<LabelCameraTarget> targets)
|
||
{
|
||
if (_disposed) return;
|
||
|
||
var wanted = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||
var runtimes = new List<LabelCameraRuntime>();
|
||
|
||
foreach (var target in targets)
|
||
{
|
||
// 摄像头停用/没有上报通道的直接跳过(服务端已经过滤过一遍,这里再兜一次)
|
||
if (!target.CameraEnabled || string.IsNullOrWhiteSpace(target.ChannelId))
|
||
{
|
||
AppLog.Write($"[label-scan] 跳过(摄像头停用或没有上报通道):{target.Summary}");
|
||
continue;
|
||
}
|
||
|
||
var key = KeyOf(target);
|
||
wanted.Add(key);
|
||
|
||
try
|
||
{
|
||
LabelCameraRunner runner;
|
||
var started = false;
|
||
lock (_gate)
|
||
{
|
||
if (!_runners.TryGetValue(key, out runner!) || !runner.Matches(target))
|
||
{
|
||
runner?.Stop();
|
||
_runners.Remove(key);
|
||
runner = new LabelCameraRunner(target, _report, RaiseChanged);
|
||
_runners[key] = runner;
|
||
started = true;
|
||
}
|
||
}
|
||
|
||
// 启动放在锁外:某一路拉流/建线程出问题,不能把整轮对账卡死或连累其它路
|
||
if (started)
|
||
{
|
||
runner.Start();
|
||
AppLog.Write($"[label-scan] 开始识别:{runner.Runtime.Summary}");
|
||
}
|
||
|
||
runtimes.Add(runner.Runtime);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
// 一路失败不影响其它路:把半成品清掉,下一轮拉取会再试
|
||
lock (_gate) _runners.Remove(key);
|
||
AppLog.Write($"[label-scan] 启动识别失败:{target.Summary}:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
List<LabelCameraRunner> stale;
|
||
lock (_gate)
|
||
{
|
||
stale = _runners.Where(pair => !wanted.Contains(pair.Key)).Select(pair => pair.Value).ToList();
|
||
foreach (var key in _runners.Keys.Where(key => !wanted.Contains(key)).ToList()) _runners.Remove(key);
|
||
|
||
_runtimes.Clear();
|
||
_runtimes.AddRange(runtimes);
|
||
}
|
||
|
||
foreach (var runner in stale)
|
||
{
|
||
runner.Stop();
|
||
AppLog.Write($"[label-scan] 停止识别:{runner.Runtime.Summary}");
|
||
}
|
||
|
||
RaiseChanged();
|
||
}
|
||
|
||
/// <summary>全部停掉(服务停止/关闭设备服务)</summary>
|
||
public void StopAll()
|
||
{
|
||
List<LabelCameraRunner> all;
|
||
lock (_gate)
|
||
{
|
||
all = _runners.Values.ToList();
|
||
_runners.Clear();
|
||
_runtimes.Clear();
|
||
}
|
||
foreach (var runner in all) runner.Stop();
|
||
RaiseChanged();
|
||
}
|
||
|
||
private void RaiseChanged() => Changed?.Invoke();
|
||
|
||
private static string KeyOf(LabelCameraTarget target) => $"{target.WorkstationId}|{target.CameraId}|{target.ChannelId}";
|
||
|
||
/// <summary>条码过滤(与扫码枪不同:摄像头误识别更多,所以字符集和长度都卡一下)</summary>
|
||
internal static string? CleanBarcode(string? raw)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(raw)) return null;
|
||
|
||
var text = raw.Trim();
|
||
if (!AllowPattern.IsMatch(text)) return null;
|
||
|
||
var digits = text.Count(char.IsDigit);
|
||
return digits >= 6 ? text : null;
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
if (_disposed) return;
|
||
_disposed = true;
|
||
StopAll();
|
||
}
|
||
}
|
||
|
||
/// <summary>一路摄像头的拉流 + 识别循环(ffmpeg 子进程 + 独立线程)</summary>
|
||
internal sealed class LabelCameraRunner
|
||
{
|
||
private const int ConnectTimeoutMs = 6000;
|
||
private const int ReconnectDelayMs = 3000;
|
||
|
||
/// <summary>
|
||
/// 解码帧率:面单放进去会停住,8fps 足够(主码流单帧解码+识别约 15~45ms,吃得下)。
|
||
/// 帧率越高,面单放上去到识别的延迟越小。
|
||
/// </summary>
|
||
private const int DecodeFps = 8;
|
||
|
||
/// <summary>
|
||
/// 命中后连续多少帧「还是同一张面单」就进入稳态退避(隔帧检测)。
|
||
/// 面单一直摆着不动时每帧都跑检测是白花的。
|
||
/// </summary>
|
||
private const int SteadyBackoffAfter = 3;
|
||
|
||
/// <summary>
|
||
/// 连续多少帧「检测/解码都没结果」就进入失败退避(隔帧检测)。
|
||
/// 画面里没面单时不必每帧都跑完整的检测 + 解码。
|
||
/// </summary>
|
||
private const int FailBackoffAfter = 3;
|
||
|
||
/// <summary>
|
||
/// 退避时每 N 帧只检 N-1 帧之外的奇数帧(隔帧)。
|
||
/// 用帧序号的奇偶而不是时间:解码本身耗时稳定,帧序号更简单也更好预测。
|
||
/// </summary>
|
||
private const int BackoffFrameModulo = 2;
|
||
|
||
/// <summary>
|
||
/// 画面变化检测:整帧压成 32x18 块均值。退避期间靠它判断「有东西放进来/面单被拿走」,
|
||
/// 一旦变化就立刻恢复每帧检测,不会因为退避而漏掉新面单。
|
||
/// </summary>
|
||
private const int SceneGridX = 32;
|
||
private const int SceneGridY = 18;
|
||
private const int SceneCellSamples = 4;
|
||
|
||
/// <summary>
|
||
/// 缩略图平均灰度差超过这个值就认为画面变了。
|
||
/// 实测:传感器噪声 1.97~2.11;有物体进入画面 12.09。取 6.0 两头都留了余量。
|
||
/// </summary>
|
||
private const double SceneChangeMeanDiff = 6.0;
|
||
|
||
/// <summary>主码流 / 子码流的 RTSP 后缀(`Streaming/Channels/{序号}01|02`)</summary>
|
||
private const string MainStreamSuffix = "01";
|
||
private const string SubStreamSuffix = "02";
|
||
|
||
/// <summary>
|
||
/// 子码流能用多小:实测这台录像机子码流是 800x448,条码只有 1.8px/模块,**读不出来**;
|
||
/// 1152x648 / 1280x720 能读。所以宽度够 1080 才用子码流(拉流解码开销只有主码流的 1/7)。
|
||
/// </summary>
|
||
private const int MinSubStreamWidth = 1080;
|
||
|
||
/// <summary>数字通道偏移:设备通道号从 33 起 → 64 号通道通常是 RTSP 第 32 路</summary>
|
||
private const int DigitalChannelOffset = 32;
|
||
|
||
private readonly LabelCameraTarget _target;
|
||
private readonly Func<string, string, int, bool, Task<CameraScanResult>> _report;
|
||
private readonly Action _changed;
|
||
private readonly CancellationTokenSource _cts = new();
|
||
private Thread? _thread;
|
||
|
||
/// <summary>
|
||
/// 命中后要上报的条码队列(**扫描线程只入队,不等结果**)。
|
||
/// 上报里有写记录文件、触发上传、通知安卓等动作,任何一个慢都不该拖住拉流和识别。
|
||
/// 容量小 + 满了丢最旧:识别出来的是「面单」,积压的旧事件没有价值。
|
||
/// </summary>
|
||
private readonly BlockingCollection<PendingReport> _pendingReports = new(new ConcurrentQueue<PendingReport>(), 8);
|
||
|
||
private Thread? _reportThread;
|
||
|
||
/// <summary>当前正在跑的 ffmpeg 子进程(<see cref="Stop"/> 要能立刻把它杀掉)</summary>
|
||
private Process? _process;
|
||
|
||
/// <summary>一条待上报的识别结果</summary>
|
||
/// <param name="AfterAbsence">这张面单是从「画面里一段时间没有任何条码」之后重新出现的(真重新打包)</param>
|
||
private readonly record struct PendingReport(string Code, double DecodeMs, double CvMs, bool AfterAbsence);
|
||
|
||
/// <summary>已确认可用的码流(成功后固定,重连不再重新探测——每次探测要 1 秒以上)</summary>
|
||
private static readonly Dictionary<string, StreamChoice> StreamCache = new(StringComparer.OrdinalIgnoreCase);
|
||
|
||
/// <summary>选中的码流:RTSP 序号 + 主/子后缀 + 分辨率</summary>
|
||
private sealed record StreamChoice(int Index, string Suffix, int Width, int Height)
|
||
{
|
||
public string Profile => Suffix == SubStreamSuffix ? "子码流" : "主码流";
|
||
|
||
public string Text => $"{Profile} {Width}x{Height}";
|
||
}
|
||
|
||
/// <summary>上一次命中的条码与时间(同一张面单连续命中时做节流)</summary>
|
||
private string _lastSeenCode = string.Empty;
|
||
private DateTime _lastReportAt = DateTime.MinValue;
|
||
|
||
/// <summary>上一次真正写日志的条码(同一张面单每秒命中一次,不能每次都刷日志)</summary>
|
||
private string _lastLoggedCode = string.Empty;
|
||
|
||
|
||
/// <summary>同一个码连续命中次数(达到 <see cref="SteadyBackoffAfter"/> 后隔帧检测)</summary>
|
||
private int _steadyHits;
|
||
|
||
/// <summary>
|
||
/// 画面里「上一次有条码」的时刻(含解不出的候选)。
|
||
/// 用它判断面单是否真的离开过:离开超过 <see cref="RepackAbsence"/> 再回来,
|
||
/// 就是新一轮打包(装错了重新打包),记录时间要用新的。
|
||
/// </summary>
|
||
private DateTime? _barcodeVisibleAt;
|
||
|
||
/// <summary>
|
||
/// 检测到「面单离开过又回来」的时刻(锁存到第一次成功解码上报为止)。
|
||
/// 面单刚回到画面时可能连续几帧「有候选但解不出」,不能因此丢掉重新打包的标记。
|
||
/// </summary>
|
||
private DateTime? _repackDetectedAt;
|
||
|
||
/// <summary>连续多少帧没结果就隔帧检测(见 <see cref="FailBackoffAfter"/>)</summary>
|
||
private int _failStreak;
|
||
|
||
/// <summary>上一次成功解出的条码(用于判断「还是同一张面单」)</summary>
|
||
private string _lastHitCode = string.Empty;
|
||
|
||
/// <summary>
|
||
/// 条码历史(旧 → 新,末尾 = 最后出现的那个)。
|
||
///
|
||
/// 为什么要有它:一帧里可能同时解出**多个**条码(两张面单并排)。
|
||
/// 只读一个必然漏单,而「谁先解出来」取决于检测器给的角点顺序、跟谁是新面单无关,
|
||
/// 所以必须记住刚才出现过哪些码,才能分辨出「刚进来的新码」和「一直在画面里的旧码」。
|
||
/// </summary>
|
||
private readonly List<(string Code, DateTime At)> _codeHistory = new();
|
||
|
||
/// <summary>上一帧看得见的码:区分「一直在画面里」和「离开又回来」,后者才算最后出现</summary>
|
||
private readonly HashSet<string> _visibleLastFrame = new(StringComparer.Ordinal);
|
||
|
||
/// <summary>
|
||
/// 条码历史保留条数。至少要 2 个才能做「新码 / 旧码」的对比;留 4 个是为了
|
||
/// 画面里同时出现两三张面单(或一两帧误读)时,不会把仍然在场的码挤出去、下一帧又被当成新码重报。
|
||
/// </summary>
|
||
private const int CodeHistorySize = 4;
|
||
|
||
public LabelCameraRunner(LabelCameraTarget target, Func<string, string, int, bool, Task<CameraScanResult>> report, Action changed)
|
||
{
|
||
_target = target;
|
||
_report = report;
|
||
_changed = changed;
|
||
Runtime = new LabelCameraRuntime
|
||
{
|
||
WorkstationId = target.WorkstationId,
|
||
CameraId = target.CameraId,
|
||
WorkstationName = target.WorkstationName,
|
||
CameraName = target.CameraName,
|
||
ChannelId = target.ChannelId,
|
||
ChannelName = target.ChannelName,
|
||
Summary = target.Summary,
|
||
};
|
||
}
|
||
|
||
public LabelCameraRuntime Runtime { get; }
|
||
|
||
/// <summary>配置是否一致(不一致就重建:换了 NVR/通道/码流都要重启)</summary>
|
||
public bool Matches(LabelCameraTarget target)
|
||
=> target.CameraId == _target.CameraId
|
||
&& target.ChannelId == _target.ChannelId
|
||
&& target.NvrHost == _target.NvrHost
|
||
&& target.NvrPort == _target.NvrPort
|
||
&& target.NvrUsername == _target.NvrUsername
|
||
&& target.NvrPassword == _target.NvrPassword
|
||
&& target.CameraChannelNo == _target.CameraChannelNo
|
||
&& target.StreamType == _target.StreamType;
|
||
|
||
public void Start()
|
||
{
|
||
// 扫描任务独占一条线程:拉流读取、识别、上报各走各的线程,谁慢都不拖别人
|
||
_thread = new Thread(() => LoopAsync().GetAwaiter().GetResult())
|
||
{
|
||
IsBackground = true,
|
||
Name = $"label-scan-{_target.CameraId}",
|
||
};
|
||
_thread.Start();
|
||
|
||
_reportThread = new Thread(ReportLoop) { IsBackground = true, Name = $"label-report-{_target.CameraId}" };
|
||
_reportThread.Start();
|
||
}
|
||
|
||
public void Stop()
|
||
{
|
||
try
|
||
{
|
||
_cts.Cancel();
|
||
// 直接杀掉 ffmpeg:只 Cancel 的话读帧线程还挂在管道读上,
|
||
// 要等下一帧/超时才退出——现场表现就是「取消掉的那路还在跑一会儿」。
|
||
TryKill(_process);
|
||
_pendingReports.CompleteAdding();
|
||
}
|
||
catch
|
||
{
|
||
// 忽略
|
||
}
|
||
|
||
SetState("stopped", null);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上报线程:串行消费命中结果。
|
||
/// 扫描线程只负责 <c>TryAdd</c>,永远不等这一环(写记录、上报、通知安卓都可能慢)。
|
||
/// </summary>
|
||
private void ReportLoop()
|
||
{
|
||
try
|
||
{
|
||
foreach (var pending in _pendingReports.GetConsumingEnumerable(_cts.Token))
|
||
{
|
||
try
|
||
{
|
||
var result = _report(_target.ChannelId, pending.Code, _target.WsType, pending.AfterAbsence).GetAwaiter().GetResult();
|
||
if (result.ScanTime is DateTime scanTime) Runtime.LastCodeAt = scanTime;
|
||
if (result.NewScan) Runtime.Hits++;
|
||
_changed();
|
||
|
||
if (result.NewScan || !string.Equals(pending.Code, _lastLoggedCode, StringComparison.Ordinal))
|
||
{
|
||
_lastLoggedCode = pending.Code;
|
||
AppLog.Write($"[label-scan] 识别到条码:{pending.Code}(通道 {_target.ChannelId}"
|
||
+ $",首次 {Runtime.LastCodeAtText},检测 {pending.CvMs:0.0}ms"
|
||
+ $",新记录={result.NewScan})");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"[label-scan] {Runtime.Summary} 上报识别结果失败:{ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
// 停机
|
||
}
|
||
catch (ObjectDisposedException)
|
||
{
|
||
// 停机
|
||
}
|
||
}
|
||
|
||
/// <summary>把一次命中丢进上报队列(满了丢最旧的,扫描线程不阻塞)</summary>
|
||
private void EnqueueReport(string code, double decodeMs, double cvMs, bool afterAbsence)
|
||
{
|
||
if (_pendingReports.IsAddingCompleted) return;
|
||
|
||
var item = new PendingReport(code, decodeMs, cvMs, afterAbsence);
|
||
while (!_pendingReports.TryAdd(item))
|
||
{
|
||
if (!_pendingReports.TryTake(out _)) break; // 丢掉最旧的一条后再试
|
||
if (_pendingReports.IsAddingCompleted) return;
|
||
}
|
||
}
|
||
|
||
private async Task LoopAsync()
|
||
{
|
||
var ct = _cts.Token;
|
||
var attempts = 0;
|
||
|
||
while (!ct.IsCancellationRequested)
|
||
{
|
||
Process? process = null;
|
||
try
|
||
{
|
||
SetState("starting", null);
|
||
var stream = await ResolveAndProbeAsync(ct).ConfigureAwait(false);
|
||
if (stream.Width <= 0 || stream.Height <= 0) throw new InvalidOperationException("拿不到码流分辨率");
|
||
|
||
Runtime.StreamText = stream.Text;
|
||
process = StartFfmpeg(stream);
|
||
_process = process;
|
||
SetState("running", null);
|
||
if (attempts > 0) AppLog.Write($"[label-scan] {Runtime.Summary} 已重新连上码流({stream.Text})");
|
||
attempts = 0;
|
||
|
||
DecodeLoop(process, stream, ct);
|
||
if (ct.IsCancellationRequested) break;
|
||
throw new InvalidOperationException("码流已断开");
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
break;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
attempts++;
|
||
SetState("error", ex.Message);
|
||
if (attempts <= 3 || attempts % 10 == 0)
|
||
{
|
||
AppLog.Write($"[label-scan] {Runtime.Summary} 识别失败({ex.Message}),{ReconnectDelayMs / 1000} 秒后重试"
|
||
+ (attempts > 1 ? $";已重试 {attempts} 次" : string.Empty));
|
||
}
|
||
}
|
||
finally
|
||
{
|
||
if (ReferenceEquals(_process, process)) _process = null;
|
||
TryKill(process);
|
||
}
|
||
|
||
try
|
||
{
|
||
await Task.Delay(ReconnectDelayMs, ct).ConfigureAwait(false);
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
SetState("stopped", null);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 选码流 + 找到可用的 RTSP 序号 + 拿分辨率。
|
||
///
|
||
/// 选流规则:**子码流够大就用子码流**(拉流解码开销只有主码流的 1/7,识别更快也更省 CPU),
|
||
/// 太小就退回主码流 —— 条码像素不够时子码流根本解不出来
|
||
/// (实测这台录像机子码流 800x448,条码只有 1.8px/模块,读不出;1152x648 / 1280x720 能读)。
|
||
///
|
||
/// 序号要按候选试(见类注释),结果缓存起来,重连不再重新探测(每次探测要 1 秒以上)。
|
||
/// </summary>
|
||
private async Task<StreamChoice> ResolveAndProbeAsync(CancellationToken ct)
|
||
{
|
||
var cacheKey = $"{_target.NvrHost}|{_target.NvrPort}|{_target.CameraChannelNo}";
|
||
|
||
if (StreamCache.TryGetValue(cacheKey, out var cached))
|
||
{
|
||
try
|
||
{
|
||
var size = await ProbeSizeAsync(BuildRtspUrl(cached.Index, cached.Suffix), ct).ConfigureAwait(false);
|
||
if (size.Width == cached.Width && size.Height == cached.Height) return cached;
|
||
}
|
||
catch
|
||
{
|
||
// 探测失败就重新走一遍候选
|
||
}
|
||
}
|
||
|
||
Exception? lastError = null;
|
||
foreach (var index in Candidates())
|
||
{
|
||
foreach (var suffix in new[] { SubStreamSuffix, MainStreamSuffix })
|
||
{
|
||
ct.ThrowIfCancellationRequested();
|
||
var url = BuildRtspUrl(index, suffix);
|
||
try
|
||
{
|
||
var size = await ProbeSizeAsync(url, ct).ConfigureAwait(false);
|
||
if (size.Width <= 0 || size.Height <= 0)
|
||
{
|
||
lastError = new InvalidOperationException($"序号 {index}{suffix} 没有取到画面");
|
||
continue;
|
||
}
|
||
|
||
if (suffix == SubStreamSuffix && size.Width < MinSubStreamWidth)
|
||
{
|
||
AppLog.Write($"[label-scan] {Runtime.Summary} 子码流只有 {size.Width}x{size.Height}"
|
||
+ "(条码像素不够,读不出来),改用主码流");
|
||
continue;
|
||
}
|
||
|
||
var choice = new StreamChoice(index, suffix, size.Width, size.Height);
|
||
StreamCache[cacheKey] = choice;
|
||
AppLog.Write($"[label-scan] {Runtime.Summary} 码流就绪:{choice.Text}(序号 {index}{suffix})");
|
||
return choice;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
lastError = ex;
|
||
}
|
||
}
|
||
}
|
||
|
||
throw new InvalidOperationException(lastError?.Message ?? "所有通道序号都取不到码流");
|
||
}
|
||
|
||
/// <summary>候选 RTSP 序号:先按「数字通道从 33 起」换算,再退回用原始通道号</summary>
|
||
private IEnumerable<int> Candidates()
|
||
{
|
||
var channelNo = _target.CameraChannelNo;
|
||
var logical = channelNo - DigitalChannelOffset;
|
||
|
||
if (logical > 0 && logical != channelNo) yield return logical;
|
||
if (channelNo > 0) yield return channelNo;
|
||
}
|
||
|
||
/// <summary>******host:554/Streaming/Channels/{序号}{01|02}</summary>
|
||
private string BuildRtspUrl(int streamIndex, string suffix)
|
||
=> $"rtsp://{Uri.EscapeDataString(_target.NvrUsername)}:{Uri.EscapeDataString(_target.NvrPassword)}"
|
||
+ $"@{_target.NvrHost}:554/Streaming/Channels/{streamIndex}{suffix}";
|
||
|
||
/// <summary>用 ffmpeg 打开一次流,从 stderr 里读出分辨率(拿不到就当失败)</summary>
|
||
private static async Task<(int Width, int Height)> ProbeSizeAsync(string url, CancellationToken ct)
|
||
{
|
||
var ffmpeg = HikFfmpegService.ResolveFfmpeg()
|
||
?? throw new InvalidOperationException("未找到 ffmpeg(安装目录 native\\ffmpeg\\ffmpeg.exe 缺失)");
|
||
|
||
using var process = NewProcess(ffmpeg, new[]
|
||
{
|
||
"-hide_banner", "-nostdin", "-rtsp_transport", "tcp", "-timeout", "5000000",
|
||
"-loglevel", "info", "-i", url,
|
||
});
|
||
process.StartInfo.RedirectStandardError = true;
|
||
|
||
var text = new StringBuilder();
|
||
process.ErrorDataReceived += (_, e) =>
|
||
{
|
||
if (e.Data is not null && text.Length < 64 * 1024) text.AppendLine(e.Data);
|
||
};
|
||
process.Start();
|
||
process.BeginErrorReadLine();
|
||
|
||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||
timeout.CancelAfter(ConnectTimeoutMs);
|
||
try
|
||
{
|
||
await process.WaitForExitAsync(timeout.Token).ConfigureAwait(false);
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
TryKill(process);
|
||
throw new InvalidOperationException("连接码流超时");
|
||
}
|
||
|
||
var matched = Regex.Match(text.ToString(), @"Video:.*?,\s*(\d{2,5})x(\d{2,5})");
|
||
if (!matched.Success) return (0, 0);
|
||
|
||
return (int.Parse(matched.Groups[1].Value), int.Parse(matched.Groups[2].Value));
|
||
}
|
||
|
||
private Process StartFfmpeg(StreamChoice stream)
|
||
{
|
||
var ffmpeg = HikFfmpegService.ResolveFfmpeg()
|
||
?? throw new InvalidOperationException("未找到 ffmpeg");
|
||
|
||
var args = new List<string>
|
||
{
|
||
"-hide_banner", "-nostdin", "-loglevel", "error",
|
||
"-rtsp_transport", "tcp", "-timeout", "5000000",
|
||
// 只开 2 个解码线程:默认的帧级多线程会把输出往后压好几帧
|
||
// (实测同一条流:默认线程 OSD 到本机 2.6s,2 线程只要 0.5s)。
|
||
// 输入 20fps、我们只要 8fps,单帧解码慢一点完全够。
|
||
"-threads", "2",
|
||
"-i", BuildRtspUrl(stream.Index, stream.Suffix),
|
||
"-an",
|
||
// 抽帧 + 转灰度 rawvideo 从 stdout 吐出:每帧正好 width*height 字节
|
||
"-vf", $"fps={DecodeFps}",
|
||
"-f", "rawvideo", "-pix_fmt", "gray",
|
||
"-s", $"{stream.Width}x{stream.Height}",
|
||
"pipe:1",
|
||
};
|
||
|
||
var process = NewProcess(ffmpeg, args);
|
||
process.StartInfo.RedirectStandardOutput = true;
|
||
process.StartInfo.RedirectStandardError = true;
|
||
process.Start();
|
||
|
||
// stderr 必须读掉,否则管道满了 ffmpeg 会卡住
|
||
process.ErrorDataReceived += (_, e) =>
|
||
{
|
||
if (string.IsNullOrWhiteSpace(e.Data) || IsNoisyFfmpegLine(e.Data)) return;
|
||
AppLog.Write($"[label-scan] ffmpeg({Runtime.CameraName}):{e.Data}");
|
||
};
|
||
process.BeginErrorReadLine();
|
||
|
||
return process;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 这台录像机的 HEVC 码流本身就会吐这些告警(画面照常解出来),
|
||
/// 不滤掉的话每帧刷屏、把有用的日志顶没了。
|
||
/// </summary>
|
||
private static bool IsNoisyFfmpegLine(string line)
|
||
=> line.Contains("hevc @", StringComparison.OrdinalIgnoreCase)
|
||
|| line.Contains("Error constructing the frame RPS", StringComparison.OrdinalIgnoreCase)
|
||
|| line.Contains("Could not find ref with POC", StringComparison.OrdinalIgnoreCase)
|
||
|| line.Contains("PPS id out of range", StringComparison.OrdinalIgnoreCase)
|
||
|| line.Contains("Last message repeated", StringComparison.OrdinalIgnoreCase);
|
||
|
||
/// <summary>逐帧读灰度图并识别(帧不完整/超时就当断流)</summary>
|
||
private void DecodeLoop(Process process, StreamChoice choice, CancellationToken ct)
|
||
{
|
||
var width = choice.Width;
|
||
var height = choice.Height;
|
||
var frameBytes = width * height;
|
||
var stream = process.StandardOutput.BaseStream;
|
||
var frameIndex = 0;
|
||
var decodeWatch = new Stopwatch();
|
||
|
||
// 三块缓冲轮转:pending(读帧线程写) / latest(最新一帧) / buffer(解码用),靠交换引用避免每帧 2MB 拷贝
|
||
var buffer = new byte[frameBytes];
|
||
var pending = new byte[frameBytes];
|
||
var latest = new byte[frameBytes];
|
||
var frameGate = new object();
|
||
var frameReady = false;
|
||
var readerDone = false;
|
||
long produced = 0;
|
||
long consumed = 0;
|
||
var frameSignal = new SemaphoreSlim(0, 1);
|
||
|
||
// ffmpeg 的 `-vf fps=8` 是生效的(实测管道里就是 8 帧/秒),这里的限速只用于
|
||
// 兜住「某些流真的吐出更多帧」的情况。留 20% 容差:帧间隔实测 119~125ms,
|
||
// 若按严格的 125ms 判定,会每两帧才放过一帧(把 8fps 砍成 4fps)。
|
||
var publishIntervalMs = 1000.0 / DecodeFps;
|
||
var publishThresholdMs = publishIntervalMs * 0.8;
|
||
|
||
// 常驻读帧线程(独占线程,不占线程池):始终把管道读空,只保留最新一帧,旧帧直接丢弃。
|
||
// 不这样做的话:解码一帧要 10~380ms,而管道里每秒有 20~30 帧,
|
||
// 帧会越积越多,等解码器拿到时画面已经是几秒前的了(现场出现过 >3 秒延迟)。
|
||
var readerThread = new Thread(ReadFrames)
|
||
{
|
||
IsBackground = true,
|
||
Name = $"label-reader-{_target.CameraId}",
|
||
};
|
||
|
||
void ReadFrames()
|
||
{
|
||
var lastPublish = Stopwatch.GetTimestamp();
|
||
try
|
||
{
|
||
while (!ct.IsCancellationRequested)
|
||
{
|
||
var filled = 0;
|
||
while (filled < frameBytes)
|
||
{
|
||
var read = stream.Read(pending, filled, frameBytes - filled);
|
||
if (read <= 0) return;
|
||
filled += read;
|
||
}
|
||
|
||
Runtime.FramesRead++;
|
||
|
||
var elapsedMs = (Stopwatch.GetTimestamp() - lastPublish) * 1000.0 / Stopwatch.Frequency;
|
||
if (elapsedMs < publishThresholdMs)
|
||
{
|
||
// 还没到下一个解码时刻:这帧只读不用
|
||
Runtime.FrameThrottled++;
|
||
continue;
|
||
}
|
||
|
||
lastPublish = Stopwatch.GetTimestamp();
|
||
|
||
lock (frameGate)
|
||
{
|
||
(pending, latest) = (latest, pending);
|
||
frameReady = true;
|
||
produced++;
|
||
}
|
||
|
||
if (frameSignal.CurrentCount == 0) frameSignal.Release();
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
// 断流 / 按取消 / 进程被杀:交给解码循环退出
|
||
}
|
||
finally
|
||
{
|
||
lock (frameGate) readerDone = true;
|
||
try { if (frameSignal.CurrentCount == 0) frameSignal.Release(); } catch { /* 已释放 */ }
|
||
}
|
||
}
|
||
|
||
readerThread.Start();
|
||
|
||
// 画面变化检测的缩略图(32x18),用于退避期间判断什么时候该恢复全帧率
|
||
var sceneNow = new byte[SceneGridX * SceneGridY];
|
||
var scenePrev = new byte[SceneGridX * SceneGridY];
|
||
var hasScene = false;
|
||
|
||
// 每次(重)连上码流都重置:重连期间看不到画面,不能算成「面单离开过」
|
||
_barcodeVisibleAt = null;
|
||
_repackDetectedAt = null;
|
||
|
||
// OpenCV 条码检测器:整图输入,直接给出条码的四角点(带旋转/透视)。
|
||
// 复用同一个 Mat 承接每帧灰度数据(SetArray 是 2MB memcpy,约 0.2ms),避免每帧分配。
|
||
using var detector = new OpenCvSharp.BarcodeDetector();
|
||
using var frameMat = new OpenCvSharp.Mat(height, width, OpenCvSharp.MatType.CV_8UC1);
|
||
|
||
// ZXing 只负责解「已矫正成轴对齐」的条码小图,输入很小,所以选项可以全开
|
||
var reader = new BarcodeReaderGeneric
|
||
{
|
||
AutoRotate = true,
|
||
Options = new DecodingOptions
|
||
{
|
||
TryHarder = true,
|
||
// 只认 Code128:运单条码都是 Code128。开启 EAN13 会把包裹/商品上的
|
||
// 商品条码也读进来(实测读到过 13 位商品码),属于误报。
|
||
PossibleFormats = new[] { BarcodeFormat.CODE_128 },
|
||
},
|
||
};
|
||
|
||
var lastConsumeAt = 0L;
|
||
while (!ct.IsCancellationRequested)
|
||
{
|
||
var waitStart = Stopwatch.GetTimestamp();
|
||
// 同步等待(不 await):整块扫描任务始终跑在自己的独占线程上,
|
||
// 不会被切到线程池去排队,线程池忙也影响不到识别。
|
||
frameSignal.Wait(ct);
|
||
Runtime.LastWaitMs = (Stopwatch.GetTimestamp() - waitStart) * 1000.0 / Stopwatch.Frequency;
|
||
|
||
// 与「上一帧被取走」的间隔 = 完整的循环周期(含上报等所有开销)
|
||
var nowTicks = Stopwatch.GetTimestamp();
|
||
if (lastConsumeAt != 0)
|
||
{
|
||
Runtime.LastLoopMs = (nowTicks - lastConsumeAt) * 1000.0 / Stopwatch.Frequency;
|
||
}
|
||
|
||
lastConsumeAt = nowTicks;
|
||
Runtime.FramesDecoded++;
|
||
|
||
long lag;
|
||
lock (frameGate)
|
||
{
|
||
if (!frameReady)
|
||
{
|
||
if (readerDone) return;
|
||
continue;
|
||
}
|
||
|
||
(latest, buffer) = (buffer, latest);
|
||
frameReady = false;
|
||
lag = produced - consumed - 1;
|
||
consumed++;
|
||
}
|
||
|
||
// lag = 这一帧之前被丢掉了几帧:0 表示实时,越大说明解码越跟不上
|
||
Runtime.FrameLag = lag > 0 ? lag : 0;
|
||
if (lag > 0) Runtime.FrameDropped += lag;
|
||
|
||
frameIndex++;
|
||
decodeWatch.Restart();
|
||
|
||
// 画面变化检测(很便宜,整帧采样约 9000 个点):退避期间靠它判断什么时候恢复全帧率。
|
||
// 面单拿走/换新/有人手伸进来都会让它变化,于是立刻恢复每帧检测。
|
||
CaptureScene(buffer, width, height, sceneNow);
|
||
var sceneChanged = hasScene && SceneDiff(scenePrev, sceneNow) > SceneChangeMeanDiff;
|
||
if (sceneChanged)
|
||
{
|
||
_steadyHits = 0;
|
||
_failStreak = 0;
|
||
}
|
||
|
||
Buffer.BlockCopy(sceneNow, 0, scenePrev, 0, sceneNow.Length);
|
||
hasScene = true;
|
||
|
||
// 退避:① 同一个码连续命中(面单没动)② 连续没结果(画面里没面单)。
|
||
// 两种都隔帧检测;画面一变立刻恢复。
|
||
var backoffSkipped = frameIndex % BackoffFrameModulo != 0
|
||
&& (_steadyHits >= SteadyBackoffAfter || _failStreak >= FailBackoffAfter);
|
||
|
||
string? code = null;
|
||
var path = "退避";
|
||
var candidates = 0;
|
||
|
||
// 本帧解出的**所有**条码(去重、按解出顺序)。多张面单同框时一个都不能丢。
|
||
var frameCodes = new List<string>(4);
|
||
|
||
if (!backoffSkipped)
|
||
{
|
||
decodeWatch.Restart();
|
||
|
||
// 整图检测:OpenCvSharp 的 BarcodeDetector 内部做梯度/多尺度搜索,
|
||
// 输出每个条码的 4 个角点(8 个 float 一组),天然带旋转和透视。
|
||
frameMat.SetArray(buffer);
|
||
detector.DetectAndDecode(frameMat, out var points, out _, out _);
|
||
candidates = points is null ? 0 : points.Length / 4;
|
||
|
||
// 「画面里有没有条码」是判断面单是否真的离开的唯一可靠信号(OpenCV 的 无候选 可信)。
|
||
// 离开超过 RepackAbsence 才重新出现 → 认定为真正的新一轮打包(例如装错了重新打包)。
|
||
var visibleAt = DateTime.Now;
|
||
if (candidates > 0)
|
||
{
|
||
if (_barcodeVisibleAt is DateTime lastVisible
|
||
&& visibleAt - lastVisible >= LabelScanWorker.RepackAbsence)
|
||
{
|
||
// 锁存:面单刚回来那几帧可能「有候选但解不出」(运动模糊),
|
||
// 不能因为后面又检测到就丢掉这个标记。
|
||
_repackDetectedAt = visibleAt;
|
||
}
|
||
|
||
_barcodeVisibleAt = visibleAt;
|
||
}
|
||
|
||
Runtime.LastCvMs = decodeWatch.Elapsed.TotalMilliseconds;
|
||
decodeWatch.Restart();
|
||
|
||
// ⚠️ 一帧里的**每个**候选都要解,不能解出第一个就停:
|
||
// 角点顺序由检测器决定,跟「哪张是新面单」没有关系,
|
||
// 只认第一个 → 两张面单并排时必然只报旧码、漏掉新码。
|
||
for (var i = 0; i < candidates; i++)
|
||
{
|
||
var text = DecodeQuad(reader, frameMat, points!, i,
|
||
out var warpW, out var warpH, out var rasterMs, out var decodeMs);
|
||
|
||
// 与扫码枪同一条合法性校验:条码长度/字符集不合规的不算命中(压掉误读)
|
||
var decoded = LabelScanWorker.CleanBarcode(text);
|
||
if (decoded is null) continue;
|
||
|
||
if (!frameCodes.Contains(decoded)) frameCodes.Add(decoded);
|
||
|
||
if (code is null)
|
||
{
|
||
code = decoded;
|
||
decodeWatch.Stop();
|
||
Runtime.LastDecodeMs = decodeWatch.Elapsed.TotalMilliseconds;
|
||
Runtime.RoiText = $"条码 {warpW}x{warpH}";
|
||
Runtime.RoiRatio = Math.Round(rasterMs + decodeMs, 2);
|
||
path = "OpenCV解码";
|
||
}
|
||
}
|
||
|
||
if (frameCodes.Count > 1) path = $"OpenCV解码x{frameCodes.Count}";
|
||
|
||
if (code is null)
|
||
{
|
||
decodeWatch.Stop();
|
||
Runtime.LastDecodeMs = decodeWatch.Elapsed.TotalMilliseconds;
|
||
Runtime.RoiText = candidates == 0 ? "无候选" : $"候选{candidates}个未解出";
|
||
Runtime.RoiRatio = 0;
|
||
path = candidates == 0 ? "无候选" : "候选未解出";
|
||
}
|
||
}
|
||
|
||
if (backoffSkipped)
|
||
{
|
||
path = _steadyHits >= SteadyBackoffAfter ? "稳态退避" : "失败退避";
|
||
}
|
||
|
||
// 条码历史:本帧解出的**每个**码(不管以前出现过没有)都刷新「最后出现时间」,
|
||
// 顺便算出哪些是历史里没有的新码
|
||
var now = DateTime.Now;
|
||
var fresh = RememberCodes(frameCodes, now);
|
||
var current = CurrentCode(frameCodes);
|
||
|
||
// 计数维护:命中就累计稳态、清失败;真跑过但没结果就累计失败、清稳态
|
||
if (current is not null)
|
||
{
|
||
_steadyHits = string.Equals(current, _lastHitCode, StringComparison.Ordinal) ? _steadyHits + 1 : 1;
|
||
_lastHitCode = current;
|
||
_failStreak = 0;
|
||
}
|
||
else if (!backoffSkipped && !sceneChanged)
|
||
{
|
||
_failStreak++;
|
||
_steadyHits = 0;
|
||
}
|
||
|
||
Runtime.LastPath = path;
|
||
Runtime.RoiFailStreak = _failStreak;
|
||
Runtime.SteadyHits = _steadyHits;
|
||
Runtime.RecentCodes = string.Join(" | ", _codeHistory.Select(x => x.Code));
|
||
|
||
if (frameCodes.Count == 0) continue;
|
||
|
||
// 这一帧是不是「面单离开过、刚回来」后的第一次上报(真正的新一轮打包)。
|
||
// 锁存时间最多认 60 秒:超过说明中间又走了别的流程,不再算这次重新打包。
|
||
var afterAbsence = _repackDetectedAt is DateTime repackAt
|
||
&& now >= repackAt
|
||
&& now - repackAt <= TimeSpan.FromSeconds(60);
|
||
if (afterAbsence) _repackDetectedAt = null;
|
||
|
||
// 上报规则(多码同框时按这个走):
|
||
// 1. **历史里没有的新码 → 立即上报**(同帧出现几个新码就报几个,一个都不漏);
|
||
// 2. 历史里已有的旧码 → 只刷新在场,不重复建档/播报;
|
||
// 3. 两个码都在历史里时,真正上报/续期的是**最后出现的那个**(current),
|
||
// 不是检测器先解出来的那个。
|
||
foreach (var item in frameCodes)
|
||
{
|
||
var isFresh = fresh.Contains(item);
|
||
var isCurrent = string.Equals(item, current, StringComparison.Ordinal);
|
||
if (!isFresh && !isCurrent) continue;
|
||
|
||
// 同一个码一直在画面里:节流到 500ms 一次,只续「在场」,不重复上报
|
||
if (!isFresh
|
||
&& string.Equals(item, _lastSeenCode, StringComparison.Ordinal)
|
||
&& (now - _lastReportAt).TotalMilliseconds < 500) continue;
|
||
|
||
ReportHit(item, afterAbsence, now);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 把本帧解出的条码登记进历史,返回「本帧新出现」的码。
|
||
///
|
||
/// 排序规则(末尾 = 最后出现的那个):
|
||
/// - **一直在画面里**的码(上一帧也看得见)只更新时间、**不重排** ——
|
||
/// 否则同框的两个码每帧按解出顺序互相顶掉,「当前码」就会来回跳、上报跟着抖;
|
||
/// - **离开过又回来**的码挪到末尾(它才是最后出现的那个);
|
||
/// - 超过 <see cref="LabelScanWorker.RepackAbsence"/> 没再出现的码直接淘汰 ——
|
||
/// 之后再出现就是新的一轮打包,按新码上报(与 ScanService 的在场判定同一个阈值)。
|
||
/// </summary>
|
||
private List<string> RememberCodes(List<string> frameCodes, DateTime now)
|
||
{
|
||
var fresh = new List<string>(frameCodes.Count);
|
||
|
||
for (var i = _codeHistory.Count - 1; i >= 0; i--)
|
||
{
|
||
if (now - _codeHistory[i].At >= LabelScanWorker.RepackAbsence) _codeHistory.RemoveAt(i);
|
||
}
|
||
|
||
foreach (var item in frameCodes)
|
||
{
|
||
var index = _codeHistory.FindIndex(x => string.Equals(x.Code, item, StringComparison.Ordinal));
|
||
if (index < 0)
|
||
{
|
||
fresh.Add(item);
|
||
_codeHistory.Add((item, now));
|
||
continue;
|
||
}
|
||
|
||
if (_visibleLastFrame.Contains(item))
|
||
{
|
||
_codeHistory[index] = (item, now);
|
||
continue;
|
||
}
|
||
|
||
// 上一帧没看见 → 现在回来了:它就是「最后出现」的那个
|
||
_codeHistory.RemoveAt(index);
|
||
_codeHistory.Add((item, now));
|
||
}
|
||
|
||
_visibleLastFrame.Clear();
|
||
foreach (var item in frameCodes) _visibleLastFrame.Add(item);
|
||
|
||
while (_codeHistory.Count > CodeHistorySize)
|
||
{
|
||
// 淘汰最旧的那个,但**本帧还看得见的不能淘汰**:
|
||
// 淘汰掉的话下一帧它又成了「新码」,会被重复上报一次。
|
||
var evict = _codeHistory.FindIndex(x => !frameCodes.Contains(x.Code));
|
||
if (evict < 0) break;
|
||
_codeHistory.RemoveAt(evict);
|
||
}
|
||
|
||
return fresh;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 本帧的「当前码」= 需要上报/续期的那个:历史里**最后出现**、且本帧还看得见的码。
|
||
///
|
||
/// 两个码都在历史里时,这里给出的永远是后出现的那个,而不是检测器先解出来的那个。
|
||
/// </summary>
|
||
private string? CurrentCode(List<string> frameCodes)
|
||
{
|
||
for (var i = _codeHistory.Count - 1; i >= 0; i--)
|
||
{
|
||
if (frameCodes.Contains(_codeHistory[i].Code)) return _codeHistory[i].Code;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>记一次命中并丢进上报队列(扫描线程只入队,不等写记录/上报/通知安卓)</summary>
|
||
private void ReportHit(string code, bool afterAbsence, DateTime now)
|
||
{
|
||
_lastSeenCode = code;
|
||
_lastReportAt = now;
|
||
|
||
// 最近命中/最近条码每次都刷新,但「首次识别时间」只认记录里的时间(面单不拿走就不变)
|
||
Runtime.LastCode = code;
|
||
Runtime.LastHitAt = now;
|
||
Runtime.LastError = null;
|
||
|
||
EnqueueReport(code, Runtime.LastDecodeMs, Runtime.LastCvMs, afterAbsence);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 把 OpenCV 检测到的第 index 个四角点区域做透视矫正,交给 ZXing 解 Code128。
|
||
///
|
||
/// 为什么必须矫正:现场面单是**竖放或斜着**摆的(实测四角点 (433,602)(450,514)(1371,685)(1354,774)),
|
||
/// 1D 码要求扫描线完整穿过整条,轴对齐裁剪会把条码截断 → 必然解不出。
|
||
/// 矫正成轴对齐后条码完整,ZXing 一次就能读出。
|
||
/// </summary>
|
||
private static string? DecodeQuad(
|
||
BarcodeReaderGeneric reader,
|
||
OpenCvSharp.Mat frame,
|
||
OpenCvSharp.Point2f[] points,
|
||
int index,
|
||
out int warpedWidth,
|
||
out int warpedHeight,
|
||
out double rasterMs,
|
||
out double decodeMs)
|
||
{
|
||
warpedWidth = 0;
|
||
warpedHeight = 0;
|
||
rasterMs = 0;
|
||
decodeMs = 0;
|
||
|
||
try
|
||
{
|
||
var offset = index * 4;
|
||
var quad = OrderCorners(
|
||
points[offset], points[offset + 1], points[offset + 2], points[offset + 3]);
|
||
|
||
var wTop = Distance(quad[0], quad[1]);
|
||
var wBottom = Distance(quad[3], quad[2]);
|
||
var hLeft = Distance(quad[0], quad[3]);
|
||
var hRight = Distance(quad[1], quad[2]);
|
||
var outW = Math.Max(8, (int)Math.Round(Math.Max(wTop, wBottom)));
|
||
var outH = Math.Max(8, (int)Math.Round(Math.Max(hLeft, hRight)));
|
||
|
||
// 太小的框是误检(实测真实条码矫正后约 937x90)
|
||
if (outW < 40 || outH < 20) return null;
|
||
|
||
warpedWidth = outW;
|
||
warpedHeight = outH;
|
||
|
||
var rasterWatch = Stopwatch.StartNew();
|
||
var source = new[] { quad[0], quad[1], quad[2], quad[3] };
|
||
var target = new[]
|
||
{
|
||
new OpenCvSharp.Point2f(0, 0),
|
||
new OpenCvSharp.Point2f(outW - 1, 0),
|
||
new OpenCvSharp.Point2f(outW - 1, outH - 1),
|
||
new OpenCvSharp.Point2f(0, outH - 1),
|
||
};
|
||
|
||
using var transform = OpenCvSharp.Cv2.GetPerspectiveTransform(source, target);
|
||
using var warped = new OpenCvSharp.Mat();
|
||
OpenCvSharp.Cv2.WarpPerspective(frame, warped, transform, new OpenCvSharp.Size(outW, outH));
|
||
|
||
warped.GetArray(out byte[] raster);
|
||
rasterWatch.Stop();
|
||
rasterMs = rasterWatch.Elapsed.TotalMilliseconds;
|
||
|
||
var decodeWatch = Stopwatch.StartNew();
|
||
var text = reader.Decode(new PlanarYUVLuminanceSource(raster, outW, outH, 0, 0, outW, outH, false))?.Text;
|
||
decodeWatch.Stop();
|
||
decodeMs = decodeWatch.Elapsed.TotalMilliseconds;
|
||
|
||
return text;
|
||
}
|
||
catch
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
private static double Distance(OpenCvSharp.Point2f a, OpenCvSharp.Point2f b)
|
||
=> Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2));
|
||
|
||
/// <summary>把 4 个角点排成「左上、右上、右下、左下」,保证透视变换不翻面</summary>
|
||
private static OpenCvSharp.Point2f[] OrderCorners(
|
||
OpenCvSharp.Point2f p0, OpenCvSharp.Point2f p1, OpenCvSharp.Point2f p2, OpenCvSharp.Point2f p3)
|
||
{
|
||
var points = new[] { p0, p1, p2, p3 };
|
||
OpenCvSharp.Point2f topLeft = points[0], topRight = points[0], bottomRight = points[0], bottomLeft = points[0];
|
||
|
||
var minSum = double.MaxValue;
|
||
var maxSum = double.MinValue;
|
||
var minDiff = double.MaxValue;
|
||
var maxDiff = double.MinValue;
|
||
|
||
foreach (var point in points)
|
||
{
|
||
var sum = point.X + point.Y;
|
||
var diff = point.Y - point.X;
|
||
|
||
if (sum < minSum) { minSum = sum; topLeft = point; }
|
||
if (sum > maxSum) { maxSum = sum; bottomRight = point; }
|
||
if (diff < minDiff) { minDiff = diff; topRight = point; }
|
||
if (diff > maxDiff) { maxDiff = diff; bottomLeft = point; }
|
||
}
|
||
|
||
return new[] { topLeft, topRight, bottomRight, bottomLeft };
|
||
}
|
||
|
||
/// <summary>
|
||
/// 把整帧压成 32x18 的块均值缩略图(每格 4x4 采样取平均,约 9000 个采样点,<0.1ms)。
|
||
/// 只用来判断「画面有没有变化」,不参与识别。
|
||
/// </summary>
|
||
private static void CaptureScene(byte[] frame, int width, int height, byte[] target)
|
||
{
|
||
var cellWidth = Math.Max(1, width / SceneGridX);
|
||
var cellHeight = Math.Max(1, height / SceneGridY);
|
||
var stepX = Math.Max(1, cellWidth / SceneCellSamples);
|
||
var stepY = Math.Max(1, cellHeight / SceneCellSamples);
|
||
|
||
for (var gy = 0; gy < SceneGridY; gy++)
|
||
{
|
||
var y0 = gy * cellHeight;
|
||
var yEnd = Math.Min(y0 + cellHeight, height);
|
||
for (var gx = 0; gx < SceneGridX; gx++)
|
||
{
|
||
var x0 = gx * cellWidth;
|
||
var xEnd = Math.Min(x0 + cellWidth, width);
|
||
var sum = 0;
|
||
var count = 0;
|
||
for (var y = y0; y < yEnd; y += stepY)
|
||
{
|
||
var rowBase = y * width;
|
||
for (var x = x0; x < xEnd; x += stepX)
|
||
{
|
||
sum += frame[rowBase + x];
|
||
count++;
|
||
}
|
||
}
|
||
|
||
target[gy * SceneGridX + gx] = count > 0 ? (byte)(sum / count) : (byte)0;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>两张缩略图的平均灰度差(0~255)</summary>
|
||
private static double SceneDiff(byte[] previous, byte[] current)
|
||
{
|
||
var sum = 0;
|
||
for (var i = 0; i < previous.Length; i++)
|
||
{
|
||
var delta = previous[i] - current[i];
|
||
sum += delta < 0 ? -delta : delta;
|
||
}
|
||
|
||
return (double)sum / previous.Length;
|
||
}
|
||
|
||
private void SetState(string state, string? error)
|
||
{
|
||
var changed = Runtime.State != state || Runtime.LastError != error;
|
||
Runtime.State = state;
|
||
Runtime.LastError = error;
|
||
if (changed) _changed();
|
||
}
|
||
|
||
private static Process NewProcess(string fileName, IEnumerable<string> args)
|
||
{
|
||
var info = new ProcessStartInfo(fileName)
|
||
{
|
||
// stdout/stderr 都由调用方按需打开;stdin 一定要重定向,避免子进程读控制台卡住
|
||
RedirectStandardInput = true,
|
||
UseShellExecute = false,
|
||
CreateNoWindow = true,
|
||
};
|
||
foreach (var arg in args) info.ArgumentList.Add(arg);
|
||
return new Process { StartInfo = info };
|
||
}
|
||
|
||
private static void TryKill(Process? process)
|
||
{
|
||
if (process is null) return;
|
||
try
|
||
{
|
||
if (!process.HasExited) process.Kill(entireProcessTree: true);
|
||
}
|
||
catch
|
||
{
|
||
// 忽略
|
||
}
|
||
finally
|
||
{
|
||
try
|
||
{
|
||
process.Dispose();
|
||
}
|
||
catch
|
||
{
|
||
// 忽略
|
||
}
|
||
}
|
||
}
|
||
}
|