123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Threading;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using Microsoft.Win32;
- using System.IO;
- using AutoEFInferenceCalcLib;
- using System.Threading;
- using Accord.Video.FFMPEG;
- using Accord.Video.DirectShow;
- using Accord.Video;
- using AI.Common;
- using System.Drawing;
- using ImageShowUtilsLib;
- namespace AutoEFCalculationDemo
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- #region field
- private bool _enableAutoEF = true;
- private bool _enableShowObjectContour = true;
- private bool _showMeasureLine = true;
- private bool _showMeasureCurve = true;
- private bool _showTimeSeriesResult = true;
- private int _numCpu;
- private AutoEFCalculation _autoEfCalculation = null;
- private ManualResetEvent _videoPlayEvent = new ManualResetEvent(false);
- private Thread _videoPlayThread = null;
- private VideoFileReader _videoReader = null;
- private readonly object _videoReaderLocker = new object();
- private VideoCaptureDevice _videoCapture;
- private volatile bool _readingVideo = false;
- private volatile bool _pausing = false;
-
- private string _calculationId;
- private double _totalDurationTime = 10000.0;
- private float _cmPerPixel = 0.026f;
- private double _testIntervalTime = 0.0;
- private double _pushImageTime = 0.0;
- private volatile int _frameCount = 0;
- private volatile int _frameIndex = 0;
- private volatile int _frameIntervalTime = 0;
- private volatile int _displayCount = 0;
- private volatile int _lastDetectTickCount;
- private volatile int _fpsStartTickCount;
- private volatile bool _continuouslyDetecting = false;
- #endregion
- public MainWindow()
- {
- InitializeComponent();
- TextTestInterval.Text = _testIntervalTime.ToString();
- CheckEnableAI.IsChecked = _enableAutoEF;
- CheckShowObjectContour.IsChecked = _enableShowObjectContour;
- CheckShowMesureLine.IsChecked = _showMeasureLine;
- //CheckShowMeasureCurve.IsChecked = _showMeasureCurve;
- //CheckShowTimeSeriesResult.IsChecked = _showTimeSeriesResult;
- ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
- ResultShowGrid.SetShowMeasureCurve(_showMeasureCurve);
- ResultShowGrid.SetShowMeasureLine(_enableShowObjectContour);
- ResultShowGrid.SetShowTimeSeriesResult(_showTimeSeriesResult);
- try
- {
- _numCpu = Convert.ToInt32(4);
- int coreCount = Environment.ProcessorCount * 2;
- if(_numCpu <= 0 || _numCpu > coreCount)
- {
- throw new Exception("CPU数目设置错误,只能为正整数且小于本机CPU线程总数!");
- }
- string netDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
- _autoEfCalculation = new AutoEFCalculation(_numCpu, netDir);
- }
- catch (Exception ex)
- {
- MessageBox.Show("AutoEFCalculation, 模型加载失败!");
- return;
- }
- }
- private void OnClickStartTest(object sender, RoutedEventArgs e)
- {
- StopVideoPlayAndCapture();
- var ofd = new OpenFileDialog();
- ofd.Filter = "视频| *.mp4; *.avi";
- ofd.Multiselect = false;
- ofd.Title = "选择一个视频";
- if(ofd.ShowDialog() == true)
- {
- var fileName = ofd.FileName;
- var fileExtension = Path.GetExtension(fileName);
- _calculationId = _autoEfCalculation.StartCalculation(_totalDurationTime, _cmPerPixel, _testIntervalTime);
- _pushImageTime = 0;
- if(fileExtension ==".mp4" || fileExtension == ".avi")
- {
- lock(_videoReaderLocker)
- {
- _videoReader = new VideoFileReader();
- _videoReader.Open(fileName);
- var frameRate = _videoReader.FrameRate.ToDouble();
- TextRealVideoFPS.Text = frameRate.ToString("0.00") + "fps";
- _frameIntervalTime = (int)(1000 / frameRate);
- _frameCount = (int)_videoReader.FrameCount;
- _frameIndex = 0;
- }
- _readingVideo = true;
-
- ProgressBarVideoPlay.IsEnabled = true;
- BtnBack.IsEnabled = true;
- BtnForward.IsEnabled = true;
- BtnPauseOrPlay.IsEnabled = true;
- BtnPauseOrPlay.Content = "暂停";
- ProgressBarVideoPlay.Minimum = 0;
- ProgressBarVideoPlay.Maximum = _frameCount;
- _lastDetectTickCount = Environment.TickCount;
- _fpsStartTickCount = _lastDetectTickCount;
- _displayCount = 0;
- _videoPlayEvent.Reset();
- _pausing = false;
- ResultShowGrid.ClearDiagResult();
- if (_videoPlayThread == null || !_videoPlayThread.IsAlive)
- {
- _videoPlayThread = new Thread(() => DoVideoPlay())
- {
- IsBackground = true,
- Name = "AI_AUTOEF_VideoPlay",
- };
- _videoPlayThread.Start();
- }
- }
- }
- }
- private void DoVideoPlay()
- {
- _continuouslyDetecting = true;
- while(!_pausing)
- {
- var currentTickCount = Environment.TickCount;
- Bitmap bitmap;
- lock(_videoReaderLocker)
- {
- bitmap = _videoReader?.ReadVideoFrame(_frameIndex);
- }
- if (bitmap == null)
- {
- break;
- }
- ShowNewFrame(bitmap);
- bitmap.Dispose();
- //更新帧率
- _displayCount++;
- UpdateFps();
- //最后还剩多少时间sleep
- var detectTimeUsed = Environment.TickCount - currentTickCount;
- var finalSleepTime = _frameIntervalTime - detectTimeUsed;
- if(finalSleepTime < 0)
- {
- finalSleepTime = 0;
- }
- if(_videoPlayEvent.WaitOne(finalSleepTime))
- {
- break;
- }
- //如果到尾了,就从头开始
- if(_frameIndex == _frameCount - 1)
- {
- _frameIndex = 0;
- }
- else
- {
- _frameIndex += 1;
- }
- //更新进度条
- Dispatcher.Invoke(() =>
- {
- ProgressBarVideoPlay.Value = _frameIndex;
- TextFrameIndex.Text = _frameIndex.ToString();
- });
- }
- _continuouslyDetecting = false;
- }
- private void ShowNewFrame(Bitmap bitmap, RawImage rawImage = null)
- {
- ResultShowGrid.UpdateImage(_pushImageTime, bitmap);
- if(_enableAutoEF)
- {
- //当满足时间间隔要求,则进行检测
- var currentTickCount = Environment.TickCount;
- if(_autoEfCalculation != null)
- {
- DetectImage(bitmap, rawImage);
- }
- var detectTimeUsed = Environment.TickCount - currentTickCount;
- //更新显示实际耗时
- UpdateTestTime(detectTimeUsed);
- }
- }
- private void DetectImage(Bitmap bitmap, RawImage rawImage = null)
- {
- _lastDetectTickCount = Environment.TickCount;
- RawImage rawImageToEval = null;
- if(rawImage == null)
- {
- rawImageToEval = RawImageShowUtils.BitmapToRawImage(bitmap);
- }
- else
- {
- rawImageToEval = rawImage;
- }
- //视频时间戳
- double currentTimeStamp = _pushImageTime;
- CardiacCurveInfos result = _autoEfCalculation.PushOneImageSync(_calculationId,
- rawImageToEval, currentTimeStamp);
- ResultShowGrid.UpdateDiagResult(_calculationId, currentTimeStamp, result);
- _pushImageTime += 33;//_frameIntervalTime
- }
- #region back forward stop
- private void OnClickStop(object sender, RoutedEventArgs e)
- {
- if(!_readingVideo)
- {
- return;
- }
- if(_continuouslyDetecting)
- {
- //停止自动播放
- _pausing = true;
- _videoPlayEvent.Set();
- BtnPauseOrPlay.Content = "播放";
- }
- else
- {
- //开始自动播放
- _videoPlayEvent.Reset();
- _pausing = false;
- if(_videoPlayThread == null || !_videoPlayThread.IsAlive)
- {
- _videoPlayThread = new Thread(() => DoVideoPlay())
- {
- IsBackground = true,
- Name = "AI_AUTOEF_VideoPlay",
- };
- _videoPlayThread.Start();
- }
- BtnPauseOrPlay.Content = "暂停";
- }
- }
- private void OnClickBackward(object sender, RoutedEventArgs e)
- {
- if(!_readingVideo)
- {
- return ;
- }
- if( _continuouslyDetecting)
- {
- //停止自动播放
- _videoPlayEvent.Set();
- _pausing = true;
- BtnPauseOrPlay.Content = "播放";
- }
- //往后退一帧
- if(_frameIndex == 0)
- {
- _frameIndex = _frameCount - 1;
- }
- else
- {
- _frameIndex -= 1;
- }
- ProgressBarVideoPlay.Value = _frameIndex;
- TextFrameIndex.Text = _frameIndex.ToString();
- //显示当前帧
- Task.Run(() =>
- {
- Bitmap bitmap;
- lock(_videoReaderLocker)
- {
- bitmap = _videoReader.ReadVideoFrame(_frameIndex);
- }
- ShowNewFrame(bitmap);
- bitmap.Dispose();
- });
- }
- private void OnClickForward(object sender, RoutedEventArgs e)
- {
- if(!_readingVideo)
- {
- return;
- }
- if( _continuouslyDetecting)
- {
- //停止自动播放
- _videoPlayEvent.Set();
- _pausing = true;
- BtnPauseOrPlay.Content = "播放";
- }
- //往前一帧
- if(_frameIndex == _frameCount -1)
- {
- _frameIndex = 0;
- }
- else
- {
- _frameIndex += 1;
- }
- ProgressBarVideoPlay.Value= _frameIndex;
- TextFrameIndex.Text= _frameIndex.ToString();
- //显示当前帧
- Task.Run(() =>
- {
- Bitmap bitmap;
- lock(_videoReaderLocker)
- {
- bitmap = _videoReader.ReadVideoFrame(_frameIndex);
- }
- ShowNewFrame(bitmap);
- bitmap.Dispose();
- });
- }
- private void StopVideoPlayAndCapture()
- {
- _videoPlayEvent.Set();
- lock(_videoReaderLocker)
- {
- if(_videoReader != null)
- {
- _videoReader.Close();
- _videoReader.Dispose();
- _videoReader = null;
- }
- }
- if(_videoCapture != null)
- {
- _videoCapture.NewFrame -= OnVideoCaptureNewFrame;
- }
- }
- private void OnVideoCaptureNewFrame(object sender, NewFrameEventArgs e)
- {
- var bitmap = e.Frame;
- ShowNewFrame(bitmap);
- bitmap.Dispose();
- //更新显示帧率
- _displayCount++;
- UpdateFps();
- }
- #endregion
- #region Update
- private void UpdateFps()
- {
- var currentTickCount = Environment.TickCount;
- var fpsTimeUsed = currentTickCount - _fpsStartTickCount;
- if( fpsTimeUsed > 1000 )
- {
- var fps = (int)((double)_displayCount / fpsTimeUsed * 1000);
- Dispatcher.Invoke(new Action(() =>
- {
- TextRealProcessFPS.Text = fps + "fps";
- }));
- _fpsStartTickCount = currentTickCount;
- _displayCount = 0;
- }
- }
- private void UpdateTestTime(int time)
- {
- Dispatcher.Invoke(() => { TextTestTime.Text = time + "ms"; });
- }
- #endregion
- #region checkbox
- private void OnCheckEnableAutoEF(object sender, RoutedEventArgs e)
- {
- _enableAutoEF = true;
- ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
- }
- private void OnUnCheckEnableAutoEF(object sender, RoutedEventArgs e)
- {
- _enableAutoEF = false;
- ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
- }
- private void OnShowObjectContour(object sender, RoutedEventArgs e)
- {
- _enableShowObjectContour = true;
- ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
- }
- private void OnUnShowObjectContour(object sender, RoutedEventArgs e)
- {
- _enableShowObjectContour = false;
- ResultShowGrid.SetImageShowsParam(_enableAutoEF, _enableShowObjectContour);
- }
- private void OnShowMeasureLine(object sender, RoutedEventArgs e)
- {
- _showMeasureLine = true;
- ResultShowGrid.SetShowMeasureLine(_showMeasureLine);
- }
- private void OnUnShowMeasureLine(Object sender, RoutedEventArgs e)
- {
- _showMeasureLine= false;
- ResultShowGrid.SetShowMeasureLine(_showMeasureLine);
- }
- private void OnBtnSetTestInterval(object sender, RoutedEventArgs e)
- {
- var testInterval = Convert.ToDouble(TextTestInterval.Text);
- if(testInterval != _testIntervalTime)
- {
- _testIntervalTime = testInterval;
- _calculationId = _autoEfCalculation.StartCalculation(_totalDurationTime, _cmPerPixel, testInterval);
- _pushImageTime = 0;
- }
- }
- private void OnShowMeasureCurve(object sender, RoutedEventArgs e)
- {
- _showMeasureCurve = true;
- ResultShowGrid.SetShowMeasureCurve(_showMeasureCurve);
- }
- private void OnUnShowMeasureCurve(object sender, RoutedEventArgs e)
- {
- _showMeasureCurve= false;
- ResultShowGrid.SetShowMeasureCurve(_showMeasureCurve);
- }
- private void OnUnCheckShowTimeSeries(object sender, RoutedEventArgs e)
- {
- _showTimeSeriesResult = false;
- ResultShowGrid.SetShowTimeSeriesResult(_showTimeSeriesResult);
- }
- private void OnCheckShowTimeSeries(object sender, RoutedEventArgs e)
- {
- _showTimeSeriesResult = true;
- ResultShowGrid.SetShowTimeSeriesResult(_showTimeSeriesResult);
- }
- private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- StopVideoPlayAndCapture();
- if(_autoEfCalculation != null)
- {
- _autoEfCalculation.NotifyError -= OnErrorOccur;
- _autoEfCalculation.Dispose();
- _autoEfCalculation = null;
- }
- }
- private void OnErrorOccur(object sender, ErrorEventArgs e)
- {
- MessageBox.Show("接收到task发出的错误:" + e.GetException().Message);
- }
- #endregion
- }
- }
|