CameraGroup.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // CameraGroup.cs 相机组线程管理类,控制相机采集和识别的线程运行,开放有算法的接口
  2. using MvCameraControl;
  3. using MvvmScaffoldFrame48.DLL.CameraTools;
  4. using MvvmScaffoldFrame48.DLL.LogTools;
  5. using MvvmScaffoldFrame48.DLL.SystemTools;
  6. using MvvmScaffoldFrame48.Model.ResultModel;
  7. using MvvmScaffoldFrame48.Model.StorageModel.SystemConfig;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MvvmScaffoldFrame48.DLL.ThreadManager
  14. {
  15. /// <summary>
  16. /// 相机组配置和状态管理类
  17. /// 管理单个相机的采集和处理线程及相关资源
  18. /// </summary>
  19. public class CameraGroup
  20. {
  21. #region 变量与实例
  22. //相机线程的休眠时间,默认为0,即不休眠。在采集节拍没有那么高时,增加此值降低性能消耗
  23. private int CameraSleepTime = 0;
  24. //帧编号记录
  25. private long lastframeNum = -1;
  26. //相机
  27. public HikCamera Camera { get; set; }
  28. //相机ID
  29. public int CameraId { get; set; }
  30. //是否运行中
  31. public bool IsRunning { get; set; }
  32. //相机处理线程
  33. public Task CameraTask { get; set; }
  34. //图像处理线程
  35. public Task ProcessingTask { get; set; }
  36. //图像队列-ConcurrentQueue
  37. public ConcurrentQueue<IFrameOut> ImageQueue { get; set; }
  38. //图像队列-自定义RingBuffer
  39. public LockFreeRingBuffer<IFrameOut> ImageRingBuffer = new LockFreeRingBuffer<IFrameOut>(30);
  40. //信号量
  41. public SemaphoreSlim QueueSemaphore { get; set; }
  42. //取消令牌
  43. public CancellationTokenSource CancellationTokenSource { get; set; }
  44. // 图像处理算法(接口方式)
  45. public IImageProcessingAlgorithmHikVision ImageProcessor { get; set; }
  46. // 结果发送事件,当图像处理完成时触发
  47. public event EventHandler<CameraProcessEventArgsResultModel> ProcessResultAvailable;
  48. // 参数配置
  49. public CameraProcessConfigModel Configuration { get; set; }
  50. #endregion
  51. #region 公共方法
  52. /// <summary>
  53. /// 启动相机组(包括采集和处理线程)
  54. /// </summary>
  55. public void Start()
  56. {
  57. if (Camera == null)
  58. {
  59. Console.WriteLine($"相机 {CameraId} 未初始化");
  60. return;
  61. }
  62. if (Camera.Device == null)
  63. {
  64. Console.WriteLine($"相机 {CameraId} 未初始化");
  65. return;
  66. }
  67. if (Camera.Device.IsConnected == false)
  68. {
  69. Console.WriteLine($"相机 {CameraId} 未打开");
  70. return;
  71. }
  72. if (IsRunning)
  73. return;
  74. // 根据配置加载算法
  75. LoadAlgorithmFromConfiguration();
  76. //Camera.StartReceiveFuntion();
  77. Camera.FrameGrabbedEvent += CameraCaptureLoop2;
  78. Camera.StartReceiveFuntionSetFrameGrabedEvent();
  79. // 重置取消令牌
  80. CancellationTokenSource = new CancellationTokenSource();
  81. var token = CancellationTokenSource.Token;
  82. // 启动相机采集线程
  83. //CameraTask = Task.Factory.StartNew(() => CameraCaptureLoop(token),
  84. // token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
  85. // 启动图像处理线程
  86. ProcessingTask = Task.Factory.StartNew(() => ImageProcessingbatchLoop2(token),
  87. token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
  88. IsRunning = true;
  89. Console.WriteLine($"相机组 {CameraId} 已启动");
  90. }
  91. /// <summary>
  92. /// 停止相机组
  93. /// </summary>
  94. public async void Stop()
  95. {
  96. if (!IsRunning)
  97. return;
  98. // 发送取消信号
  99. CancellationTokenSource.Cancel();
  100. // 等待线程完成
  101. try
  102. {
  103. if (CameraTask != null)
  104. await CameraTask;
  105. if (ProcessingTask != null)
  106. await ProcessingTask;
  107. }
  108. catch (OperationCanceledException)
  109. {
  110. // 正常取消,忽略异常
  111. }
  112. IsRunning = false;
  113. Camera.StopReceiveFuntionSetFrameGrabedEvent();
  114. Console.WriteLine($"相机组 {CameraId} 已停止");
  115. }
  116. /// <summary>
  117. /// 更新算法参数
  118. /// </summary>
  119. /// <param name="parameters">新参数</param>
  120. public void UpdateAlgorithmParameters(object parameters)
  121. {
  122. if (ImageProcessor != null && parameters != null)
  123. {
  124. string parametersjson = ImageProcessor.GetSaveJson();
  125. ImageProcessor.Configure(parametersjson);
  126. // 更新配置
  127. if (Configuration != null)
  128. {
  129. Configuration.AlgorithmParameters = parametersjson;
  130. }
  131. Console.WriteLine($"相机 {CameraId} 算法参数已更新");
  132. }
  133. }
  134. #endregion
  135. #region 私有方法
  136. /// <summary>
  137. /// 设置图像处理算法
  138. /// </summary>
  139. /// <param name="algorithmName">算法名称</param>
  140. private void SetProcessingAlgorithm(string algorithmName, string parameters = null)
  141. {
  142. if (string.IsNullOrEmpty(algorithmName))
  143. {
  144. ImageProcessor = null;
  145. return;
  146. }
  147. try
  148. {
  149. // 根据算法名称创建实例(这里可以使用简单的工厂方法或反射)
  150. ImageProcessor = ImageProcessingAlgorithmHikVisionFactory.CreateAlgorithm(algorithmName);
  151. // 配置参数
  152. if (parameters != null && ImageProcessor != null)
  153. {
  154. ImageProcessor.Configure(parameters);
  155. }
  156. Console.WriteLine($"相机 {CameraId} 成功设置算法: {algorithmName}");
  157. }
  158. catch (Exception ex)
  159. {
  160. Console.WriteLine($"相机 {CameraId} 设置算法 {algorithmName} 失败: {ex.Message}");
  161. }
  162. }
  163. /// <summary>
  164. /// 根据配置动态加载算法
  165. /// </summary>
  166. private void LoadAlgorithmFromConfiguration()
  167. {
  168. if (Configuration != null && !string.IsNullOrEmpty(Configuration.ProcessingAlgorithmName))
  169. {
  170. SetProcessingAlgorithm(Configuration.ProcessingAlgorithmName, Configuration.AlgorithmParameters);
  171. }
  172. else
  173. {
  174. // 使用默认算法(无参数)
  175. SetProcessingAlgorithm("Default");
  176. }
  177. }
  178. /// <summary>
  179. /// 加载相机配置
  180. /// </summary>
  181. private void LoadCameraConfiguration()
  182. {
  183. if(Configuration != null&& !string.IsNullOrEmpty(Configuration.CameraParameters))
  184. {
  185. }
  186. }
  187. /// <summary>
  188. /// 发送处理结果到通信线程的方法
  189. /// 多个线程可以共用此方法
  190. /// </summary>
  191. /// <param name="resultData">处理结果数据</param>
  192. private void SendProcessResult(object resultData)
  193. {
  194. var eventArgs = new CameraProcessEventArgsResultModel
  195. {
  196. CameraId = this.CameraId,
  197. ResultData = resultData,
  198. Timestamp = DateTime.Now
  199. };
  200. // 触发事件,通知订阅者有新的处理结果
  201. ProcessResultAvailable?.Invoke(this, eventArgs);
  202. }
  203. /// <summary>
  204. /// 图像处理实现
  205. /// </summary>
  206. private void ProcessImage(IFrameOut imageData, int cameraId)
  207. {
  208. object resultData = null;
  209. try
  210. {
  211. // 优先使用接口方式的算法
  212. if (ImageProcessor != null && imageData!=null)
  213. {
  214. resultData = ImageProcessor.ProcessImage(imageData, cameraId);
  215. }
  216. // 如果都没有设置,则使用默认处理
  217. else
  218. {
  219. Console.WriteLine($"相机 {CameraId} 未加载算法");
  220. // 默认处理逻辑
  221. }
  222. if(resultData!=null)
  223. {
  224. // 发送处理结果
  225. SendProcessResult(resultData);
  226. }
  227. // 输出处理结果
  228. }
  229. catch (Exception ex)
  230. {
  231. Console.WriteLine($"相机 {CameraId} 图像处理错误: {ex.Message}");
  232. // 发送错误结果
  233. }
  234. }
  235. #endregion
  236. #region 线程主体方法
  237. /// <summary>
  238. /// 相机采集循环
  239. /// </summary>
  240. private async void CameraCaptureLoop(CancellationToken token)
  241. {
  242. //int frameCount = 0;
  243. try
  244. {
  245. while (!token.IsCancellationRequested)
  246. {
  247. // 模拟相机图像采集(无休眠,持续采集)
  248. if (Camera.GetOnceImage(out IFrameOut imageData))
  249. {
  250. // 将图像放入队列
  251. ImageQueue.Enqueue(imageData.Clone() as IFrameOut);
  252. double QuTuYanshi = (DateTime.Now - TimeStampTools.FromUnixTimestamp((long)imageData.HostTimeStamp)).TotalMilliseconds;
  253. //Console.WriteLine($"相机 {CameraId} 采集到图像,帧号{imageData.FrameNum}");
  254. if(QuTuYanshi > 12)
  255. {
  256. Console.WriteLine($"识别落后时间{QuTuYanshi}");
  257. }
  258. QueueSemaphore.Release(); // 通知处理线程有新数据
  259. imageData.Dispose();
  260. }
  261. // 这里不添加休眠,保持最大帧率采集
  262. await Task.Delay(CameraSleepTime);
  263. }
  264. }
  265. catch (OperationCanceledException)
  266. {
  267. // 线程被取消,正常退出
  268. }
  269. catch (Exception ex)
  270. {
  271. Console.WriteLine($"相机 {CameraId} 采集异常: {ex.Message}");
  272. }
  273. }
  274. /// <summary>
  275. /// 相机回调取图
  276. /// 与循环取图二选一
  277. /// </summary>
  278. /// <param name="sender"></param>
  279. /// <param name="e"></param>
  280. private void CameraCaptureLoop(object sender, FrameGrabbedEventArgs e)
  281. {
  282. try
  283. {
  284. // 将图像放入队列
  285. ImageQueue.Enqueue(e.FrameOut.Clone() as IFrameOut);
  286. double QuTuYanshi = (DateTime.Now - TimeStampTools.FromUnixTimestamp((long)e.FrameOut.HostTimeStamp)).TotalMilliseconds;
  287. //Console.WriteLine($"相机 {CameraId} 采集到图像,帧号{imageData.FrameNum}");
  288. if (QuTuYanshi > 12)
  289. {
  290. Console.WriteLine($"识别落后时间{QuTuYanshi}");
  291. }
  292. QueueSemaphore.Release(); // 通知处理线程有新数据
  293. e.FrameOut.Dispose();
  294. }
  295. catch (Exception ex)
  296. {
  297. Console.WriteLine($"相机 {CameraId} 采集异常: {ex.Message}");
  298. }
  299. }
  300. /// <summary>
  301. /// 相机回调取图
  302. /// 更换了存储图像的队列
  303. /// </summary>
  304. /// <param name="sender"></param>
  305. /// <param name="e"></param>
  306. private void CameraCaptureLoop2(object sender, FrameGrabbedEventArgs e)
  307. {
  308. try
  309. {
  310. // 将图像放入队列
  311. ImageRingBuffer.TryEnqueue(e.FrameOut.Clone() as IFrameOut);
  312. double QuTuYanshi = (DateTime.Now - TimeStampTools.FromUnixTimestamp((long)e.FrameOut.HostTimeStamp)).TotalMilliseconds;
  313. //Console.WriteLine($"相机 {CameraId} 采集到图像,帧号{imageData.FrameNum}");
  314. if (QuTuYanshi > 12)
  315. {
  316. Console.WriteLine($"识别落后时间{QuTuYanshi}");
  317. }
  318. if (lastframeNum == -1)
  319. {
  320. lastframeNum = e.FrameOut.FrameNum;
  321. }
  322. else if (lastframeNum == e.FrameOut.FrameNum - 1)
  323. {
  324. lastframeNum = e.FrameOut.FrameNum;
  325. }
  326. else
  327. {
  328. //丢帧记录
  329. TxtLog.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
  330. e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum, lastframeNum), 6);
  331. Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
  332. e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum, lastframeNum);
  333. lastframeNum = e.FrameOut.FrameNum;
  334. }
  335. QueueSemaphore.Release(); // 通知处理线程有新数据
  336. e.FrameOut.Dispose();
  337. }
  338. catch (Exception ex)
  339. {
  340. Console.WriteLine($"相机 {CameraId} 采集异常: {ex.Message}");
  341. }
  342. }
  343. /// <summary>
  344. /// 图像处理循环
  345. /// 适用取图频率不高的处理,或者处理速度远快于取图速度的方法
  346. /// </summary>
  347. private async void ImageProcessingLoop(CancellationToken token)
  348. {
  349. try
  350. {
  351. while (!token.IsCancellationRequested)
  352. {
  353. // 等待图像数据
  354. await QueueSemaphore.WaitAsync(token);
  355. // 从队列获取图像
  356. if (ImageQueue.TryDequeue(out IFrameOut imageData))
  357. {
  358. // 执行图像处理逻辑
  359. ProcessImage(imageData, CameraId);
  360. imageData.Dispose();
  361. // 可以根据队列长度决定是否短暂休眠以避免过度占用CPU
  362. if (ImageQueue.Count == 0)
  363. {
  364. await Task.Delay(1, token); // 短暂让出CPU
  365. }
  366. }
  367. }
  368. }
  369. catch (OperationCanceledException)
  370. {
  371. // 线程被取消,正常退出
  372. }
  373. catch (Exception ex)
  374. {
  375. Console.WriteLine($"相机 {CameraId} 处理异常: {ex.Message}");
  376. }
  377. }
  378. /// <summary>
  379. /// 图像处理循环-引入批处理
  380. /// 处理速度与取图速度的差不多的时候
  381. /// </summary>
  382. /// <param name="token"></param>
  383. private async void ImageProcessingbatchLoop(CancellationToken token)
  384. {
  385. const int batchSize = 5; // 每批处理的最大图像数量
  386. var batch = new List<IFrameOut>(batchSize); // 存储当前批次的图像
  387. try
  388. {
  389. while (!token.IsCancellationRequested)
  390. {
  391. // 等待至少一张图像数据
  392. await QueueSemaphore.WaitAsync(token);
  393. // 收集一批图像(严格按队列顺序)
  394. while (batch.Count < batchSize && ImageQueue.TryDequeue(out IFrameOut imageData))
  395. {
  396. batch.Add(imageData);
  397. if (ImageQueue.Count > 0)
  398. {
  399. QueueSemaphore.Release(); // 提前释放信号量,唤醒其他等待线程
  400. }
  401. }
  402. // 顺序处理当前批次的所有图像
  403. foreach (var image in batch)
  404. {
  405. ProcessImage(image, CameraId); // 严格按顺序执行处理逻辑
  406. image.Dispose(); // 处理完成后立即释放资源
  407. }
  408. // 清空当前批次,准备下一轮处理
  409. batch.Clear();
  410. // 如果队列为空,短暂休眠以避免过度占用CPU
  411. if (ImageQueue.Count == 0)
  412. {
  413. await Task.Delay(1, token); // 固定短时间休眠
  414. }
  415. }
  416. }
  417. catch (OperationCanceledException)
  418. {
  419. // 线程被取消,正常退出
  420. }
  421. catch (Exception ex)
  422. {
  423. Console.WriteLine($"相机 {CameraId} 处理异常: {ex.Message}");
  424. }
  425. }
  426. private async void ImageProcessingbatchLoop2(CancellationToken token)
  427. {
  428. const int batchSize = 5; // 每批处理的最大图像数量
  429. var batch = new List<IFrameOut>(batchSize); // 存储当前批次的图像
  430. try
  431. {
  432. while (!token.IsCancellationRequested)
  433. {
  434. // 等待至少一张图像数据
  435. await QueueSemaphore.WaitAsync(token);
  436. // 收集一批图像(严格按队列顺序)
  437. while (batch.Count < batchSize && ImageRingBuffer.TryDequeue(out IFrameOut imageData))
  438. {
  439. if (imageData == null) continue;
  440. batch.Add(imageData);
  441. if (ImageRingBuffer.Count > 0)
  442. {
  443. QueueSemaphore.Release(); // 提前释放信号量,唤醒其他等待线程
  444. }
  445. }
  446. // 顺序处理当前批次的所有图像
  447. foreach (var image in batch)
  448. {
  449. ProcessImage(image, CameraId); // 严格按顺序执行处理逻辑
  450. image.Dispose(); // 处理完成后立即释放资源
  451. }
  452. // 清空当前批次,准备下一轮处理
  453. batch.Clear();
  454. // 如果队列为空,短暂休眠以避免过度占用CPU
  455. if (ImageRingBuffer.Count == 0)
  456. {
  457. await Task.Delay(1, token); // 固定短时间休眠
  458. }
  459. }
  460. }
  461. catch (OperationCanceledException)
  462. {
  463. // 线程被取消,正常退出
  464. }
  465. catch (Exception ex)
  466. {
  467. Console.WriteLine($"相机 {CameraId} 处理异常: {ex.Message}");
  468. }
  469. }
  470. #endregion
  471. }
  472. }