How to perform face recognition using Dahua NVR from VB.NET

Viewed 89

Has anyone used VB.NET to get face recognition data from a Dahua NVR using VB.NET?

I am facing two problems that I just can't figure out.

I can connect to the NVR and set up a callback for video using

        bDeviceInitialized = CLIENT_Init(AddressOf DeviceDisconnected, 0)
        Dim lSDKVersion As Long
        lSDKVersion = CLIENT_GetSDKVersion()
        console.writeline( "SDK: " + lSDKVersion.ToString.Substring(0, 1) + "." + lSDKVersion.ToString.Substring(1, 2) + " " + lSDKVersion.ToString.Substring(3) )

        fAnalyzer = AddressOf AnalyzerDataCallBack

        'Set reconnect callback
        CLIENT_SetAutoReconnect(AddressOf DeviceReconnected, 0)

        ' Set device connection timeout And trial times.
        ' Optional operation
        Dim nWaitTime As Integer = 5000 ' Timeout Is 5 seconds.
        Dim nTryTimes As Integer = 3    ' If timeout, it will try to log in three times.
        CLIENT_SetConnectTime(nWaitTime, nTryTimes)
        'A wait is required
        System.Threading.Thread.Sleep(1000)

        netInLoginWithHighLevelSecurity.dwSize = Marshal.SizeOf(netInLoginWithHighLevelSecurity)
        Array.Copy(System.Text.Encoding.Default.GetBytes(My.Settings.DeviceIP), netInLoginWithHighLevelSecurity.szIP, My.Settings.DeviceIP.Length)
        netInLoginWithHighLevelSecurity.nPort = CInt(My.Settings.DevicePort)
        Array.Copy(System.Text.Encoding.Default.GetBytes(My.Settings.DeviceUserId), netInLoginWithHighLevelSecurity.szUserName, My.Settings.DeviceUserId.Length)
        Array.Copy(System.Text.Encoding.Default.GetBytes(My.Settings.DevicePassword), netInLoginWithHighLevelSecurity.szPassword, My.Settings.DevicePassword.Length)
        netInLoginWithHighLevelSecurity.emSpecCap = EM_LOGIN_SPAC_CAP_TYPE.EM_LOGIN_SPEC_CAP_TCP

        netOutLoginWithHighLevelSecurity.stuDeviceInfo = devInfo
        netOutLoginWithHighLevelSecurity.dwSize = Marshal.SizeOf(netOutLoginWithHighLevelSecurity)
        'Create a pointer for the structure
        'pNetOutLoginWithHighLevelSecurity = Marshal.AllocHGlobal(Marshal.SizeOf(netOutLoginWithHighLevelSecurity))

        hLoginId = CLIENT_LoginWithHighLevelSecurity(netInLoginWithHighLevelSecurity, netOutLoginWithHighLevelSecurity)

        ' Get the serial number of the NVR
        Dim i As Integer = 0
        Dim strSerial As String = ""
        While i < 64 And netOutLoginWithHighLevelSecurity.stuDeviceInfo.sSerialNumber(i) <> 0
            strSerial &= Chr(netOutLoginWithHighLevelSecurity.stuDeviceInfo.sSerialNumber(i))
            i += 1
        End While

        ' Enable cameras
        Dim dwUser As Int64 = 0
        Dim oReserved As IntPtr = 0
        Dim nChannel As Int16 =0
        Dim iNeedPicture As Int32 = 1
        ' pbChannel1 is a PictureBox
        lMonitorChannel(nChannel) = CLIENT_RealPlayEx(hLoginId, nChannel, pbChannel1.Handle, EM_REAL_PLAY_TYPE.EM_REAL_PLAY_REALPLAY)
        lAlarmFaceDetection(nChannel) = CLIENT_RealLoadPictureEx(hLoginId, nChannel, EVENT_IVS_ALL, iNeedPicture, fAnalyzer, Nothing, Nothing)

This works fine and I can see the video from channel 1.

The problems are in the analyzer callback which is defined as

Public Delegate Sub AnalyzerDataCallBackDelegate(lAnalyzerHandle As Int64, dwAlarmType As UInt32, AlarmInfo As IntPtr, pBuffer As IntPtr, dwBufferSize As UInt32, dwUser As Int64, nSequence As Int16, Reserved As IntPtr)

Public Shared Sub AnalyzerDataCallBack(ByVal lAnalyzerHandle As Int64, ByVal dwAlarmType As Int32, ByVal AlarmInfo As IntPtr,
                                       ByVal pBuffer As IntPtr, ByVal dwBufferSize As Int32, ByVal dwUser As Int64, ByVal nSequence As Int16,
                                       ByVal Reserved As IntPtr)

Firstly, I am not getting any event other than motion detect and face recognition, even though I subscribed to EVENT_IVS_ALL.

Secondly, I am not getting complete face recognition information.

I copied from the unmanaged buffer into a (complex) structure.

  structFaceRecognitionInfo = CType(Marshal.PtrToStructure(AlarmInfo, GetType(DEV_EVENT_FACERECOGNITION_INFO)), DEV_EVENT_FACERECOGNITION_INFO)

But I don't get complete information. Only the first few elements are filled with the rest have garbage or zeros.

I tried copying the buffer into a byte array to inspect the data during debug and see that it is indeed filled with zeros.

      Dim lenBuffer As Int32 = Marshal.SizeOf(Of DEV_EVENT_FACERECOGNITION_INFO)
      ReDim bData(lenBuffer)
      Dim gchBuffer As GCHandle = GCHandle.Alloc(bData, GCHandleType.Pinned)
      Marshal.Copy(AlarmInfo, bData, 0, lenBuffer)
      gchBuffer.Free()

I've been stuck at this point for a week.

0 Answers
Related