123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- using System;
- using System.Windows;
- using System.Threading;
- using System.Threading.Tasks;
- using System.IO;
- using System.Collections.Generic;
- using Microsoft.Win32;
- using System.Drawing;
- using Accord.Video;
- using Accord.Video.FFMPEG;
- using Accord.Video.DirectShow;
- using CameraDataCapture;
- using AI.Common;
- using AI.Common.Log;
- using Rect = AI.Common.Rect;
- using ImageShowUtilsLib;
- using System.Windows.Controls;
- using Intel.RealSense;
- using RUSCommon;
- using System.Diagnostics;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- using Size = System.Drawing.Size;
- using Rectangle = System.Drawing.Rectangle;
- using RUSCommon.CameraConfigure;
- namespace CameraImageCaptureTest
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- #region private
- private CameraDataCapture.CameraDataCapture _cameraImageCapture;
- private ManualResetEvent _playEvent = new ManualResetEvent(false);
- private VideoFileReader _videoReader;
- private readonly object _videoReaderLocker = new object();
- private VideoCaptureDevice _videoCapture;
- // 相机图像
- private bool _isConfirmCameraInfo;// 是否已确认相机信息
- private CameraInformation _cameraInfo;
- private ICameraPara _cameraPara;
- private DateTime _cameraImageReceiveStartTime;
- private volatile int _cameraImageReceiveCount = 0;
- #endregion
- #region 用户界面响应
- public MainWindow()
- {
- InitializeComponent();
- try
- {
- RefreshComboBoxVideoCards();
- // 遍历所有的相机、采集卡
- var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
- foreach (var device in videoDevices)
- {
- ComboBoxCameras.Items.Add(device);
- }
- // 相机类型
- ComboBoxCameraType.Items.Add("单目");
- ComboBoxCameraType.Items.Add("双目");
- ComboBoxCameraType.Items.Add("realsense");
- ComboBoxCameraType.Items.Add("知微");
- ComboBoxCameraType.SelectedIndex = -1;
- }
- catch (Exception e)
- {
- MessageBox.Show("初始化过程中出错:" + e);
- }
- }
- /// <summary>
- /// 用户关闭窗口
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- StopPlay();
- // 如果已打开相机,则关闭
- if (_cameraImageCapture != null)
- {
- _cameraImageCapture.StopCameraDataAcquire();
- _cameraImageCapture.NotifyCameraDataUpdate -= OnCameraDatasUpdate;
- _cameraImageCapture.NotifyError -= OnErrorOccur;
- _cameraImageCapture.Dispose();
- _cameraImageCapture = null;
- // 记录一下总共收到多少幅相机图像(主要用来对比一下保存数据输入记录、启用模型监测和不做任何操作时,是否有影响帧率)
- var timeSpan = DateTime.Now - _cameraImageReceiveStartTime;
- LogHelper.InfoLog("TotalReceivedCameraImageCount:" + _cameraImageReceiveCount + ",TimeSpan:" + timeSpan.TotalMilliseconds + "(ms).", string.Empty);
- BtnOpenCameras.Content = "打开相机";
- _isConfirmCameraInfo = false;
- TextCameraProcessTime.Text = string.Empty;
- // 清除耗时显示
- Dispatcher.Invoke(() =>
- {
- TextCameraProcessTime.Text = string.Empty;
- });
- }
- }
- /// <summary>
- /// 选择相机类型
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnComboBoxCameraTypeSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (_isConfirmCameraInfo)
- {
- MessageBox.Show("注意是否已打开相机,如需重新选择相机类型,请先关闭相机后再重新打开");
- }
- switch (ComboBoxCameraType.SelectedIndex)
- {
- case 0:
- _cameraInfo.CameraType = EnumCameraType.CameraMonocular;
- break;
- case 1:
- _cameraInfo.CameraType = EnumCameraType.CameraStereo;
- break;
- case 2:
- _cameraInfo.CameraType = EnumCameraType.CameraRealSense435;
- break;
- case 3:
- _cameraInfo.CameraType = EnumCameraType.CameraZhiSensor;
- break;
- }
- }
- /// <summary>
- /// 打开相机
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnOpenCameraClick(object sender, RoutedEventArgs e)
- {
- if (ComboBoxCameraType.SelectedIndex == -1)
- {
- MessageBox.Show("未选中相机类型");
- return;
- }
- if (ComboBoxCameras.SelectedIndex == -1)
- {
- MessageBox.Show("未选中相机源(用于拍摄人体和探头二维码模块)");
- return;
- }
- // 如果已打开相机,则关闭
- if (_cameraImageCapture != null)
- {
- _cameraImageCapture.StopCameraDataAcquire();
- _cameraImageCapture.NotifyCameraDataUpdate -= OnCameraDatasUpdate;
- _cameraImageCapture.NotifyError -= OnErrorOccur;
- _cameraImageCapture.Dispose();
- _cameraImageCapture = null;
- // 记录一下总共收到多少幅相机图像(主要用来对比一下保存数据输入记录、启用模型监测和不做任何操作时,是否有影响帧率)
- var timeSpan = DateTime.Now - _cameraImageReceiveStartTime;
- LogHelper.InfoLog("TotalReceivedCameraImageCount:" + _cameraImageReceiveCount + ",TimeSpan:" + timeSpan.TotalMilliseconds + "(ms).", string.Empty);
- BtnOpenCameras.Content = "打开相机";
- _isConfirmCameraInfo = false;
- TextCameraProcessTime.Text = string.Empty;
- // 清除耗时显示
- Dispatcher.Invoke(() =>
- {
- TextCameraProcessTime.Text = string.Empty;
- });
- }
- else
- {
- // 检查有没有重复打开同一个视频源
- var device = (FilterInfo)ComboBoxCameras.SelectedItem;
- var deviceName = device.MonikerString;
- _cameraInfo.CameraDeviceName = deviceName;
- // 临时设置相机参数
- SetCameraPara(_cameraInfo.CameraType);
- // todo 根据不同的相机类型选则相应实例
- switch (_cameraInfo.CameraType)
- {
- case EnumCameraType.CameraMonocular:
- break;
- case EnumCameraType.CameraStereo:
- break;
- case EnumCameraType.CameraRealSense435:
- break;
- case EnumCameraType.CameraZhiSensor:
- _cameraInfo.CameraDeviceName = "192.168.6.115";
- break;
- default:
- break;
- }
- _cameraImageCapture = new CameraDataCapture.CameraDataCapture(_cameraInfo);
- _cameraImageCapture.NotifyCameraDataUpdate += OnCameraDatasUpdate;
- _cameraImageCapture.NotifyError += OnErrorOccur;
- _cameraPara = _cameraImageCapture.GetCameraParaInfo;
- _cameraImageCapture.StartCameraDataAcquire();
- BtnOpenCameras.Content = "关闭相机";
- _isConfirmCameraInfo = true;
- _cameraImageReceiveStartTime = DateTime.Now;
- }
- }
- /// <summary>
- /// 加载视频
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnLoadVideoClick(object sender, RoutedEventArgs e)
- {
- StopPlay();
- var ofd = new OpenFileDialog();
- ofd.Filter = "图像或视频|*.png;*.bmp;*.jpg;*.jpeg;*.mp4;*.avi";
- ofd.Multiselect = false;
- ofd.Title = "选择一幅图像或一个视频";
- if (ofd.ShowDialog() == true)
- {
- var fileName = ofd.FileName;
- var fileExtension = Path.GetExtension(fileName);
- if (fileExtension == ".png" || fileExtension == ".bmp" || fileExtension == ".jpg" || fileExtension == ".jpeg")
- {
- var bitmap = new Bitmap(fileName);
- MyImageCanvas.UpdateRGBImage(bitmap);
- bitmap.Dispose();
- }
- else
- {
- double frameRate;
- long frameCount;
- lock (_videoReaderLocker)
- {
- _videoReader = new VideoFileReader();
- _videoReader.Open(fileName);
- frameRate = _videoReader.FrameRate.ToDouble();
- frameCount = _videoReader.FrameCount;
- }
- var sleepTime = (int)(1000 / frameRate);
- _playEvent.Reset();
- Task.Run(() =>
- {
- bool breakFlag = false;
- for (var ni = 0; ni < frameCount; ni++)
- {
- if (_playEvent.WaitOne(1))
- {
- breakFlag = true;
- break;
- }
- var currentTickCount = Environment.TickCount;
- Bitmap bitmap;
- lock (_videoReaderLocker)
- {
- bitmap = _videoReader.ReadVideoFrame(ni);
- }
- if (bitmap == null)
- {
- continue;
- }
- MyImageCanvas.UpdateRGBImage(bitmap);
- bitmap.Dispose();
- var detectTimeUsed = Environment.TickCount - currentTickCount;
- var finalSleepTime = sleepTime - detectTimeUsed;
- if (finalSleepTime < 0)
- {
- finalSleepTime = 0;
- }
- if (_playEvent.WaitOne(finalSleepTime))
- {
- breakFlag = true;
- break;
- }
- }
- if (!breakFlag)
- {
- lock (_videoReaderLocker)
- {
- _videoReader.Close();
- _videoReader.Dispose();
- _videoReader = null;
- }
- }
- });
- }
- }
- }
- /// <summary>
- /// 被选择的采集卡有变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnComboBoxCardsSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- StopPlay();
- }
- /// <summary>
- /// 刷新所有采集卡
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnRefreshCardsClick(object sender, RoutedEventArgs e)
- {
- RefreshComboBoxVideoCards();
- }
- /// <summary>
- /// 打开采集卡
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnBtnOpenGrabClick(object sender, RoutedEventArgs e)
- {
- if (ComboBoxCameras.SelectedIndex != -1)
- {
- StopPlay();
- var device = (FilterInfo)ComboBoxCameras.SelectedItem;
- _videoCapture = new VideoCaptureDevice(device.MonikerString);
- _videoCapture.NewFrame += OnVideoCaptureNewFrame;
- _videoCapture.Start();
- }
- else
- {
- MessageBox.Show("未检测到可用的视频采集设备!");
- }
- }
- #endregion
- #region private
- /// <summary>
- /// 刷新列表中的采集卡
- /// </summary>
- private void RefreshComboBoxVideoCards()
- {
- StopPlay();
- ComboBoxCameras.Items.Clear();
- var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
- foreach (var device in videoDevices)
- {
- ComboBoxCameras.Items.Add(device);
- }
- if (ComboBoxCameras.Items.Count > 0)
- {
- ComboBoxCameras.SelectedIndex = 0;
- }
- }
- /// <summary>
- /// 视频更新了一幅新的图像
- /// </summary>
- private void OnVideoCaptureNewFrame(object sender, NewFrameEventArgs e)
- {
- var bitmap = e.Frame;
- MyImageCanvas.UpdateRGBImage(bitmap);
- bitmap.Dispose();
- }
- /// <summary>
- /// 停止播放视频
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 有结果更新
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnCameraDatasUpdate(object sender, CameraDataUpdateEventArgs e)
- {
- //// 更新显示耗时
- string totalTime = string.Empty;
- string organSegTime = string.Empty;
- totalTime = e.UpdateCameraImageData.CameraProcessTime.ToString();
- Dispatcher.Invoke(() =>
- {
- TextCameraProcessTime.Text = totalTime;
- });
- int strTime = Environment.TickCount;
- _cameraImageReceiveCount += 1;
- var imgColor = e.UpdateCameraImageData.ImageColor;
- var PCLData = e.UpdateCameraImageData.PCLData;
- var bitmapRGB = RawImageShowUtils.RawImageToBitmap(imgColor.Clone());
- // 更新界面上显示的图片
- MyImageCanvas.UpdateRGBImage(bitmapRGB);
- imgColor.Dispose();
- PCLData.Dispose();
- bitmapRGB.Dispose();
- int useTime = Environment.TickCount - strTime;
- }
- /// <summary>
- /// 有log要记
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnLogWrite(object sender, LogEventArgs e)
- {
- switch (e.LogType)
- {
- case EnumLogType.InfoLog:
- LogHelper.InfoLog(e.Msg, string.Empty);
- break;
- case EnumLogType.WarnLog:
- LogHelper.WarnLog(e.Msg, string.Empty);
- break;
- case EnumLogType.ErrorLog:
- LogHelper.ErrorLog(e.Msg, string.Empty);
- break;
- case EnumLogType.FatalLog:
- LogHelper.FatalLog(e.Msg, string.Empty);
- break;
- }
- }
- /// <summary>
- /// 有错误发生
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnErrorOccur(object sender, ErrorEventArgs e)
- {
- MessageBox.Show("发生错误:" + e.GetException());
- }
- /// <summary>
- /// 设置相机参数
- /// </summary>
- /// <param name="cameraType"></param>
- private void SetCameraPara(EnumCameraType cameraType)
- {
- switch (cameraType)
- {
- case EnumCameraType.CameraMonocular:
- _cameraInfo.ImageWidth = 640;
- _cameraInfo.ImageHeight = 480;
- _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[1];
- _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 968.7391609376731f, Cx = 287.327201924683f, Fy = 973.0819774437676f, Cy = 228.3629053369949f };
- _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[1];
- _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
- {
- K1 = -0.1423318502459511f,
- K2 = 0.9421222939220543f,
- P1 = -0.00623928181067679f,
- P2 = -0.0001133860088364576f,
- K3 = -1.764144824643008f
- };
- //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
- break;
- case EnumCameraType.CameraStereo:
- _cameraInfo.ImageWidth = 640;
- _cameraInfo.ImageHeight = 480;
- _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[2];
- _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[2];
- _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 418.20295986882917f, Cx = 331.58936826262072f, Fy = 416.81959360159908f, Cy = 237.67218546829204f };
- _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
- {
- K1 = -0.054219464530630361f,
- K2 = 0.34091210469992994f,
- P1 = -0.0015726118762586696f,
- P2 = -0.0044293500195294868f,
- K3 = -0.54448581741567326f
- };
- _cameraInfo.CameraIntrinsicParas[1] = new StructCameraMatrix { Fx = 415.80928115541127f, Cx = 317.71374656324372f, Fy = 415.03444466070465f, Cy = 241.40091921800106f };
- _cameraInfo.CameraDistortionParas[1] = new StructDistortionCoeffs
- {
- K1 = -0.10663788123585277f,
- K2 = 0.52465203680907246f,
- P1 = -0.0035902534296184912f,
- P2 = -0.0031625627870525645f,
- K3 = -0.81617126606653123f
- };
- _cameraInfo.CameraExteriorPara = new StructCamExternalParam
- {
- RVec1 = 0.0032517163600536804f,
- RVec2 = -0.014466251153525995f,
- RVec3 = -0.0020912953401341617f,
- TVec1 = -120.19263575225523f,
- TVec2 = 0.37262915065413843f,
- TVec3 = -0.41720831397963321f
- };
- //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
- break;
- case EnumCameraType.CameraRealSense435:
- _cameraInfo.ImageWidth = 848;
- _cameraInfo.ImageHeight = 480;
- _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[1];
- _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 603.784f, Cx = 324.581f, Fy = 602.461f, Cy = 253.871f };
- _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[1];
- _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
- {
- K1 = 0.0f,
- K2 = 0.0f,
- P1 = 0.0f,
- P2 = 0.0f,
- K3 = 0.0f
- };
- //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
- break;
- case EnumCameraType.CameraZhiSensor:
- _cameraInfo.ImageWidth = 1280;
- _cameraInfo.ImageHeight = 720;
- _cameraInfo.CameraIntrinsicParas = new StructCameraMatrix[1];
- _cameraInfo.CameraIntrinsicParas[0] = new StructCameraMatrix { Fx = 957.37f, Cx = 660.29f, Fy = 950.03f, Cy = 479.29f };
- _cameraInfo.CameraDistortionParas = new StructDistortionCoeffs[1];
- _cameraInfo.CameraDistortionParas[0] = new StructDistortionCoeffs
- {
- K1 = 0.06723f,
- K2 = -0.06952f,
- P1 = -2.49e-03f,
- P2 = 1.24e-03f,
- K3 = 0.0f
- };
- //_cameraPara = new CameraPara(_cameraInfo.CameraType, _cameraInfo.CameraIntrinsicParas, _cameraInfo.CameraDistortionParas);
- break;
- }
- }
- private unsafe Bitmap BytesToBmp(byte[] bmpBytes, Size imageSize)
- {
- Bitmap bmp = new Bitmap(imageSize.Width, imageSize.Height);
- BitmapData bData = bmp.LockBits(new Rectangle(0, 0, imageSize.Width, imageSize.Height),
- ImageLockMode.ReadWrite,
- PixelFormat.Format48bppRgb);
- // Copy the bytes to the bitmap object
- Marshal.Copy(bmpBytes, 0, bData.Scan0, bmpBytes.Length);
- bmp.UnlockBits(bData);
- return bmp;
- }
- #endregion
- }
- }
|