使用C#对华为IPC摄像头二次开发
使用C#对华为的摄像头二次开发,摄像头是IPC摄像头。
开发环境:
操作系统:Win10 x64专业版2004
开发工具:VS2019 16.7.2
目标平台:x86,因为要操作内存,所以要打开允许非安全代码(项目属性==》生成==》勾选允许不安全代码)
首先去下载IPC SDK(点击下载,需要华为授权账户。)
新建一个WPF的项目,Framework版本为4.7
把下载的sdk压缩包中的windows\output32目录中的HWPuSDK.dll和lib目录中的所有文件,都复制到项目的bin/debug目录中(和生成的exe同级),华为的这个SDK对64位支持不好,使用64位遇到不少问题,最终还是先采用32位的DLL。
项目中对图像的手动处理,经过对比,在Emgu CV和OpenCVSharp4中采用了OpenCVSharp4,个人感觉OpenCVSharp4使用起来更简洁方便。
项目中对视频流的回调手动处理展示,采用WriteableBitmap(参考吕毅大神的《WPF 高性能位图渲染 WriteableBitmap 及其高性能用法示例》),本来想采用D3D这种显卡加速的方法,无奈没有找到相关文章和资料,如果哪位大神有资料,还望告知一下。谢谢!
项目中引用了以下组件
在本次开发中,我们先实现自动预览摄像头视频和手动对摄像头视频流进行处理。
因为SDK自动播放需要传入一个控件的句柄,而WPF中窗体上所有控件的句柄都是窗体本身,所以我们还需要使用WindowsFromHost来使用Winform的一些控件来实现播放句柄的传入。
项目中引用WindowsFromsIntegeration
在项目的MainWindow.xaml中添加三个按钮、两个RadioButton、一个Winform的PictureBox和一个WPF的Image控件。大致布局如下:
详细的xaml代码:
<Window x:Class="HuaWeiCamera.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" mc:Ignorable="d" WindowStartupLocation="CenterScreen" Loaded="MainWindow_OnLoaded" Title="MainWindow" Height="800" Width="1200" Closed="MainWindow_OnClosed"> <Grid Margin="0,0,2,0"> <Grid.RowDefinitions> <RowDefinition Height="60"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <WrapPanel VerticalAlignment="Center"> <StackPanel Margin="30,5,0,0" VerticalAlignment="Center"> <Button Content="预览摄像头" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Height="30" Click="ButtonView_OnClick" /> <WrapPanel Margin="0,5,0,0"> <RadioButton Content="自动处理" VerticalAlignment="Center" IsChecked="True" GroupName="PlayMode" x:Name="RadioButtonAuto" /> <RadioButton Content="手动处理" VerticalAlignment="Center" GroupName="PlayMode" x:Name="RadioButtonManual" Margin="10,0,0,0" /> </WrapPanel> </StackPanel> <Button x:Name="ButtonSaveOne" Content="抓拍一张" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="30,0,0,0" Width="75" Height="30" IsEnabled="False" Click="ButtonSave_OnClick" />
<Button Content="人脸抓拍" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="30,0,0,0" Width="75" Height="30" /> </WrapPanel> <WrapPanel Grid.Row="1"> <WindowsFormsHost HorizontalAlignment="Center" Width="1200" Height="700" VerticalAlignment="Center" x:Name="FormsHostVideo"> <wf:PictureBox x:Name="ImagePlay"></wf:PictureBox> </WindowsFormsHost> <Image VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="CanvaVideo" Stretch="Fill" Source="{Binding Path=VideoWriteableBitmap}" /> </WrapPanel> </Grid> </Window>
在App.cs中定义下日志记录类
public partial class App : Application { public static NLog.Logger NewNLog; private void App_OnStartup(object sender, StartupEventArgs e) { DispatcherUnhandledException += App_DispatcherUnhandledException; NewNLog = NLog.LogManager.GetLogger("HuaWeiCameraLoger"); } }
根据华为的《SDC 8.0.1 SDK开发指南》,我们要实现摄像头预览,需要先定义以下几个struct和enum:
sturct:PU_REAL_PLAY_INFO_S(视频实时预览结构体)、PU_TIME_S(时间结构体)
enum:PU_PROTOCOL_TYPE(传输协议类型)、PU_STREAM_TYPE(码流类型)、PU_VIDEO_TYPE(数据流类型)、PU_MEDIA_CRYPTO_TYPE(加密类型)、PU_MEDIA_CALLBACK_TYPE(回调类型)
using System; using System.Runtime.InteropServices; using HuaWeiCamera.Enums; using HuaWeiCamera.Enums.Media; using HuaWeiCamera.Enums.Video; namespace HuaWeiCamera.Struct { /// <summary> /// 视频实时预览结构体,http://www.cnblogs.com/wdw984 /// </summary> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct PU_REAL_PLAY_INFO_S { /// <summary> /// 设备通道号,一般为101。 /// </summary> public uint ulChannelId; /// <summary> /// 播放窗口句柄,为IntPtr.Zero表示用户自己处理视频数据流,不自动播放视频流 /// </summary> public IntPtr hPlayWnd; /// <summary> /// 码流类型,主码流、子码 /// </summary> public PU_STREAM_TYPE enStreamType; /// <summary> /// 流类型:视频流、音频流、复合流、录 像流、元数据 /// </summary> public PU_VIDEO_TYPE enVideoType; /// <summary> /// 传输协议类型,UDP,TCP /// </summary> public PU_PROTOCOL_TYPE enProtocolType; /// <summary> /// 回调类型:0:RTP解密1:RTP不解密 2:Frame 3:YUV /// </summary> public PU_MEDIA_CALLBACK_TYPE enMediaCallbackType; /// <summary> /// 请求端IP,第三方平台可以不填,SDK会 自动获取 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string szLocalIp; /// <summary> /// 是否保活 /// </summary> public bool bKeepLive; /// <summary> /// 请求预录、录像开始时间(本地时 间)。 /// </summary> public PU_TIME_S stStartTime; /// <summary> /// 请求预录、录像结束时间(本地时 间)。 /// </summary> public PU_TIME_S stEndTime; /// <summary> /// 加密类型,只支持AES加密。 /// </summary> public PU_MEDIA_CRYPTO_TYPE enMediaCryptoType; /// <summary> /// 加密密钥 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 44)] public string szMediaCrypto; /// <summary> /// szReserved[0-15]表示组播IP地址 /// szReserved[16-19]表示组播端口 /// szReserved[22]表示智能分析数据打包 格式 0:XML,1:元数据 /// szReserved[23]表示元数据请求类型,取值参考枚举 PU_METADATA_REQUEST_TYPE_E定义 /// </summary> [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public byte[] szReserved; } }
namespace HuaWeiCamera.Enums { /// <summary> /// 视频流类型,http://www.cnblogs.com/wdw984 /// </summary> public enum PU_STREAM_TYPE { /// <summary> /// 视频主码流 /// </summary> PU_VIDEO_MAIN_STREAM=0, /// <summary> /// 视频子码流 /// </summary> PU_VIDEO_SUB_STREAM1, /// <summary> /// 视频子码流2(VWareC01 不支持) /// </summary> PU_VIDEO_SUB_STREAM2, /// <summary> /// 视频子码流3(VWareC01 不支持) /// </summary> PU_VIDEO_SUB_STREAM3, /// <summary> /// 视频子码流4 /// </summary> PU_VIDEO_SUB_STREAM4, /// <summary> /// 视频子码流5 /// </summary> PU_VIDEO_SUB_STREAM5, /// <summary> /// 预留值 /// </summary> PU_VIDEO_STREAM_MAX } }
namespace HuaWeiCamera.Enums.Video { /// <summary> /// 码流类型,http://www.cnblogs.com/wdw984 /// </summary> public enum PU_VIDEO_TYPE { /// <summary> /// 视频流 /// </summary> PU_VIDEO_TYPE_VIDEO = 0, /// <summary> /// 音频流 /// </summary> PU_VIDEO_TYPE_AUDIO, /// <summary> /// 复合流 /// </summary> PU_VIDEO_TYPE_MUX, /// <summary> /// 录像流 /// </summary> PU_VIDEO_TYPE_RECORD, /// <summary> /// 元数据流 /// </summary> PU_VIDEO_TYPE_META, /// <summary> /// 视频+元数据流 /// </summary> PU_VIDEO_TYPE_VIDEO_META, /// <summary> /// 预留值 /// </summary> PU_VIDEO_TYPE_MAX } }
namespace HuaWeiCamera.Enums { /// <summary> /// 数据传输类型,http://www.cnblogs.com/wdw984 /// </summary> public enum PU_PROTOCOL_TYPE { /// <summary> /// UDP /// </summary> PU_PROTOCOL_TYPE_UDP = 0, /// <summary> /// TCP /// </summary> PU_PROTOCOL_TYPE_TCP, /// <summary> /// 组播方式 /// </summary> PU_PROTOCOL_TYPE_MULTICAST, /// <summary> /// 预留值 /// </summary> PU_PROTOCOL_TYPE_MAX } }
namespace HuaWeiCamera.Enums.Media { /// <summary> /// 媒体回调类型,http://www.cnblogs.com/wdw984 /// </summary> public enum PU_MEDIA_CALLBACK_TYPE { /// <summary> /// RTP包方式 /// </summary> PU_MEDIA_CALLBACK_TYPE_RTP = 0, /// <summary> /// RTP包形式,不解密 /// </summary> PU_MEDIA_CALLBACK_TYPE_RTP_CRYPTO, /// <summary> /// 帧回调方式 /// </summary> PU_MEDIA_CALLBACK_TYPE_FRAME, /// <summary> /// YUV方式,Linux不支持 /// </summary> PU_MEDIA_CALLBACK_TYPE_YUV, /// <summary> /// 把RTP包回调给控件方处理方式,Linux不支持 /// </summary> PU_MEDIA_CALLBACK_TYPE_FOR_STORAGE, /// <summary> /// 智能元数据方式 /// </summary> PU_MEDIA_CALLBACK_TYPE_META_FRAME, /// <summary> /// 预留值 /// </summary> PU_MEDIA_CALLBACK_TYPE_MAX } }
using System.Runtime.InteropServices; namespace HuaWeiCamera.Struct { /// <summary> /// 时间结构体,http://www.cnblogs.com/wdw984 /// </summary> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct PU_TIME_S { /// <summary> /// 年 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)] public string szYear; /// <summary> /// 月 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string szMonth; /// <summary> /// 日 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string szDay; /// <summary> /// 时 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string szHour; /// <summary> /// 分 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string szMinute; /// <summary> /// 秒 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string szSecond; } }
定义一个静态类,用来实现调用SDK和摄像头交互(HuaWeiSDKHelper)
using System; using System.Runtime.InteropServices; using HuaWeiCamera.Enums; using HuaWeiCamera.Enums.SnapShot; using HuaWeiCamera.Struct; namespace HuaWeiCamera.Class { /// <summary> /// 华为HoloSens SDC二次开发使用,http://www.cnblogs.com/wdw984 /// </summary> public static class HuaWeiSdkHelper { private const string SdkPath= "HWPuSDK.dll"; #region 初始化和登录 /// <summary> /// 初始化设备 /// </summary> /// <param name="ulLinkMode">0自动 1手动 3混合模式</param> /// <param name="szLocalIp">本地IP</param> /// <param name="ulLocalPort">本地端口</param> /// <returns></returns> [DllImport(SdkPath, EntryPoint = "IVS_PU_Init", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)] public static extern bool IVS_Pu_Init(uint ulLinkMode, string szLocalIp, uint ulLocalPort); /// <summary> /// 远程登录设备 /// </summary> /// <param name="szLoginIp">设备IP</param> /// <param name="ulLoginPort">设备端口 6060</param> /// <param name="szUserName">登录名 ApiAdmin</param> /// <param name="szPasswd">登录密码 HuaWei123</param> /// <returns></returns> [DllImport(SdkPath, EntryPoint = "IVS_PU_Login", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)] public static extern uint IVS_PU_Login(string szLoginIp, uint ulLoginPort, string szUserName, string szPasswd); /// <summary> /// 初始化和登录设备 /// </summary> /// <param name="sdcIp">SDC设备IP</param> /// <param name="sdcPort">SDC设备端口</param> /// <param name="sdcUser">SDC登录用户名</param> /// <param name="sdcPwd">SDC登录密码</param> /// <param name="errMsg">失败时的错误信息</param> /// <param name="ulIdentifyId">登录成功后返回登录句柄</param> public static void InitAndLogin(string sdcIp,uint sdcPort,string sdcUser,string sdcPwd,out uint ulIdentifyId,out string errMsg) { ulIdentifyId = 0; errMsg = ""; //要开启TLS的情况时:初始化调用IVS_PU_InitEx接口,登录调用IVS_PU_Login 接口时端口号设置为6061 if (!IVS_Pu_Init(1, "192.168.2.144", 6060)) { errMsg=($"设备初始化失败,{GetLastErrorInfo()}"); return; } ulIdentifyId = IVS_PU_Login(sdcIp, sdcPort, sdcUser, sdcPwd); if (ulIdentifyId == 0) { errMsg=$"设备登录失败,{GetLastErrorInfo()}"; } } #endregion #region 预览相关 /// <summary> /// 实时预览 /// </summary> /// <param name="ulIdentifyId">登录成功后返回的用户编号</param> /// <param name="pstRealPlayInfo">播放结构体</param> /// <param name="fRealDataCallBack">回调实现播放</param> /// <param name="pUsrData">传入码流数据 回调函数作为参数</param> /// <returns></returns> [DllImport(SdkPath, EntryPoint = "IVS_PU_RealPlay")] public static extern uint IVS_PU_RealPlay(uint ulIdentifyID, PU_REAL_PLAY_INFO_S[] pstRealPlayInfo, PfRealDataCallBack fRealDataCallBack, ref IntPtr pUsrData); public delegate void PfRealDataCallBack(IntPtr szBuffer, int lSize, IntPtr pUsrData); /// <summary> /// 停止实时预览 /// </summary> /// <param name="ulIdentifyId">登录成功后返回的用户编号</param> /// <param name="ulRealHandle">实时播放句柄</param> /// <returns></returns> [DllImport(SdkPath, EntryPoint = "IVS_PU_StopRealPlay", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)] public static extern uint IVS_PU_StopRealPlay(uint ulIdentifyId, uint ulRealHandle); #endregion #region 错误信息相关 /// <summary> /// 获取最后一次错误代码 /// </summary> /// <returns></returns> [DllImport(SdkPath, EntryPoint = "IVS_PU_GetLastError", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)] private static extern int IVS_PU_GetLastError(); /// <summary> /// 根据错误代码返回错误信息 /// </summary> /// <param name="ulErrorNo">错误编号</param> /// <returns></returns> [DllImport(SdkPath, EntryPoint = "IVS_PU_GetErrorMsg", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)] private static extern IntPtr IVS_PU_GetErrorMsg(int ulErrorNo); /// <summary> /// 返回最后一次错误码和错误信息 /// </summary> /// <returns></returns> public static string GetLastErrorInfo() { var lastErrorCode = IVS_PU_GetLastError(); var lastErrorMsg = Marshal.PtrToStringAnsi(IVS_PU_GetErrorMsg(lastErrorCode)); return $"错误码:{IVS_PU_GetLastError()},错误信息:{lastErrorMsg}"; } #endregion #region 退出登录 /// <summary> /// 退出登录 /// </summary> /// <param name="ulIdentifyId">登录成功后返回的句柄编号</param> /// <returns></returns> [DllImport(SdkPath, EntryPoint = "IVS_PU_Logout", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)] public static extern bool IVS_PU_Logout(uint ulIdentifyId); /// <summary> /// 反注册设备,退出时进行设备释放 /// </summary> /// <returns></returns> [DllImport(SdkPath, EntryPoint = "IVS_PU_Cleanup", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)] public static extern bool IVS_PU_Cleanup(); #endregion } }
定义一个继承了INotifyPropertyChanged的Model,用来实现刷新Image控件
public class VideoYuvModelView : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private WriteableBitmap bitmapImage { get; set; } public WriteableBitmap VideoWriteableBitmap { get => bitmapImage; set { bitmapImage = value; PropertyChanged?.Invoke(this,new PropertyChangedEventArgs("VideoBitmapImage")); } } }
在MainWindow.cs中我们需要定义一些变量:
private uint _ulIdentifyId;//登录摄像头后返回的编号 private uint _ulRealHandleId;//调用预览SDK返回的结果值 private static bool _isSave;//是否保存当前帧为图片 private static bool _isExit;//是否退出
private static HuaWeiSdkHelper.PfRealDataCallBack _fedRealPlayCallbackWithYUV;//手动处理摄像头回调时的委托事件
private const uint ByteLength = 1920 * 1080 * 4;//位图的大小 private static readonly VideoYuvModelView VideoYuvModelView = new VideoYuvModelView();//Image的数据源,用来手动处理视频时候展示图像 private IntPtr _videoPlayHandle = IntPtr.Zero;//自动预览时的控件句柄 [DllImport("kernel32.dll")] private static extern void CopyMemory(IntPtr destination, IntPtr source, uint length);//用来复制内存中的数据
在窗体初始化和加载事件中,我们来初始化一些数据的绑定
public MainWindow() { InitializeComponent(); VideoYuvModelView.VideoWriteableBitmap = new WriteableBitmap(1920, 1800, 96.0, 96.0, PixelFormats.Bgr32, null);//因为摄像头返回的图片大小时1920*1080,所以这里定义的大小要和返回的图片大小一致 } private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { CanvaVideo.DataContext = VideoYuvModelView;//手动处理时,Image控件数据源 _videoPlayHandle = ImagePlay.Handle;//自动处理时,使用Winform的控件句柄 }
预览摄像头按钮事件,中间根据选中的自动处理和手动处理来分别做出不同的相应
#region 视频预览(自动和手动处理视频流) private void ButtonView_OnClick(object sender, RoutedEventArgs e) { if (0 == _ulIdentifyId) {
//这里通过网络登录到摄像头,具体端口、用户名、密码请参考开发手册 HuaWeiSdkHelper.InitAndLogin("192.168.2.250", 6060, "ApiAdmin", "HuaWei123", out _ulIdentifyId, out string errMsg); if (0 == _ulIdentifyId) { MessageBox.Show(errMsg); return; } } var prpInfos = new PU_REAL_PLAY_INFO_S[1]; var clientInfo = new PU_REAL_PLAY_INFO_S { ulChannelId = 101, hPlayWnd = _videoPlayHandle, enProtocolType = PU_PROTOCOL_TYPE.PU_PROTOCOL_TYPE_TCP, enStreamType = PU_STREAM_TYPE.PU_VIDEO_MAIN_STREAM, enVideoType = PU_VIDEO_TYPE.PU_VIDEO_TYPE_VIDEO, enMediaCryptoType = PU_MEDIA_CRYPTO_TYPE.PU_MEDIA_CRYPTO_NONE, enMediaCallbackType = PU_MEDIA_CALLBACK_TYPE.PU_MEDIA_CALLBACK_TYPE_RTP, szReserved = new byte[32], bKeepLive = true }; IntPtr pUsrData = (IntPtr)_ulIdentifyId; if (RadioButtonManual.IsChecked == true) { //手动处理视频预览 FormsHostVideo.Visibility = Visibility.Collapsed; CanvaVideo.Visibility = Visibility.Visible; clientInfo.hPlayWnd = IntPtr.Zero; clientInfo.enMediaCallbackType = PU_MEDIA_CALLBACK_TYPE.PU_MEDIA_CALLBACK_TYPE_YUV; prpInfos[0] = clientInfo; _fedRealPlayCallbackWithYUV = FedRealPlayCallbackWithYUV; //手动处理回调 _ulRealHandleId = HuaWeiSdkHelper.IVS_PU_RealPlay(_ulIdentifyId, prpInfos, _fedRealPlayCallbackWithYUV, ref pUsrData); if (0 == _ulRealHandleId) { MessageBox.Show(HuaWeiSdkHelper.GetLastErrorInfo()); } ButtonSaveOne.IsEnabled = true;//点击 抓拍一张 按钮,会把_isSave变量设置为true,从而在回调事件中可以保存当前帧为图片 } else {
ButtonSaveOne.IsEnabled = false; CanvaVideo.Visibility = Visibility.Collapsed; FormsHostVideo.Visibility = Visibility.Visible; prpInfos[0] = clientInfo; //传入句柄,自动预览 _ulRealHandleId = HuaWeiSdkHelper.IVS_PU_RealPlay(_ulIdentifyId, prpInfos, null, ref pUsrData); if (0 == _ulRealHandleId) { MessageBox.Show(HuaWeiSdkHelper.GetLastErrorInfo()); } } } #region 手动解析YUV数据并展示在界面上 private static void FedRealPlayCallbackWithYUV(IntPtr szBuffer, int lSize, IntPtr pUsrData) { if (_isExit) return; try { Span<byte> nativeSpan; unsafe { nativeSpan = new Span<byte>(szBuffer.ToPointer(), lSize); } if (nativeSpan.Length > 0) { #region 处理视频流YUV图像 Mat yuvImg = new Mat(1080 * 3 / 2, 1920, MatType.CV_8UC1); Mat rgbImg = new Mat(1080, 1920, MatType.CV_8UC1); Marshal.Copy(nativeSpan.ToArray(), 0, yuvImg.Data, nativeSpan.Length); Cv2.CvtColor(yuvImg, rgbImg, ColorConversionCodes.YUV2RGBA_I420); Application.Current.Dispatcher?.InvokeAsync(() => { VideoYuvModelView.VideoWriteableBitmap.Lock(); unsafe { CopyMemory(VideoYuvModelView.VideoWriteableBitmap.BackBuffer, new IntPtr(rgbImg.DataPointer), ByteLength); } VideoYuvModelView.VideoWriteableBitmap.AddDirtyRect(new Int32Rect(0, 0, 1920, 1080)); VideoYuvModelView.VideoWriteableBitmap.Unlock(); }); if (_isSave) { Cv2.ImWrite(Path.Combine($"{AppDomain.CurrentDomain.BaseDirectory}", "jpg",$"{Guid.NewGuid()}.jpg"), rgbImg); _isSave = false; } #endregion } } catch (Exception e) { App.NewNLog.Error($"解析视频流出错:{e}"); } } #endregion #endregion
在程序关闭的时候进行资源释放。
private void MainWindow_OnClosed(object sender, EventArgs e) { _isExit = true;if (_ulRealHandleId > 0) { HuaWeiSdkHelper.IVS_PU_StopRealPlay(_ulIdentifyId, _ulRealHandleId);//停止预览 } if (_ulIdentifyId > 0) { HuaWeiSdkHelper.IVS_PU_Logout(_ulIdentifyId);//退出登录 } HuaWeiSdkHelper.IVS_PU_Cleanup();//SDK资源释放 }
具体效果如下:
1、自动预览
2、手动处理
但是这里的YUV回调填充到WriteableBitmap遇到个问题,就是无法填满WriteableBitmap(把WriteableBitmap中的数据保存下来也是和预览的效果一样,估计是填充的算法不对),只能填充一部分,这里涉及到本人的知识盲区,暂时没法解决掉。
3、一帧图像保存为本地图片
至此我们使用C#初步实现了华为IPC摄像头的预览和数据流处理(也可以把处理类型换成视频流,然后存本地视频文件,因为默认时h265编码格式,播放时使用PotPlayer播放器来播放),算是入门了。
下一章我们将实现调用SDK来实现人脸自动抓拍效果。
=======================================================================================
上一篇我们实现了用SDK登录摄像头并实现预览,这次我们实现通过SDK调用摄像头本身自带的人脸抓拍功能。
因为篇幅较短,这里直接上代码。
首先我们在MainWindow代码里定义一个安全队列用来存储抓拍到的人脸数据,一个定时取队列数据的定时器,一个人脸抓拍回调事件
private static ConcurrentQueue<CaptureInfo> _concurrentQueue = new ConcurrentQueue<CaptureInfo>(); private static HuaWeiSdkHelper.PfRealDataCallBack _fedRealPlayCallbackFaceCapture; private Timer _timer;
在窗体加载事件中初始化定时器,用来把抓拍到的数据保存到本地
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) {
//上一篇文章中的代码省略 _timer = new Timer(300) { Enabled = false }; _timer.Elapsed += Timer_Elapsed; }
SDK定义了人脸捕获需要定义的Struct和Enum
namespace HuaWeiCamera.Struct { /// <summary> /// 元数据获取相关参数 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct PU_META_DATA { /// <summary> /// 数据容量 /// </summary> public ushort usCapacity; /// <summary> /// 有效数目 /// </summary> public ushort usValidNumber; /// <summary> /// 参考PU_UserData 定义 /// </summary> public System.IntPtr pstMetaUserData; } }
namespace HuaWeiCamera.Struct { /// <summary> /// 元数据用户数据 /// </summary> [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 1)] public struct PU_UserData { /// <summary> /// 元数据类型 /// </summary> public LAYER_THREE_TYPE eType; /// <summary> /// 用户元数据详情 /// </summary> public PU_UserData_unMetadata Union1; } }
namespace HuaWeiCamera.Struct { /// <summary> /// 用户元数据详情 /// </summary> [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)] public struct PU_UserData_unMetadata { [System.Runtime.InteropServices.FieldOffset(0)] public int bBoolValue; [System.Runtime.InteropServices.FieldOffset(0)] public byte charValue; [System.Runtime.InteropServices.FieldOffset(0)] public byte ucharValue; [System.Runtime.InteropServices.FieldOffset(0)] public short shortValue; [System.Runtime.InteropServices.FieldOffset(0)] public ushort ushortValue; [System.Runtime.InteropServices.FieldOffset(0)] public int IntValue; [System.Runtime.InteropServices.FieldOffset(0)] public uint uIntValue; [System.Runtime.InteropServices.FieldOffset(0)] public long longlongValue; [System.Runtime.InteropServices.FieldOffset(0)] public ulong uLonglongValue; /// <summary> /// 元数据二进制颜色 /// </summary> [System.Runtime.InteropServices.FieldOffset(0)] public ST_BINARY stBinay; /// <summary> /// 元数据矩形 /// </summary> [System.Runtime.InteropServices.FieldOffset(0)] public META_RECT_S stRec; /// <summary> /// 元数据划点 /// </summary> [System.Runtime.InteropServices.FieldOffset(0)] public META_POINT_S stPoint; /// <summary> /// 元数据划线 /// </summary> [System.Runtime.InteropServices.FieldOffset(0)] public META_LINE_S stLine; [System.Runtime.InteropServices.FieldOffset(0)] public IntPtr stPolyGon; [System.Runtime.InteropServices.FieldOffset(0)] public IntPtr stColor; [System.Runtime.InteropServices.FieldOffset(0)] public IntPtr stHumanAttr; /// <summary> /// 人脸信息 /// </summary> [System.Runtime.InteropServices.FieldOffset(0)] public META_FACE_INFO stFaceInfo; /// <summary> /// 人脸属性 /// </summary> [System.Runtime.InteropServices.FieldOffset(0)] public META_FACE_ATTRIBUTES stFaceAttr; [System.Runtime.InteropServices.FieldOffset(0)] public IntPtr szUserData; } }
namespace HuaWeiCamera.Enums { /// <summary> /// 元数据类型以《全网智能接口对接TLV数据详解文档为准》 /// 下载链接:https://support.huawei.com/enterprise/zh/doc/EDOC1100084903 /// </summary> public enum LAYER_THREE_TYPE { /// <summary> /// 时间戳 /// </summary> PTS = 0x09000001, /// <summary> /// 处理图片宽 /// </summary> IMG_WIDTH = 0x07000100, /// <summary> /// 处理图片高 /// </summary> IMG_HEIGHT = 0x07000101, // 人脸 /// <summary> /// 人脸ID /// </summary> FACE_ID = 0x07000016, /// <summary> /// 人脸全景图片大小 /// </summary> FACE_PANOPIC_SIZE = 0x07000018, /// <summary> /// 人脸抠图产生时间 /// </summary> FACE_PIC_TIME = 0x08000015, /// <summary> /// 人脸抠图设备时区(单位ms 东区为+ 西区为-) /// </summary> FACE_PIC_TZONE = 0x08000020, /// <summary> /// 人体属性(不建议使用,使用0X070003xx开始的单个人体属性代替) /// </summary> HUMAN_FEATURE = 0x10000002, /// <summary> /// 人脸属性(不建议使用,使用0X070002xx开始的单个人脸属性代替) /// </summary> FACE_FEATURE = 0x11000003, /// <summary> /// 全景图片 /// </summary> PANORAMA_PIC = 0x0A00000A, /// <summary> /// 人脸抠图 /// </summary> FACE_PIC = 0x0A000012, /// <summary> /// 人脸抠图kps质量过滤标志位 /// </summary> FACE_PIC_KPS = 0x07000012, /// <summary> /// 人体抠图 /// </summary> HUMAN_PIC = 0x0A000013, /// <summary> /// 人体抠图kps质量过滤标志位 /// </summary> HUMAN_PIC_KPS = 0x07000013, /// <summary> /// 人体抠图中的人体目标框 /// </summary> HUMAN_PIC_ROI = 0x0B000017, /// <summary> /// 人脸全景 /// </summary> FACE_PANORAMA = 0x0A000017, /// <summary> /// 人脸抠图小框位置 /// </summary> FACE_PIC_POSITION = 0x0B000011, /// <summary> /// 人脸位置(实时位置框, 万分比) (开始支持版本:SDC V500R019C30) /// </summary> FACE_POS = 0x0B000012, /// <summary> /// 人脸数据库中匹配图片 /// </summary> FACE_MATCH = 0x0A000014, /// <summary> /// 名单库中的人脸ID,用来维持特征 record的一致性 /// </summary> FACELIB_RECORDID = 0x07000017, /// <summary> /// 人脸匹配率 /// </summary> FACE_MATCHRATE = 0x07000020, /// <summary> /// 人脸信息,对应数据库中信息 /// </summary> FACE_INFO = 0x12000001, /// <summary> /// 名单库类型 /// </summary> FACE_LIB_TYPE = 0x07000022, /// <summary> /// 名单库名字 /// </summary> FACE_LIB_NAME = 0x0A000015, /// <summary> /// target类型,所有智能的业务类型(开始支持版本:SDC V500R019C30) /// </summary> TARGET_TYPE = 0x07000023, /// <summary> /// 人脸属性, 以FACE开头 0 表示未知 1~n依次对应注释的属性 /// 眼镜{无,有} /// </summary> FACE_GLASS = 0X07000200, /// <summary> /// 性别{女,男} /// </summary> FACE_GENDER = 0X07000201, /// <summary> /// 年龄,具体的年龄值1~99 /// </summary> FACE_AGE = 0X07000202, /// <summary> /// 遮档(口罩) {无,是} /// </summary> FACE_MOUTHMASK = 0X07000203, /// <summary> /// 人脸表情{微笑、愤怒、悲伤、正常、惊讶} /// </summary> FACE_EXPRESSION = 0X07000204, /// <summary> /// 帽子{无, 有} /// </summary> FACE_HAT = 0X07000205, /// <summary> /// 胡子{无, 有} (支持版本:SDC 8.0.1) /// </summary> FACE_MUSTACHE = 0X07000206, /// <summary> /// 发型{长, 短}(支持版本:SDC 8.0.1) /// </summary> FACE_HAIR = 0X07000207, /// <summary> /// 眼镜{无,普通眼镜,太阳眼镜} (开始支持版本:SDC 8.0.1) /// </summary> FACE_GLASS_TYPE = 0X07000208, /// <summary> /// 人体属性类 以HUMAN开头 0 表示未知 1~n依次对应注释的属性 /// 年龄 {少年,青年,老年} /// </summary> HUMAN_AGE = 0X07000300, /// <summary> /// 性别{男,女} /// </summary> HUMAN_GENDER = 0X07000301, /// <summary> /// 上衣款式 {长袖,短袖} /// </summary> HUMAN_UPPERSTYLE = 0X07000302, /// <summary> /// 上衣颜色 {黑,蓝,绿,白/灰,黄/橙/棕,红/粉/紫} /// </summary> HUMAN_UPPERCOLOR = 0X07000303, /// <summary> /// 上衣纹理 {纯色,条纹,格子} /// </summary> HUMAN_UPPERTEXTURE = 0X07000304, /// <summary> /// 下衣款式 {长裤,短裤,裙子} /// </summary> HUMAN_LOWSTYLE = 0X07000305, /// <summary> /// 下衣颜色 {黑,蓝,绿,白/灰,黄/橙/棕,红/粉/紫} /// </summary> HUMAN_LOWERCOLOR = 0X07000306, /// <summary> /// 体型{standard, fat, thin} /// </summary> HUMAN_SHAPE = 0X07000307, /// <summary> /// 口罩{no,yes} /// </summary> HUMAN_MOUTHMASK = 0X07000308, /// <summary> /// 发型{ long, short } /// </summary> HUMAN_HAIR = 0X07000309, /// <summary> /// 背包{no,yes} /// </summary> HUMAN_BACKPACK = 0X0700030A, /// <summary> /// 是否拎东西{no,yes} /// </summary> HUMAN_CARRY = 0X0700030B, /// <summary> /// 斜挎包{no,yes} /// </summary> HUMAN_SATCHEL = 0X0700030C, /// <summary> /// 雨伞{no,yes} /// </summary> HUMAN_UMBRELLA = 0X0700030D, /// <summary> /// 前面背包{no,yes} /// </summary> HUMAN_FRONTPACK = 0X0700030E, /// <summary> /// 行李箱{no,yes} /// </summary> HUMAN_LUGGAGE = 0X0700030F, /// <summary> /// 行进方向{forward,backward} /// </summary> HUMAN_DIRECT = 0X07000310, /// <summary> /// 行进速度{slow,fast} /// </summary> HUMAN_SPEED = 0X07000311, /// <summary> /// 朝向{frontal, back, leftprofiled, rightprofiled} /// </summary> HUMAN_VIEW = 0X07000312, /// <summary> /// 眼镜{no,glass, sunglass} /// </summary> HUMAN_GLASS = 0X07000313, /// <summary> /// 戴帽子{no, yes} /// </summary> HUMAN_HAT = 0X07000314, /// <summary> /// 非机动车属性类 以RIDERMAN开头 0 表示未知 1~n依次对应注释的属性 /// </summary> RIDERMAN_AGE = 0X07000400, // 年龄 {少年,青年,老年} RIDERMAN_GENDER = 0X07000401, // 性别{男,女} RIDERMAN_UPPERSTYLE = 0X07000402, // 上衣款式 {长袖,短袖} RIDERMAN_UPPERCOLOR = 0X07000403, // 上衣颜色 {黑,蓝,绿,白/灰,黄/橙/棕,红/粉/紫} RIDERMAN_HELMET = 0X07000404, // 是否戴头盔 {no, yes} RIDERMAN_HELMETCOLOR = 0X07000405, // 头盔颜色 {黑,蓝,绿,白/灰,黄/橙/棕,红/粉/紫} APPROACH_LANE_ID = 0x07000605, // 临近车道号(开始支持版本:SDC 8.0.1) // 人体 HUMAN_RECT = 0x0B000013, // 人体位置(实时位置框) HUMAN_RECT_POSITION = 0x0B000014, // 人体抠图小框位置 SHOULDER_RECT = 0x0B000018, // 头肩位置 SHOULDER_NUM = 0x06000001, // 头肩个数 QUEUE_TIME = 0x06000002, // 排队时长 OBJ_ID = 0x07000021, // 目标ID OBJ_STATUS = 0x06000022, // 目标状态 OBJ_POS = 0x0B000023, // 目标位置 OBJ_TYPE = 0x06000024, // 目标类型 OBJ_SPEED = 0x0C000025, // 目标速度 OBJ_UPHALF_COLOR = 0x0F000026, // 目标上半部颜色 OBJ_DOWNHALF_COLOR = 0x0F000027, // 目标下半部颜色 RULE_TYPE = 0x07000031, // 规则类型 RULE_LINE_POS = 0x0D000032, // 规则线位置 RULE_LINE_DIR = 0x07000033, // 规则线方向 RULE_AREA_POS = 0x0E000034, // 规则框位置 OBJ_POS_R = 0x0B000035, // 目标位置(相对位置) OBJ_SPEED_R = 0x0C000036, // 目标速度(相对位置) RULE_LINE_POS_R = 0x0D000037, // 规则线位置(相对位置) RULE_AREA_POS_R = 0x0E000038, // 规则框位置(相对位置) LANE_ID = 0x07000002, // 车道号 TRAFFIC_LIGHT_COLOR_ONE = 0x07000106, // 信号灯 1 颜色 TRAFFIC_LIGHT_DIREC_ONE = 0x07000107, // 信号灯1方向 TRAFFIC_LIGHT_COLOR_TWO = 0x07000108, // 信号灯2颜色 TRAFFIC_LIGHT_DIREC_TWO = 0x07000109, // 信号灯2方向 TRAFFIC_LIGHT_COLOR_THREE = 0x07000110, // 信号灯3颜色 TRAFFIC_LIGHT_DIREC_THREE = 0x07000111, // 信号灯3方向 TRAFFIC_LIGHT_COLOR_FOUR = 0x07000112, // 信号灯4颜色 TRAFFIC_LIGHT_DIREC_FOUR = 0x07000113, // 信号灯4方向 VEHICLE_TYPE = 0x07000003, // 车辆类型 VEHICLE_TYPE_EXT = 0x07000406, // 车辆类型扩展(开始支持版本:SDC V500R019C50) VEHICLE_COLOR = 0x07000004, // 车辆颜色 VEHICLE_DIRECTION = 0x07000005, // 车辆行驶方向 VEHICLE_POS = 0x0B000005, // 车辆位置 (万分比,开始支持版本:SDC V500R019C30) VEHICLE_POS_ABS = 0x0B000020, // 车辆位置绝对坐标 VEHICLE_POS_COM = 0x0B000021, // 车辆位置相对坐标万分比 PLATE_TYPE = 0x07000006, // 车牌类型 PLATE_POS = 0x0B000007, // 车牌位置(开始支持版本:SDC V500R019C30) PLATE_POS_ABS = 0x0B000026, // 车牌位置绝对坐标 PLATE_POS_COM = 0x0B000027, // 车牌位置相对坐标万分比 PLATE_CHAR = 0x0A000008, // 车牌字符 PLATE_PIC = 0x0A000009, // 车牌抠图(开始支持版本:SDC V500R019C60) PLATE_BMP_BIT = 0x0A00000B, // 车牌二值图(开始支持版本:SDC 8.0.1) PLATE_BMP_BYTE = 0x0A00000C, // 车牌BMP图(开始支持版本:SDC 8.0.1) PLATE_CONFIDENCE = 0x07000061, // 车牌置信度(开始支持版本:SDC V500R019C60) PLATE_COLOR = 0x07000062, // 车牌颜色 PLATE_SNAPSHOT_TYPE = 0x07000066, // 车牌抓拍类型 VEHICLE_PIC = 0x0A000067, // 车辆特写图(开始支持版本:SDC V500R019C30) FACE_FEATURE_PIC = 0x0A000068, // 行人闯红灯人脸特写图(开始支持版本:SDC 8.0.1) PIC_SNAPSHOT_TIMEMS = 0x09000003, // 抓拍时间(单位ms)(开始支持版本:SDC V500R019C50) PIC_SNAPSHOT_TIME = 0x07000068, // 抓拍时间 PIC_SNAPSHOT_TZONE = 0x08000069, // 设备时区(单位ms 东区为+ 西区为-) DEVICE_ID = 0x0A000025, // 设备编号 ROID_ID = 0x0A000026, // 道路编号 DIR_ID = 0x0A000027, // 方向编号 DIR_INFO = 0x0A000028, // 方向信息 REGULATION_CODE = 0x0A000029, // 违章代码字符串(开始支持版本:SDC 8.0.1) LANE_DESC = 0x070000B2, // 车道描述(开始支持版本:SDC V500R019C30) LANE_DIR_DESC = 0x070000B3, // 车道方向描述(开始支持版本:SDC V500R019C30) CAR_DRV_DIR = 0x070000B6, // 车辆行驶方向描述(开始支持版本:SDC V500R019C30) RADER_CAR_DIR = 0x070000B7, // 雷达测速方向(开始支持版本:SDC V500R019C30) CUR_SNAP_INDEX = 0x070000B8, // 当前抓拍序列号(开始支持版本:SDC V500R019C30) ITS_COMBINE = 0x01000003, // 违章图片 是否开启合成 ITS_OSD_PIC_OFFSET = 0x06000037, // ITS 六合一卡口osd导致的车辆位置偏移量,正值表示叠加外侧上边缘, // 负值表示叠加外侧下边缘(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS = 0x070000A0, // 车流量统计参数(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_LANE_COUNT = 0x070000A1, // 车流量统计车道数量(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_LANE_INDEX = 0x070000A2, // 车流量统计当前车道(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_VEHICLE_COUNT = 0x070000A3, // 车辆计数(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_AVG_SPEED = 0x070000A4, // 平均速度(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_LANE_TIME_USED_RATIO = 0x070000A5, // 车道时间占有率(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_VEHICLE_DENSITY = 0x070000A6, // 车流密度(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_VEHICLE_HEAD_INTERVAL = 0x070000A7, // 车头时间间隔(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_VEHICLE_HEAD_SPACE_INTERVAL = 0x070000A8, // 车头间隔(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_CONGESTION_DEGREE = 0x070000A9, // 交通状态(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_VEHICLE_TYPE1_COUNT = 0x070000AA, // 大型车数量(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_VEHICLE_TYPE2_COUNT = 0x070000AB, // 中型车数量(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_VEHICLE_TYPE3_COUNT = 0x070000AC, // 小型车数量(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_QUEUE_LENGTH = 0x070000AD, // 排队长度(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_LANE_SPACE_USED_RATIO = 0x070000AE, // 车道空间占有率(开始支持版本:SDC V500R019C30) ITS_TRAFFIC_LEFT_VEHICLE_COUNT = 0x070000AF, // its左转车数量(开始支持版本:SDC V500R019C30) ITS_TRAFFIC_STRAIGHT_VHEICLE_COUNT = 0x070000B0, // its直行车数量(开始支持版本:SDC V500R019C30) ITS_TRAFFIC_RIGHT_VHEICLE_COUNT = 0x070000B1, // its右转车数量(开始支持版本:SDC V500R019C30) TRAFFIC_STATISTICS_CYCLE = 0x070000B9, // 车流量统计周期(开始支持版本:SDC V500R019C50) TRAFFIC_STATISTICS_NONMOTOR_COUNT = 0x070000C0, // 非机动车数量(开始支持版本:SDC 8.0.0) TRAFFIC_STATISTICS_PEDESTRIAN_COUNT = 0x070000C1, // 行人数量(开始支持版本:SDC 8.0.0) TRAFFIC_STATISTICS_TOTAL_VEHICLE_COUNT = 0x070000C2, // 断面流量(开始支持版本:SDC 8.0.0) VEHICLE_SPEED = 0x07000075, // 车辆速度 REGULATION_TYPE = 0x07000076, // 违章类型 MFR_MAIN_CALL = 0x06000025, // 主驾驶打电话(开始支持版本:SDC V500R019C30) MFR_MAIN_BELT = 0x06000026, // 主驾驶安全带(开始支持版本:SDC V500R019C30) MFR_VICE_EXIST = 0x06000027, // 是否有副驾驶(开始支持版本:SDC V500R019C30) MFR_VICE_BELT = 0x06000035, // 副驾驶安全带(开始支持版本:SDC V500R019C30) MFR_YEAR_LOG = 0x06000036, // 年检标(开始支持版本:SDC V500R019C30) MFR_MAIN_SUN_VISOR = 0x06000030, // 主驾驶遮阳板(开始支持版本:SDC V500R019C30) MFR_VICE_SUN_VISOR = 0x06000031, // 副驾驶遮阳板(开始支持版本:SDC V500R019C30) MFR_NAP_KIN_BOX = 0x06000032, // 纸巾盒(开始支持版本:SDC V500R019C30) MFR_CAR_PENDANT = 0x06000034, // 挂件(开始支持版本:SDC V500R019C30) VEHICLE_BODY_RECT = 0x0B000008, // 车身位置 NOMOTOR_BODY_RECT = 0x0B000009, // 非机动车车身位置 MOTOR_COLOR = 0X07000600, // 非机动车颜色 {黑(1),蓝(2),绿(3),白/灰(4),黄/橙/棕(5),红/粉/紫(6)} (开始支持版本:SDC 8.0.1) MOTOR_SUNSHADE = 0X07000601, // 是否有遮阳伞{否(1),是(2)} (开始支持版本:SDC 8.0.1) MOTOR_SUNSHADE_COLOR = 0X07000602, // 遮阳伞颜色 {黑(1),蓝(2),绿(3),白/灰(4),黄/橙/棕(5),红/粉/紫(6)} (开始支持版本:SDC 8.0.1) MOTOR_MOTOR_CARRY = 0X07000603, // 是否有携带物 {否(1),是(2)} (开始支持版本:SDC 8.0.1) MOTOR_LICENSE_PLATE = 0X07000604, // 是否有车牌{否(1),是(2)} (开始支持版本:SDC 8.0.1) CAR_PRE_BRAND = 0x0A000007, // 品牌字符 (大 众)(开始支持版本:SDC V500R019C60 ) CAR_SUB_BRAND = 0x0A000022, // 子款字符 (桑塔纳)(开始支持版本:SDC V500R019C60 ) CAR_YEAR_BRAND = 0x0A000024, // 年款字符 (2011)(开始支持版本:SDC V500R019C60 ) VHD_OBJ_ID = 0x09000006, // 机非人ID CAR_PRE_BRAND_INDEX = 0x06000028, // 品牌字符索引 (大 众)(开始支持版本:SDC V500R019C30 ) CAR_SUB_BRAND_INDEX = 0x06000029, // 子款字符索引 (桑塔纳)(开始支持版本:SDC V500R019C30 ) /// <summary> /// 设备数 /// </summary> DEV_CNT = 0x03000070, /// <summary> /// 通道号 /// </summary> CHAN_ID = 0x03000071, /// <summary> /// 人群密度检测算法人数 /// </summary> PEOPLE_NUM = 0X07000087, /// <summary> /// 人群密度检测算法返回框的地址 /// </summary> HEADSHOULDER_POS = 0X0B000088, /// <summary> /// 人群密度检测算法人群密度 /// </summary> AREARATIO = 0X07000089, /// <summary> /// 跟踪目标id (开始支持版本:SDC V500R019C30) /// </summary> TRACK_OBJECT = 0x07000028, /// <summary> /// 相机通道号(开始支持版本:SDC V500R019C30) /// </summary> CHANNEL_ID = 0x09000078 } }
定时器事件中处理捕获到的人脸数据(存为本地图片)
#region 处理人脸数据 private void Timer_Elapsed(object sender, ElapsedEventArgs e) { if (_concurrentQueue.Count == 0) { Console.WriteLine(@"暂无人脸图片"); return; } if (!_concurrentQueue.TryDequeue(out CaptureInfo face)) { Console.WriteLine(@"读取队列错误"); return; } if (face._dataFacePic != null && face._dataFacePic.Length > 0) { Console.WriteLine(@"人脸存储中"); Task.Run(async () => { var saveFaceFile = Path.Combine($"{AppDomain.CurrentDomain.BaseDirectory}", "jpg", $"face_{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.jpg"); await YuvHelper.Byte2Jpg(face._dataFacePic, saveFaceFile).ConfigureAwait(false); }); } if (face._dataFacePanorama != null && face._dataFacePanorama.Length > 0) { Console.WriteLine(@"全景图片存储中"); Task.Run(async () => { var savePanoramaFile = Path.Combine($"{AppDomain.CurrentDomain.BaseDirectory}", "jpg", $"Panorama_{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.jpg"); await YuvHelper.Byte2Jpg(face._dataFacePanorama, savePanoramaFile).ConfigureAwait(false); }); } } #endregion
在人脸捕获按钮事件中启动人脸捕获回调
#region 人脸捕获 private void ButtonFace_OnClick(object sender, RoutedEventArgs e) { if (0 == _ulIdentifyId) { HuaWeiSdkHelper.InitAndLogin("192.168.2.250", 6060, "ApiAdmin", "HuaWei123", out _ulIdentifyId, out string errMsg); if (0 == _ulIdentifyId) { MessageBox.Show(errMsg); return; } } var prpInfos = new PU_REAL_PLAY_INFO_S[1]; var clientInfo = new PU_REAL_PLAY_INFO_S { ulChannelId = 101, hPlayWnd = IntPtr.Zero, enProtocolType = PU_PROTOCOL_TYPE.PU_PROTOCOL_TYPE_TCP, enStreamType = PU_STREAM_TYPE.PU_VIDEO_MAIN_STREAM, enVideoType = PU_VIDEO_TYPE.PU_VIDEO_TYPE_META,//这里需要设置为视频类型为元数据 enMediaCryptoType = PU_MEDIA_CRYPTO_TYPE.PU_MEDIA_CRYPTO_NONE, enMediaCallbackType = PU_MEDIA_CALLBACK_TYPE.PU_MEDIA_CALLBACK_TYPE_META_FRAME,//回调方式为智能元数据 bKeepLive = true, szLocalIp = null, szReserved = new byte[32] }; clientInfo.szReserved[22] = 1;//szReserved[22]表示智能分析数据打包格式 0:XML,1:元数据 prpInfos[0] = clientInfo; var loginUserId = _ulIdentifyId; IntPtr pUsrData = (IntPtr)loginUserId; _fedRealPlayCallbackFaceCapture = FaceCaptureReaplayCallbackWithMetaFrame; var ulRealHandleCapture = HuaWeiSdkHelper.IVS_PU_RealPlay(_ulIdentifyId, prpInfos, _fedRealPlayCallbackFaceCapture, ref pUsrData); if (0 == ulRealHandleCapture) { MessageBox.Show(HuaWeiSdkHelper.GetLastErrorInfo()); return; } _timer.Enabled = true; } #region 人脸捕获数据回调 private static void FaceCaptureReaplayCallbackWithMetaFrame(IntPtr szBuffer, int lSize, IntPtr pUsrData) { var ptrstMetaTargetData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PU_META_DATA))); try { var bRet = HuaWeiSdkHelper.IVS_User_GetMetaData(szBuffer, lSize, LAYER_TWO_TYPE.TARGET, ref ptrstMetaTargetData); if (false == bRet) { return; } if ((IntPtr)0 == ptrstMetaTargetData) { return; } //将数据从非托管内存块封送到新分配的指定类型的托管对象 var pstMetaData = (PU_META_DATA)Marshal.PtrToStructure(ptrstMetaTargetData, typeof(PU_META_DATA)); //数据处理 if (0 == pstMetaData.usValidNumber) { return; } PU_UserData pstMetaUserData = new PU_UserData(); int nSizeofPuUserDataInC = Marshal.SizeOf(pstMetaUserData); byte[] dataFacePic = null;//人脸图片,如果捕获到人脸,会转成byte[]数组填充进来 byte[] dataFacePanorama = null;//检测到人脸的时候的全景图片 var faceFeature = new META_FACE_ATTRIBUTES();//附加的人脸上的数据 bool hasFaceFeature = false; int target = 0; for (int uIndex = 0; uIndex < pstMetaData.usValidNumber; ++uIndex) { IntPtr ptr2 = new IntPtr(pstMetaData.pstMetaUserData.ToInt32() + nSizeofPuUserDataInC * uIndex); pstMetaUserData = (PU_UserData)Marshal.PtrToStructure(ptr2, typeof(PU_UserData));//数据转成元用户数据结构 switch (pstMetaUserData.eType) { case LAYER_THREE_TYPE.TARGET_TYPE: target = pstMetaUserData.Union1.IntValue; break; case LAYER_THREE_TYPE.FACE_PIC://人脸抠图 dataFacePic = new byte[pstMetaUserData.Union1.stBinay.ulBinaryLenth]; //使用地址data来获取需要的内存块中的数据 Marshal.Copy(pstMetaUserData.Union1.stBinay.pBinaryData, dataFacePic, 0, (int)pstMetaUserData.Union1.stBinay.ulBinaryLenth); break; case LAYER_THREE_TYPE.FACE_PANORAMA://人脸全景 dataFacePanorama = new byte[pstMetaUserData.Union1.stBinay.ulBinaryLenth]; //使用地址data来获取需要的内存块中的数据 Marshal.Copy(pstMetaUserData.Union1.stBinay.pBinaryData, dataFacePanorama, 0, (int)pstMetaUserData.Union1.stBinay.ulBinaryLenth); break; case LAYER_THREE_TYPE.FACE_FEATURE://人脸属性 hasFaceFeature = true; faceFeature = pstMetaUserData.Union1.stFaceAttr; break; default: break; } } if ((int)Target.FaceCapture == target) { CaptureInfo info = new CaptureInfo { _dataFacePanorama = dataFacePanorama, _dataFacePic = dataFacePic, _faceFeature = faceFeature, _hasFaceFeature = hasFaceFeature }; _concurrentQueue.Enqueue(info);//加入到待处理队列中 } HuaWeiSdkHelper.IVS_User_FreeMetaData(out ptrstMetaTargetData);//释放数据占用空间 } catch (Exception e) { Console.WriteLine(e); throw; } finally { Marshal.FreeHGlobal(ptrstMetaTargetData);//释放内存 } } #endregion #endregion
在程序退出时,去释放资源
private void MainWindow_OnClosed(object sender, EventArgs e) { _isExit = true; if (_timer.Enabled) _timer.Enabled = false; if (_ulRealHandleId > 0) { HuaWeiSdkHelper.IVS_PU_StopRealPlay(_ulIdentifyId, _ulRealHandleId); } if (_ulIdentifyId > 0) { HuaWeiSdkHelper.IVS_PU_Logout(_ulIdentifyId); } HuaWeiSdkHelper.IVS_PU_Cleanup(); VideoFileStream.Close(); }
SDK把人脸抓拍注册成功后,摄像头本身带的有人脸识别算法,捕获到人脸后,会把数据回调给注册事件,注册事件中根据回调中给的人脸数据的内存地址取出数据,实例化成C#的数据结构,把图片转换成byte[]写入到队列里,定时处理队列时取出数据写成图片,即完成了摄像头人脸识别抓拍(有的摄像头带人脸比对算法,可直接进行人脸比对)。
出处:
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/14646479.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2021-04-12 08:58 jack_Meng 阅读(1267) 评论(0) 编辑 收藏 举报