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 { /// /// MainWindow.xaml 的交互逻辑 /// 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); } } /// /// 用户关闭窗口 /// /// /// 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; }); } } /// /// 选择相机类型 /// /// /// 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; } } /// /// 打开相机 /// /// /// 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; } } /// /// 加载视频 /// /// /// 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; } } }); } } } /// /// 被选择的采集卡有变 /// /// /// private void OnComboBoxCardsSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { StopPlay(); } /// /// 刷新所有采集卡 /// /// /// private void OnRefreshCardsClick(object sender, RoutedEventArgs e) { RefreshComboBoxVideoCards(); } /// /// 打开采集卡 /// /// /// 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 /// /// 刷新列表中的采集卡 /// 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; } } /// /// 视频更新了一幅新的图像 /// private void OnVideoCaptureNewFrame(object sender, NewFrameEventArgs e) { var bitmap = e.Frame; MyImageCanvas.UpdateRGBImage(bitmap); bitmap.Dispose(); } /// /// 停止播放视频 /// 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; } } /// /// 有结果更新 /// /// /// 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; } /// /// 有log要记 /// /// /// 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; } } /// /// 有错误发生 /// /// /// private void OnErrorOccur(object sender, ErrorEventArgs e) { MessageBox.Show("发生错误:" + e.GetException()); } /// /// 设置相机参数 /// /// 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 } }