123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- using AI.Common.Interface;
- using AICustomizeSDKDemo.Infrastucture;
- using AICustomizeSDKDemo.Presentation;
- using AICustomizeSDKDemo.Service;
- using AICustomizeSDKDemo.Settings;
- using CustomizeDiagnosisSDK;
- using CustomizeDiagnosisSDK.Interfaces;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Threading;
- using Path = System.IO.Path;
- using Point = System.Windows.Point;
- namespace AICustomizeSDKDemo
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- private CustomizeDiagnosisSDK.CustomizeDiagnosis _customizeDiagnosis;
- private string _calcultaionId;
- private bool _isPlaying;
- private Bitmap _image;
- private List<Organ> _organs;
- private MyTransform _transform;
- private IPlayer _player;
- private IImageProvider _customizeDiagnosisImageProvider;
- private IDetectedObject[] _currentDetectedObject;
- private Stopwatch _stopwatch;
- private int _startTickCount;
- private SettingWindow _settingWindow;
- public MainWindow()
- {
- InitializeComponent();
- try
- {
- _stopwatch = new Stopwatch();
- Logger.Info("The Customer Diagnosis System SDK Demo Program started.");
- var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CustomizeDiagnosisSystem");
- MaxButton.Visibility = Visibility.Collapsed;
- RestoreButton.Visibility = Visibility.Visible;
- OnValueChanged(null, new List<SettingProperty>
- {
- SettingProperty.IsEnableAI,
- });
- InitializePlayer();
- OnPlayerChanged(null, EventArgs.Empty);
- PlayerManager.Instance.PlayerChanged += OnPlayerChanged;
- SettingConfig.Instance.ValueChangedEvent += OnValueChanged;
- }
- catch (Exception ex)
- {
- Logger.Error($"MainWindow :{ex}");
- }
- }
- private void RemoveOldCache()
- {
- RemoveOrgans();
- _organs = null;
- _currentDetectedObject = null;
- }
- private void InitializePlayer()
- {
- if (_player != null)
- {
- _player.Stop();
- _player.InputFrameReceived -= OnInputFrameReceived;
- _player.FPSChanged -= OnFPSChanged;
- }
- _player = PlayerManager.Instance.Player;
- if (_player != null)
- {
- _player.InputFrameReceived += OnInputFrameReceived;
- _player.FPSChanged += OnFPSChanged;
- _player.Play();
- _isPlaying = true;
- Dispatcher.Invoke(() =>
- {
- PlayButton.IsEnabled = SettingConfig.Instance.IsVideo;
- if (PlayButton.Visibility == Visibility.Visible)
- {
- PlayButton.Visibility = Visibility.Collapsed;
- }
- });
- }
- }
- private void OnPlayerChanged(object sender, EventArgs e)
- {
- if (_player != null)
- {
- _player.InputFrameReceived -= OnInputFrameReceived;
- _player.FPSChanged -= OnFPSChanged;
- _player.Stop();
- }
- _player = PlayerManager.Instance.Player;
- Logger.Info($"Player Changed:{SettingConfig.Instance.UltrasoundImageSource}");
- if (_player != null)
- {
- _player.InputFrameReceived += OnInputFrameReceived;
- _player.FPSChanged += OnFPSChanged;
- _player.Play();
- Dispatcher.Invoke(() =>
- {
- VideoProgress.Maximum = _player.TotalFrameCount - 1;
- });
- _isPlaying = true;
- Dispatcher.Invoke(() =>
- {
- PlayButton.IsEnabled = SettingConfig.Instance.IsVideo;
- if (PlayButton.Visibility == Visibility.Visible)
- {
- PlayButton.Visibility = Visibility.Collapsed;
- }
- });
- if (SettingConfig.Instance.IsEnableAI)
- {
- if (SettingConfig.Instance.IsVideo == false)//当源文件为图片的情况下,就重新Show一次给AI提供图片信息。
- {
- DetectImage();
- }
- }
- }
- }
- private void DetectImage()
- {
- if (_image != null)
- {
- if (_customizeDiagnosis != null)
- {
- using (var rawImage = BitmapHelper.BitmapToRawImage(_image, true))
- {
- var imageDataId = new ImageDataId
- {
- DataName = Guid.NewGuid().ToString(),
- CaseId = Guid.NewGuid().ToString(),
- IsVideo = true,
- FrameTimeStamp = 0,
- };
- var input = new ImageInputData(rawImage, imageDataId);
- if (SettingConfig.Instance.IsAsync)
- {
- OnStartEvaluationNotification(this, EventArgs.Empty);
- _customizeDiagnosis.DetectOneImageAsync(input);
- }
- else
- {
- OnStartEvaluationNotification(this, EventArgs.Empty);
- var result = _customizeDiagnosis.DetectOneImage(input);
- OnAIFinishEvaluationNotification(this, result);
- }
- }
- }
- }
- }
- private void StartDiagnosis()
- {
- try
- {
- switch (SettingConfig.Instance.AIDiagnosisType)
- {
- case AIDiagnosisType.CustomizeDiagnosis:
- if (_customizeDiagnosis == null)
- {
- if (_customizeDiagnosisImageProvider == null)
- {
- _customizeDiagnosisImageProvider = new ImageProvider();
- }
- _customizeDiagnosis = new CustomizeDiagnosis();
- Logger.Info("Initialize Diagnosis");
- }
- if (_customizeDiagnosis != null)
- {
- _customizeDiagnosis.Initialize(null, _customizeDiagnosisImageProvider, SettingConfig.Instance.DetectMode, SettingConfig.Instance.DetectTps, SettingConfig.Instance.IntervalTime);
- _customizeDiagnosis.StartEvaluation += OnStartEvaluationNotification;
- _customizeDiagnosis.FinishEvaluation += OnAIFinishEvaluationNotification;
- _customizeDiagnosis.Start();
- Logger.Info("Start Diagnosis");
- }
- break;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"Start Diagnosis Error:{ex}");
- }
- }
- private void StopDiagnosis()
- {
- if (_customizeDiagnosis != null)
- {
- _customizeDiagnosis.StartEvaluation -= OnStartEvaluationNotification;
- _customizeDiagnosis.FinishEvaluation -= OnAIFinishEvaluationNotification;
- _customizeDiagnosis.Stop();
- _customizeDiagnosis = null;
- Logger.Info("Stop Diagnosis");
- }
- }
- private void CloseDiagnosis()
- {
- if (_customizeDiagnosis != null)
- {
- _customizeDiagnosis.StartEvaluation -= OnStartEvaluationNotification;
- _customizeDiagnosis.FinishEvaluation -= OnAIFinishEvaluationNotification;
- _customizeDiagnosis.Close();
- _customizeDiagnosis = null;
- Logger.Info("Close Diagnosis");
- }
- }
- private void OnStartEvaluationNotification(object sender, EventArgs e)
- {
- _stopwatch.Restart();
- Logger.Info($"Start AI Detection");
- Dispatcher.Invoke(() =>
- {
- ErrorInfoText.Text = null;
- });
- }
- #region OnAIFinishEvaluationNotification
- private void OnAIFinishEvaluationNotification(object sender, IDetectedObject[] diagResult)
- {
- try
- {
- if (diagResult != null && diagResult.Length > 0)
- {
- Logger.Info("Finish AI Detection");
- _stopwatch.Stop();
- var elapsedTime = _stopwatch.ElapsedMilliseconds;
- var dateTimeNow = DateTime.Now;
- // 整理诊断结果中相关信息
- List<Organ> organs = new List<Organ>();
- UpdateTransforms();
- Logger.Info("Update Transforms");
- if (diagResult == null)
- {
- return;
- }
- foreach (var result in diagResult)
- {
- organs.Add(new Organ(result.Label.Name, (AI.Common.Interface.Rect)result.BoundingBox));
- }
- // 删掉旧的Roi和Organ
- RemoveOldCache();
- // 设置
- _organs = organs;
- // 更新画布上显示的Organ
- UpdateOrgans();
- Logger.Info("Update Organs");
- var score = diagResult.FirstOrDefault().Score;
- Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
- {
- ResultInfoText.Text = $"Score:{score},时间:{dateTimeNow:yyyyMMddHHmmss}_{dateTimeNow.Millisecond:d3}";
- ElapsedTimeText.Text = $"AI耗时: {elapsedTime}ms";
- }));
- }
- else
- {
- Logger.Info("diag Result is null!");
- }
- }
- catch
- {
- }
- }
- #endregion OnAIFinishEvaluationNotification
- private void OnInputFrameReceived(object sender, Bitmap e)
- {
- _image = e.Clone(new System.Drawing.Rectangle(0, 0, e.Width, e.Height), e.PixelFormat);
- Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
- {
- var bitmapimage = BitmapHelper.BitmapToBitmapImage(e);
- UsImage.Source = bitmapimage;
- }));
- var player = sender as IPlayer;
- if (player != null)
- {
- Dispatcher.Invoke(() =>
- {
- VideoProgress.Value = player.FrameIndex;
- });
- }
- }
- private void OnFPSChanged(object sender, int fps)
- {
- Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
- {
- if (fps > 0)
- {
- FPSText.Text = $"实际帧率:{fps} fps";
- }
- else
- {
- FPSText.Text = "";
- }
- }));
- }
- private void OnValueChanged(object sender, List<SettingProperty> e)
- {
- //当AI功能变更
- if (e.Contains(SettingProperty.IsEnableAI) || e.Contains(SettingProperty.AIDiagnosisType))
- {
- if (e.Contains(SettingProperty.AIDiagnosisType))
- {
- _player?.Stop();
- }
- Logger.Info($"Enable AI:\"{SettingConfig.Instance.IsEnableAI}\",Detect Mode:\"{SettingConfig.Instance.DetectMode}\",Detect Tps:\"{SettingConfig.Instance.DetectTps}\",Interval Time:\"{SettingConfig.Instance.IntervalTime}\"");
- CloseDiagnosis();
- CleanAIResult();
- if (e.Contains(SettingProperty.AIDiagnosisType))
- {
- _player?.Play();
- }
- if (SettingConfig.Instance.IsEnableAI)
- {
- StartDiagnosis();
- if (SettingConfig.Instance.IsVideo == false)//当源文件为图片的情况下,就重新Show一次给AI提供图片信息。
- {
- DetectImage();
- }
- }
- }
- else
- {
- if (SettingConfig.Instance.IsEnableAI)
- {
- if (e.Contains(SettingProperty.DetectTps))
- {
- Logger.Info($"Detect Tps:\"{SettingConfig.Instance.DetectTps}\"");
- if (_customizeDiagnosis != null)
- {
- _customizeDiagnosis.DetectTps = SettingConfig.Instance.DetectTps;
- }
- }
- if (e.Contains(SettingProperty.IntervalTime))
- {
- Logger.Info($"Interval Time:\"{SettingConfig.Instance.IntervalTime}\"");
- if (_customizeDiagnosis != null)
- {
- _customizeDiagnosis.IntervalTime = SettingConfig.Instance.IntervalTime;
- }
- }
- if (e.Contains(SettingProperty.DetectMode))
- {
- Logger.Info($"Detect Mode:\"{SettingConfig.Instance.DetectMode}\"");
- if (_customizeDiagnosis != null)
- {
- _customizeDiagnosis.DetectMode = SettingConfig.Instance.DetectMode;
- }
- }
- }
- }
- if (e.Contains(SettingProperty.IsAsync) && !SettingConfig.Instance.IsVideo)
- {
- if (SettingConfig.Instance.IsEnableAI)
- {
- StartDiagnosis();
- if (SettingConfig.Instance.IsVideo == false)//当源文件为图片的情况下,就重新Show一次给AI提供图片信息。
- {
- DetectImage();
- }
- }
- }
- }
- private void CleanAIResult()
- {
- RemoveOldCache();
- Dispatcher.Invoke(() =>
- {
- ResultInfoText.Text = "";
- ElapsedTimeText.Text = "";
- });
- }
- private void UpdateTransforms()
- {
- if (_image != null)
- {
- // 使图片保持长宽比例,居中显示,需要平移和缩放
- var imgWidth = _image.Width;
- var imgHeight = _image.Height;
- var canvasWidth = UsCanvas.ActualWidth;
- var canvasHeight = UsCanvas.ActualHeight;
- var scaleX = canvasWidth / imgWidth;
- var scaleY = canvasHeight / imgHeight;
- var scale = scaleX < scaleY ? scaleX : scaleY;
- double offsetX, offsetY;
- if (Math.Abs(scale - scaleX) < 0.0001)
- {
- offsetY = 0.5 * (scaleY - scale) * imgHeight;
- offsetX = 0;
- }
- else
- {
- offsetX = 0.5 * (scaleX - scale) * imgWidth;
- offsetY = 0;
- }
- _transform = new MyTransform(scale, offsetX, offsetY);
- }
- }
- private void UpdateOrgans()
- {
- Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
- {
- if (_organs != null)
- {
- for (int ni = 0; ni < _organs.Count; ni++)
- {
- DrawOrganInCanvas(_organs[ni]);
- }
- }
- }));
- }
- private void RemoveOrgans()
- {
- Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
- {
- // 移除所有旧的Organ
- if (_organs != null)
- {
- for (int ni = 0; ni < _organs.Count; ni++)
- {
- RemoveOrganInCanvas(_organs[ni]);
- }
- }
- }));
- }
- private void DrawOrganInCanvas(Organ organ)
- {
- if (_transform == null)
- {
- return;
- }
- // 已有则删除
- RemoveOrganInCanvas(organ);
- if (organ.BoundBox == null)
- {
- return;
- }
- // 有轮廓就显示轮廓,没有轮廓就显示方框
- string organName = organ.OrganName;
- var rect = new System.Windows.Rect(organ.BoundBox.Left, organ.BoundBox.Top, organ.BoundBox.Width, organ.BoundBox.Height);
- var transRect = _transform.Transform(rect);
- System.Windows.Shapes.Rectangle rectDraw = new System.Windows.Shapes.Rectangle
- {
- StrokeThickness = 3,
- Stroke = organ.Color,
- Width = transRect.Width,
- Height = transRect.Height
- };
- Canvas.SetLeft(rectDraw, transRect.Left);
- Canvas.SetTop(rectDraw, transRect.Top);
- UsCanvas.Children.Add(rectDraw);
- string rectName = "Organ_Rect_" + organName;
- UsCanvas.RegisterName(rectName, rectDraw);
- // 将Organ的序号写在左上角
- var pointLT = new Point(organ.BoundBox.Left, organ.BoundBox.Top);
- var transPointLT = _transform.Transform(pointLT);
- var textLeft = transPointLT.X + 16;
- var textTop = transPointLT.Y;
- TextBlock text = new TextBlock
- {
- Text = organName,
- FontSize = 16,
- FontWeight = FontWeights.Bold,
- Foreground = organ.Color,
- VerticalAlignment = VerticalAlignment.Top
- };
- Canvas.SetLeft(text, textLeft);
- Canvas.SetTop(text, textTop);
- UsCanvas.Children.Add(text);
- string textName = "Organ_Text_" + organName;
- UsCanvas.RegisterName(textName, text);
- }
- private void RemoveOrganInCanvas(Organ organ)
- {
- string organName = organ.OrganName;
- // 框
- string rectName = "Organ_Rect_" + organName;
- RemoveElement(rectName);
- // 文字
- string textName = "Organ_Text_" + organName;
- RemoveElement(textName);
- }
- private void Window_StateChanged(object sender, EventArgs e)
- {
- if (WindowState == WindowState.Maximized)
- {
- MaxButton.Visibility = Visibility.Collapsed;
- RestoreButton.Visibility = Visibility.Visible;
- }
- else
- {
- MaxButton.Visibility = Visibility.Visible;
- RestoreButton.Visibility = Visibility.Collapsed;
- }
- }
- private void OnOpenSettingWindow(object sender, RoutedEventArgs e)
- {
- _settingWindow = new SettingWindow();
- _settingWindow.ShowDialog();
- }
- private void OnMinClick(object sender, RoutedEventArgs e)
- {
- WindowState = WindowState.Minimized;
- }
- private void OnMaxClick(object sender, RoutedEventArgs e)
- {
- WindowState = WindowState.Maximized;
- }
- private void OnRestoreClick(object sender, RoutedEventArgs e)
- {
- WindowState = WindowState.Normal;
- }
- private void OnCloseClick(object sender, RoutedEventArgs e)
- {
- if (_player != null)
- {
- _player.Stop();
- _player.InputFrameReceived -= OnInputFrameReceived;
- _player.FPSChanged -= OnFPSChanged;
- }
- CloseDiagnosis();
- Close();
- }
- private void Grid_MouseMove(object sender, MouseEventArgs e)
- {
- if (SettingConfig.Instance.IsVideo)
- {
- if (_isPlaying)
- {
- PauseButton.Visibility = Visibility.Visible;
- }
- }
- }
- private void CanvasSizeChanged(object sender, SizeChangedEventArgs e)
- {
- UpdateTransforms();
- RemoveOrgans();
- UpdateOrgans();
- }
- private void OnPlayClick(object sender, RoutedEventArgs e)
- {
- if (_player != null)
- {
- _player.Continue();
- _isPlaying = true;
- PlayButton.Visibility = Visibility.Collapsed;
- PauseButton.Visibility = Visibility.Visible;
- }
- }
- private void OnPauseClick(object sender, RoutedEventArgs e)
- {
- if (_player != null)
- {
- _player.Pause();
- _isPlaying = false;
- PlayButton.Visibility = Visibility.Visible;
- PauseButton.Visibility = Visibility.Collapsed;
- System.Threading.Tasks.Task.Run(() =>
- {
- Thread.Sleep(1000);
- DetectImage();
- });
- }
- }
- private void RemoveElement(string name)
- {
- var uiElement = UsCanvas.FindName(name) as UIElement;
- if (uiElement != null)
- {
- UsCanvas.Children.Remove(uiElement);
- UsCanvas.UnregisterName(name);
- }
- }
- private void Grid_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
- {
- PauseButton.Visibility = Visibility.Collapsed;
- }
- }
- }
|