123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- using System;
- using System.Windows;
- using System.Windows.Threading;
- using System.Windows.Media.Imaging;
- using System.Collections.Generic;
- using System.Collections.Concurrent;
- using System.Threading.Tasks;
- using System.Drawing;
- using System.Drawing.Imaging;
- using Microsoft.Win32;
- using System.Threading;
- using System.Diagnostics;
- using Accord.Video.FFMPEG;
- using System.IO;
- using System.Runtime.InteropServices;
- using YOLODetectProcessLib;
- using Accord.Video.DirectShow;
- using Accord.Video;
- using System.Windows.Controls;
- namespace YOLODetectDemo
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- private IInferenceNetwork _inferNet = null;
- private BitmapImage _origImg = null;
- private BitmapImage _dstImg = null;
- private int _doneImageNum = 0;
- private volatile bool _isCropped = false;
- private Stopwatch _stopWatch = new Stopwatch();
- //private BreastLesionDetectImg _doBreastLesionDetect;
- private LiverLesionDetectImg _doLiverLesionDetect;
- int coreCount = Environment.ProcessorCount * 2;
- int numCPU;
- private volatile bool _enableAI = true;
- private volatile bool _continuouslyDetecting = false;
- List<DetectedBreastLesionResult> inferResult = new List<DetectedBreastLesionResult>();
- int oriWidth = 0;
- int oriHeight = 0;
- private readonly object _videoReaderLocker = new object();
- private VideoFileReader _videoReader;
- private volatile int _displayCount = 0;
- private ManualResetEvent _playEvent = new ManualResetEvent(false);
- private VideoCaptureDevice _videoCapture;
- public MainWindow()
- {
- InitializeComponent();
- try
- {
- numCPU = Convert.ToInt32(TextBoxCPU.Text);
- if (numCPU <= 0 || numCPU > coreCount)
- throw new Exception("CPU数目设置错误");
- }
- catch (Exception excep)
- {
- MessageBox.Show("CPU数目设置错误,只能为正整数且小于本机CPU线程总数!");
- return;
- }
- try
- {
- string netDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
- //_doBreastLesionDetect = new BreastLesionDetectImg(numCPU, netDir);
- _doLiverLesionDetect = new LiverLesionDetectImg(numCPU, netDir);
- }
- catch (Exception excep)
- {
- MessageBox.Show("doMyocardialSegment失败!");
- return;
- }
- }
- private void OnBtnTestClicked(object sender, RoutedEventArgs e)
- {
- StopPlay();
- try
- {
- OpenFileDialog openFileDialog = new OpenFileDialog
- {
- Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg;*.avi;*.mp4",
- Multiselect = false,
- Title = "选择一幅或多幅图",
- };
- if (openFileDialog.ShowDialog() == true)
- {
- var fileName = openFileDialog.FileName;
- var fileExtension = Path.GetExtension(fileName);
- if (fileExtension == ".png" || fileExtension == ".jpg" || fileExtension == ".bmp" || fileExtension == ".jpeg")
- {
- var bitmap = new Bitmap(fileName);
- if (_enableAI)
- {
- DoImgTest(bitmap);
- }
- MyImageCanvas.UpdateImage(bitmap);
- bitmap.Dispose();
- }
- else
- {
- long frameCount;
- lock (_videoReaderLocker)
- {
- _videoReader = new VideoFileReader();
- _videoReader.Open(fileName);
- frameCount = _videoReader.FrameCount;
- }
- _displayCount = 0;
- _playEvent.Reset();
- Task.Run(() =>
- {
- _continuouslyDetecting = true;
- bool breakFlag = false;
- while (!_playEvent.WaitOne(1) && !breakFlag)
- {
- for (var ni = 0; ni < frameCount; ni++)
- {
- if (_playEvent.WaitOne(1))
- {
- breakFlag = true;
- break;
- }
- Bitmap bitmap;
- lock (_videoReaderLocker)
- {
- bitmap = _videoReader.ReadVideoFrame(ni);
- }
- _displayCount++;
- if (_enableAI)
- {
- DoImgTest(bitmap);
- }
- // 更新显示图像
- MyImageCanvas.UpdateImage(bitmap);
- bitmap.Dispose();
- if (_playEvent.WaitOne(0))
- {
- breakFlag = true;
- break;
- }
- }
- }
- _continuouslyDetecting = false;
- });
- VideoFileReader videoReader = new VideoFileReader();
- videoReader.Open(fileName);
- int totalCount = (int)videoReader.FrameCount;
- videoReader.Close();
- for (int ni = 0; ni < totalCount; ni++)
- {
- videoReader.Open(fileName);
- videoReader.Close();
- }
- videoReader.Dispose();
- }
- }
- }
- catch (Exception excep)
- {
- MessageBox.Show("加载测试图片时出错:" + excep);
- }
- }
- private void DoImgTest(Bitmap bmp)
- {
- try
- {
- RawImage rawImg = BitmapToRawImage(bmp);
- _stopWatch.Restart();
- //inferResult = _doBreastLesionDetect.ProcessBreastLesionImg(rawImg, _isCropped);
- inferResult = _doLiverLesionDetect.ProcessLiverLesionImg(rawImg, _isCropped);
- long inferTime = _stopWatch.ElapsedMilliseconds;
- _stopWatch.Stop();
- Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
- {
- ForwardTime.Text = inferTime.ToString();
- }));
- using (var g = Graphics.FromImage(bmp))
- {
- Brush brush = Brushes.Red;
- if (inferResult.Count > 0)
- {
- int len = inferResult.Count;
- for (int nj = 0; nj < len; nj++)
- {
- string labels = "";
- //if (inferResult[nj].Label == 1)
- //{
- // labels = "脂肪瘤";
- //}
- //if (inferResult[nj].Label == 2)
- //{
- // labels = "2类";
- //}
- //if (inferResult[nj].Label == 3)
- //{
- // labels = "3类";
- //}
- //if (inferResult[nj].Label == 4)
- //{
- // labels = "4a类";
- //}
- //if (inferResult[nj].Label == 5)
- //{
- // labels = "4b类";
- //}
- //if (inferResult[nj].Label == 6)
- //{
- // labels = "4c类";
- //}
- //if (inferResult[nj].Label == 7)
- //{
- // labels = "5类";
- //}
- if (inferResult[nj].Label == 1)
- {
- labels = "肝内强回声灶";
- }
- if (inferResult[nj].Label == 2)
- {
- labels = "肝血管瘤";
- }
- if (inferResult[nj].Label == 3)
- {
- labels = "肝囊肿";
- }
- if (inferResult[nj].Label == 4)
- {
- labels = "肝癌可能";
- }
- g.DrawString(labels + ":" + inferResult[nj].Confidence.ToString("0.0" + "%"), new Font("Arial", 10),
- brush, inferResult[nj].BoundingBox.Left + 10, inferResult[nj].BoundingBox.Top + 10);
- System.Drawing.Rectangle rect2 = new Rectangle(inferResult[nj].BoundingBox.Left,
- inferResult[nj].BoundingBox.Top, inferResult[nj].BoundingBox.Width,
- inferResult[nj].BoundingBox.Height);
- g.DrawRectangle(new Pen(brush, 2), rect2);
- }
- }
- }
- _doneImageNum += 1;
- }
- catch (Exception excep)
- {
- MessageBox.Show("测试图像时出错:" + excep);
- }
- }
- private void OnUncheckedEnableAI(object sender, RoutedEventArgs e)
- {
- _enableAI = false;
- MyImageCanvas.SetShowParams(_enableAI);
- }
- private void OnCheckedEnableAI(object sender, RoutedEventArgs e)
- {
- _enableAI = true;
- MyImageCanvas.SetShowParams(_enableAI);
- if (!_continuouslyDetecting && MyImageCanvas.Image != null)
- {
- DoImgTest(MyImageCanvas.Image);
- }
- }
- public static void SaveBitmapImageIntoFile(BitmapImage bitmapImage, string filePath)
- {
- BitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
- using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
- {
- encoder.Save(fileStream);
- }
- }
- public static RawImage BitmapToRawImage(Bitmap image, bool keepChannel = false)
- {
- int width = image.Width;
- int height = image.Height;
- PixelFormat pixelFormat = image.PixelFormat;
- if (pixelFormat != PixelFormat.Format24bppRgb && pixelFormat != PixelFormat.Format32bppArgb &&
- pixelFormat != PixelFormat.Format32bppPArgb && pixelFormat != PixelFormat.Format32bppRgb &&
- pixelFormat != PixelFormat.Format8bppIndexed)
- {
- throw new Exception("Unexpected image pixel format:" + pixelFormat);
- }
- int origChannel;
- if (pixelFormat == PixelFormat.Format24bppRgb)
- {
- origChannel = 3;
- }
- else if (pixelFormat == PixelFormat.Format8bppIndexed)
- {
- origChannel = 1;
- }
- else
- {
- origChannel = 4;
- }
- int dstChannel = 3;
- if (keepChannel)
- {
- dstChannel = origChannel;
- }
- var bmData = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat);
- byte[] dstDataArray = new byte[width * height * dstChannel];
- {
- int stride = bmData.Stride;
- int dstStride = width * dstChannel;
- IntPtr ptr = bmData.Scan0;
- IntPtr ptrH, ptrW;
- for (int nh = 0; nh < height; nh++)
- {
- ptrH = IntPtr.Add(ptr, nh * stride);
- if (origChannel == dstChannel)
- {
- Marshal.Copy(ptrH, dstDataArray, nh * dstStride, dstStride);
- }
- else if (origChannel > dstChannel)
- {
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origChannel);
- Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel, dstChannel);
- }
- }
- else
- {
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origChannel);
- for (int nc = 0; nc < dstChannel; nc++)
- {
- Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel + nc, 1);
- }
- }
- }
- }
- }
- image.UnlockBits(bmData);
- return new RawImage(dstDataArray, width, height, dstChannel, width * dstChannel);
- }
- public static Bitmap RawImageToBitmap(RawImage image)
- {
- int width = image.Width;
- int height = image.Height;
- int channel = image.Channel;
- PixelFormat pixelFormat;
- switch (channel)
- {
- case 1:
- pixelFormat = PixelFormat.Format8bppIndexed;
- break;
- case 3:
- pixelFormat = PixelFormat.Format24bppRgb;
- break;
- case 4:
- pixelFormat = PixelFormat.Format32bppArgb;
- break;
- default:
- throw new ArgumentOutOfRangeException("The expected image.channel is 1,3,4 but got:" + channel + ".");
- }
- Bitmap dstBmp = new Bitmap(width, height, pixelFormat);
- var bmData = dstBmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, pixelFormat);
- {
- int stride = bmData.Stride;
- IntPtr ptr = bmData.Scan0;
- byte[] dataBuffer = image.DataBuffer;
- if (stride == width * channel)
- {
- Marshal.Copy(dataBuffer, 0, ptr, width * height * channel);
- }
- else
- {
- IntPtr ptrDst;
- for (int ni = 0; ni < height; ni++)
- {
- ptrDst = IntPtr.Add(ptr, ni * stride);
- Marshal.Copy(dataBuffer, ni * width * channel, ptrDst, width * channel);
- }
- }
- }
- dstBmp.UnlockBits(bmData);
- return dstBmp;
- }
- private BitmapImage BitmapToBitmapImage(Bitmap img)
- {
- BitmapImage bmpimg = new BitmapImage();
- using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
- {
- img.Save(ms, ImageFormat.Png);
- bmpimg.BeginInit();
- bmpimg.StreamSource = ms;
- bmpimg.CacheOption = BitmapCacheOption.OnLoad;
- bmpimg.EndInit();
- bmpimg.Freeze();
- ms.Dispose();
- }
- return bmpimg;
- }
- private void StopPlay()
- {
- _playEvent.Set();
- lock (_videoReaderLocker)
- {
- if (_videoReader != null)
- {
- _videoReader.Close();
- _videoReader.Dispose();
- _videoReader = null;
- }
- }
- if (_videoCapture != null)
- {
- _videoCapture.NewFrame -= OnVideoCaptureNewFrame;
- _videoCapture.SignalToStop();
- _videoCapture = null;
- }
- _continuouslyDetecting = false;
- }
- private void OnVideoCaptureNewFrame(object sender, NewFrameEventArgs e)
- {
- var bitmap = e.Frame;
- // 更新显示图像
- MyImageCanvas.UpdateImage(bitmap);
- _displayCount++;
- if (_enableAI)
- {
- DoImgTest(bitmap);
- }
- MyImageCanvas.UpdateImage(bitmap);
- bitmap.Dispose();
- }
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- _inferNet?.Dispose();
- }
- private void OnCheckedIsCropped(object sender, RoutedEventArgs e)
- {
- _isCropped = true;
- }
- private void OnUncheckedIsCropped(object sender, RoutedEventArgs e)
- {
- _isCropped = false;
- }
- }
- }
|