MainWindow.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Threading;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using Microsoft.Win32;
  16. using System.IO;
  17. using AutoEFInferenceCalcLib;
  18. using System.Threading;
  19. using Accord.Video.FFMPEG;
  20. using Accord.Video.DirectShow;
  21. using Accord.Video;
  22. using AI.Common;
  23. using System.Drawing;
  24. using ImageShowUtilsLib;
  25. namespace AutoEFCalculationDemo
  26. {
  27. /// <summary>
  28. /// MainWindow.xaml 的交互逻辑
  29. /// </summary>
  30. public partial class MainWindow : Window
  31. {
  32. #region field
  33. private bool _enableAutoEF = true;
  34. private bool _enableShowObjectContour = true;
  35. private bool _showMeasureLine = true;
  36. private bool _showMeasureCurve = true;
  37. private bool _showTimeSeriesResult = true;
  38. private int _numCpu;
  39. private AutoEFCalculation _autoEfCalculation = null;
  40. private ManualResetEvent _videoPlayEvent = new ManualResetEvent(false);
  41. private Thread _videoPlayThread = null;
  42. private VideoFileReader _videoReader = null;
  43. private readonly object _videoReaderLocker = new object();
  44. private VideoCaptureDevice _videoCapture;
  45. private volatile bool _readingVideo = false;
  46. private volatile bool _pausing = false;
  47. private string _calculationId;
  48. private double _totalDurationTime = 10000.0;
  49. private float _cmPerPixel = 0.026f;
  50. private double _testIntervalTime = 0.0;
  51. private double _pushImageTime = 0.0;
  52. private volatile int _frameCount = 0;
  53. private volatile int _frameIndex = 0;
  54. private volatile int _frameIntervalTime = 0;
  55. private volatile int _displayCount = 0;
  56. private volatile int _lastDetectTickCount;
  57. private volatile int _fpsStartTickCount;
  58. private volatile bool _continuouslyDetecting = false;
  59. #endregion
  60. public MainWindow()
  61. {
  62. InitializeComponent();
  63. TextTestInterval.Text = _testIntervalTime.ToString();
  64. CheckEnableAI.IsChecked = _enableAutoEF;
  65. CheckShowObjectContour.IsChecked = _enableShowObjectContour;
  66. CheckShowMesureLine.IsChecked = _showMeasureLine;
  67. //CheckShowMeasureCurve.IsChecked = _showMeasureCurve;
  68. //CheckShowTimeSeriesResult.IsChecked = _showTimeSeriesResult;
  69. ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
  70. ResultShowGrid.SetShowMeasureCurve(_showMeasureCurve);
  71. ResultShowGrid.SetShowMeasureLine(_enableShowObjectContour);
  72. ResultShowGrid.SetShowTimeSeriesResult(_showTimeSeriesResult);
  73. try
  74. {
  75. _numCpu = Convert.ToInt32(4);
  76. int coreCount = Environment.ProcessorCount * 2;
  77. if(_numCpu <= 0 || _numCpu > coreCount)
  78. {
  79. throw new Exception("CPU数目设置错误,只能为正整数且小于本机CPU线程总数!");
  80. }
  81. string netDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
  82. _autoEfCalculation = new AutoEFCalculation(_numCpu, netDir);
  83. }
  84. catch (Exception ex)
  85. {
  86. MessageBox.Show("AutoEFCalculation, 模型加载失败!");
  87. return;
  88. }
  89. }
  90. private void OnClickStartTest(object sender, RoutedEventArgs e)
  91. {
  92. StopVideoPlayAndCapture();
  93. var ofd = new OpenFileDialog();
  94. ofd.Filter = "视频| *.mp4; *.avi";
  95. ofd.Multiselect = false;
  96. ofd.Title = "选择一个视频";
  97. if(ofd.ShowDialog() == true)
  98. {
  99. var fileName = ofd.FileName;
  100. var fileExtension = Path.GetExtension(fileName);
  101. _calculationId = _autoEfCalculation.StartCalculation(_totalDurationTime, _cmPerPixel, _testIntervalTime);
  102. _pushImageTime = 0;
  103. if(fileExtension ==".mp4" || fileExtension == ".avi")
  104. {
  105. lock(_videoReaderLocker)
  106. {
  107. _videoReader = new VideoFileReader();
  108. _videoReader.Open(fileName);
  109. var frameRate = _videoReader.FrameRate.ToDouble();
  110. TextRealVideoFPS.Text = frameRate.ToString("0.00") + "fps";
  111. _frameIntervalTime = (int)(1000 / frameRate);
  112. _frameCount = (int)_videoReader.FrameCount;
  113. _frameIndex = 0;
  114. }
  115. _readingVideo = true;
  116. ProgressBarVideoPlay.IsEnabled = true;
  117. BtnBack.IsEnabled = true;
  118. BtnForward.IsEnabled = true;
  119. BtnPauseOrPlay.IsEnabled = true;
  120. BtnPauseOrPlay.Content = "暂停";
  121. ProgressBarVideoPlay.Minimum = 0;
  122. ProgressBarVideoPlay.Maximum = _frameCount;
  123. _lastDetectTickCount = Environment.TickCount;
  124. _fpsStartTickCount = _lastDetectTickCount;
  125. _displayCount = 0;
  126. _videoPlayEvent.Reset();
  127. _pausing = false;
  128. ResultShowGrid.ClearDiagResult();
  129. if (_videoPlayThread == null || !_videoPlayThread.IsAlive)
  130. {
  131. _videoPlayThread = new Thread(() => DoVideoPlay())
  132. {
  133. IsBackground = true,
  134. Name = "AI_AUTOEF_VideoPlay",
  135. };
  136. _videoPlayThread.Start();
  137. }
  138. }
  139. }
  140. }
  141. private void DoVideoPlay()
  142. {
  143. _continuouslyDetecting = true;
  144. while(!_pausing)
  145. {
  146. var currentTickCount = Environment.TickCount;
  147. Bitmap bitmap;
  148. lock(_videoReaderLocker)
  149. {
  150. bitmap = _videoReader?.ReadVideoFrame(_frameIndex);
  151. }
  152. if (bitmap == null)
  153. {
  154. break;
  155. }
  156. ShowNewFrame(bitmap);
  157. bitmap.Dispose();
  158. //更新帧率
  159. _displayCount++;
  160. UpdateFps();
  161. //最后还剩多少时间sleep
  162. var detectTimeUsed = Environment.TickCount - currentTickCount;
  163. var finalSleepTime = _frameIntervalTime - detectTimeUsed;
  164. if(finalSleepTime < 0)
  165. {
  166. finalSleepTime = 0;
  167. }
  168. if(_videoPlayEvent.WaitOne(finalSleepTime))
  169. {
  170. break;
  171. }
  172. //如果到尾了,就从头开始
  173. if(_frameIndex == _frameCount - 1)
  174. {
  175. _frameIndex = 0;
  176. }
  177. else
  178. {
  179. _frameIndex += 1;
  180. }
  181. //更新进度条
  182. Dispatcher.Invoke(() =>
  183. {
  184. ProgressBarVideoPlay.Value = _frameIndex;
  185. TextFrameIndex.Text = _frameIndex.ToString();
  186. });
  187. }
  188. _continuouslyDetecting = false;
  189. }
  190. private void ShowNewFrame(Bitmap bitmap, RawImage rawImage = null)
  191. {
  192. ResultShowGrid.UpdateImage(_pushImageTime, bitmap);
  193. if(_enableAutoEF)
  194. {
  195. //当满足时间间隔要求,则进行检测
  196. var currentTickCount = Environment.TickCount;
  197. if(_autoEfCalculation != null)
  198. {
  199. DetectImage(bitmap, rawImage);
  200. }
  201. var detectTimeUsed = Environment.TickCount - currentTickCount;
  202. //更新显示实际耗时
  203. UpdateTestTime(detectTimeUsed);
  204. }
  205. }
  206. private void DetectImage(Bitmap bitmap, RawImage rawImage = null)
  207. {
  208. _lastDetectTickCount = Environment.TickCount;
  209. RawImage rawImageToEval = null;
  210. if(rawImage == null)
  211. {
  212. rawImageToEval = RawImageShowUtils.BitmapToRawImage(bitmap);
  213. }
  214. else
  215. {
  216. rawImageToEval = rawImage;
  217. }
  218. //视频时间戳
  219. double currentTimeStamp = _pushImageTime;
  220. CardiacCurveInfos result = _autoEfCalculation.PushOneImageSync(_calculationId,
  221. rawImageToEval, currentTimeStamp);
  222. ResultShowGrid.UpdateDiagResult(_calculationId, currentTimeStamp, result);
  223. _pushImageTime += 33;//_frameIntervalTime
  224. }
  225. #region back forward stop
  226. private void OnClickStop(object sender, RoutedEventArgs e)
  227. {
  228. if(!_readingVideo)
  229. {
  230. return;
  231. }
  232. if(_continuouslyDetecting)
  233. {
  234. //停止自动播放
  235. _pausing = true;
  236. _videoPlayEvent.Set();
  237. BtnPauseOrPlay.Content = "播放";
  238. }
  239. else
  240. {
  241. //开始自动播放
  242. _videoPlayEvent.Reset();
  243. _pausing = false;
  244. if(_videoPlayThread == null || !_videoPlayThread.IsAlive)
  245. {
  246. _videoPlayThread = new Thread(() => DoVideoPlay())
  247. {
  248. IsBackground = true,
  249. Name = "AI_AUTOEF_VideoPlay",
  250. };
  251. _videoPlayThread.Start();
  252. }
  253. BtnPauseOrPlay.Content = "暂停";
  254. }
  255. }
  256. private void OnClickBackward(object sender, RoutedEventArgs e)
  257. {
  258. if(!_readingVideo)
  259. {
  260. return ;
  261. }
  262. if( _continuouslyDetecting)
  263. {
  264. //停止自动播放
  265. _videoPlayEvent.Set();
  266. _pausing = true;
  267. BtnPauseOrPlay.Content = "播放";
  268. }
  269. //往后退一帧
  270. if(_frameIndex == 0)
  271. {
  272. _frameIndex = _frameCount - 1;
  273. }
  274. else
  275. {
  276. _frameIndex -= 1;
  277. }
  278. ProgressBarVideoPlay.Value = _frameIndex;
  279. TextFrameIndex.Text = _frameIndex.ToString();
  280. //显示当前帧
  281. Task.Run(() =>
  282. {
  283. Bitmap bitmap;
  284. lock(_videoReaderLocker)
  285. {
  286. bitmap = _videoReader.ReadVideoFrame(_frameIndex);
  287. }
  288. ShowNewFrame(bitmap);
  289. bitmap.Dispose();
  290. });
  291. }
  292. private void OnClickForward(object sender, RoutedEventArgs e)
  293. {
  294. if(!_readingVideo)
  295. {
  296. return;
  297. }
  298. if( _continuouslyDetecting)
  299. {
  300. //停止自动播放
  301. _videoPlayEvent.Set();
  302. _pausing = true;
  303. BtnPauseOrPlay.Content = "播放";
  304. }
  305. //往前一帧
  306. if(_frameIndex == _frameCount -1)
  307. {
  308. _frameIndex = 0;
  309. }
  310. else
  311. {
  312. _frameIndex += 1;
  313. }
  314. ProgressBarVideoPlay.Value= _frameIndex;
  315. TextFrameIndex.Text= _frameIndex.ToString();
  316. //显示当前帧
  317. Task.Run(() =>
  318. {
  319. Bitmap bitmap;
  320. lock(_videoReaderLocker)
  321. {
  322. bitmap = _videoReader.ReadVideoFrame(_frameIndex);
  323. }
  324. ShowNewFrame(bitmap);
  325. bitmap.Dispose();
  326. });
  327. }
  328. private void StopVideoPlayAndCapture()
  329. {
  330. _videoPlayEvent.Set();
  331. lock(_videoReaderLocker)
  332. {
  333. if(_videoReader != null)
  334. {
  335. _videoReader.Close();
  336. _videoReader.Dispose();
  337. _videoReader = null;
  338. }
  339. }
  340. if(_videoCapture != null)
  341. {
  342. _videoCapture.NewFrame -= OnVideoCaptureNewFrame;
  343. }
  344. }
  345. private void OnVideoCaptureNewFrame(object sender, NewFrameEventArgs e)
  346. {
  347. var bitmap = e.Frame;
  348. ShowNewFrame(bitmap);
  349. bitmap.Dispose();
  350. //更新显示帧率
  351. _displayCount++;
  352. UpdateFps();
  353. }
  354. #endregion
  355. #region Update
  356. private void UpdateFps()
  357. {
  358. var currentTickCount = Environment.TickCount;
  359. var fpsTimeUsed = currentTickCount - _fpsStartTickCount;
  360. if( fpsTimeUsed > 1000 )
  361. {
  362. var fps = (int)((double)_displayCount / fpsTimeUsed * 1000);
  363. Dispatcher.Invoke(new Action(() =>
  364. {
  365. TextRealProcessFPS.Text = fps + "fps";
  366. }));
  367. _fpsStartTickCount = currentTickCount;
  368. _displayCount = 0;
  369. }
  370. }
  371. private void UpdateTestTime(int time)
  372. {
  373. Dispatcher.Invoke(() => { TextTestTime.Text = time + "ms"; });
  374. }
  375. #endregion
  376. #region checkbox
  377. private void OnCheckEnableAutoEF(object sender, RoutedEventArgs e)
  378. {
  379. _enableAutoEF = true;
  380. ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
  381. }
  382. private void OnUnCheckEnableAutoEF(object sender, RoutedEventArgs e)
  383. {
  384. _enableAutoEF = false;
  385. ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
  386. }
  387. private void OnShowObjectContour(object sender, RoutedEventArgs e)
  388. {
  389. _enableShowObjectContour = true;
  390. ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
  391. }
  392. private void OnUnShowObjectContour(object sender, RoutedEventArgs e)
  393. {
  394. _enableShowObjectContour = false;
  395. ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
  396. }
  397. private void OnShowMeasureLine(object sender, RoutedEventArgs e)
  398. {
  399. _showMeasureLine = true;
  400. ResultShowGrid.SetShowMeasureLine(_showMeasureLine);
  401. }
  402. private void OnUnShowMeasureLine(Object sender, RoutedEventArgs e)
  403. {
  404. _showMeasureLine= false;
  405. ResultShowGrid.SetShowMeasureLine(_showMeasureLine);
  406. }
  407. private void OnBtnSetTestInterval(object sender, RoutedEventArgs e)
  408. {
  409. var testInterval = Convert.ToDouble(TextTestInterval.Text);
  410. if(testInterval != _testIntervalTime)
  411. {
  412. _testIntervalTime = testInterval;
  413. _calculationId = _autoEfCalculation.StartCalculation(_totalDurationTime, _cmPerPixel, testInterval);
  414. _pushImageTime = 0;
  415. }
  416. }
  417. private void OnShowMeasureCurve(object sender, RoutedEventArgs e)
  418. {
  419. _showMeasureCurve = true;
  420. ResultShowGrid.SetShowMeasureCurve(_showMeasureCurve);
  421. }
  422. private void OnUnShowMeasureCurve(object sender, RoutedEventArgs e)
  423. {
  424. _showMeasureCurve= false;
  425. ResultShowGrid.SetShowMeasureCurve(_showMeasureCurve);
  426. }
  427. private void OnUnCheckShowTimeSeries(object sender, RoutedEventArgs e)
  428. {
  429. _showTimeSeriesResult = false;
  430. ResultShowGrid.SetShowTimeSeriesResult(_showTimeSeriesResult);
  431. }
  432. private void OnCheckShowTimeSeries(object sender, RoutedEventArgs e)
  433. {
  434. _showTimeSeriesResult = true;
  435. ResultShowGrid.SetShowTimeSeriesResult(_showTimeSeriesResult);
  436. }
  437. private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
  438. {
  439. StopVideoPlayAndCapture();
  440. if(_autoEfCalculation != null)
  441. {
  442. _autoEfCalculation.NotifyError -= OnErrorOccur;
  443. _autoEfCalculation.Dispose();
  444. _autoEfCalculation = null;
  445. }
  446. }
  447. private void OnErrorOccur(object sender, ErrorEventArgs e)
  448. {
  449. MessageBox.Show("接收到task发出的错误:" + e.GetException().Message);
  450. }
  451. #endregion
  452. }
  453. }