Real time human flow monitoring -- initial experience of Hikvision sdk

Posted by camoconnell.com on Thu, 14 Nov 2019 15:26:40 +0100

This article is mainly about the exploration process of the blogger's using the Haikang SDK for the statistics of human flow. Here is a brief record.

There are about two ways to query documents and realize traffic statistics: alarm or monitor,

I chose the monitoring mode here. Net? DVR? Startlisten? V30 is the interface to start monitoring. It can receive the alarm information uploaded by the device and support multithreading.

 

We need to write a callback function to accept data (process business logic),

 

Net DVR alarm is an alarm information structure. Comm alarm PDC data is needed here. It is actually a macro to determine the type of data to be received. The corresponding structure here is net DVR PDC alram info. Please refer to the document for specific parameters. Here I have a general understanding of SDK calls. So start.

First, create a personal traffic demo, import the necessary header files of the SDK and the corresponding resource files.

 

(ps: many DLLs here are not needed)

 

Direct code:

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include "HCNetSDK.h"
 4 using namespace std;
 5 
 6 void CALLBACK MessageCallback(LONG lCommand, NET_DVR_ALARMER* pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void*pUser)
 7 {    
 8 
 9     cout << "Entering callback" << endl;//Make sure to set the callback address and port on the device side before entering the callback. Otherwise, you will not be able to enter the callback
10     NET_DVR_PDC_ALRAM_INFO struPdcALramInfo;//Data structure
11     memcpy(&struPdcALramInfo, pAlarmInfo, sizeof(NET_DVR_PDC_ALRAM_INFO));
12     switch (lCommand)//Message type 
13     {
14         cout << lCommand << endl;
15         case COMM_ALARM_PDC:
16         {    
17             if (struPdcALramInfo.byMode == 0)
18             {
19                 //Test display data 
20                 cout << "Current time:" << struPdcALramInfo.uStatModeParam.struStatFrame.dwRelativeTime << endl;
21                 cout << "Number of people in:" << struPdcALramInfo.dwEnterNum << endl;
22                 cout << "Number of people leaving" << struPdcALramInfo.dwLeaveNum << endl;
23                 break;
24             }
25             
26         }    
27         default:break;        
28     }
29 }
30 void main() {
31     
32     NET_DVR_Init();// Initialization    
33     NET_DVR_SetConnectTime(2000, 1);//Set connection time and reconnection time        
34     NET_DVR_SetReconnect(10000, true);
35     LONG lUserID;// Registration equipment
36     NET_DVR_DEVICEINFO_V30 struDeviceInfo;
37     char ip[32] = "192.168.x.64";//The IP address of the device should be in the same network segment as that of the computer
38     char user[32] = "xxx";//Login name
39     char userPass[32] = "xxx";//I don't need to say more about the code 
40 
41     lUserID = NET_DVR_Login_V30(ip, 8000, user, userPass, &struDeviceInfo);//The port number here is 8000
42     if (lUserID < 0)
43     {
44         printf("Login error, %d\n", NET_DVR_GetLastError());//If the login fails, there will be a reason here. You can view the document and apply the medicine to the case
45         NET_DVR_Cleanup();
46         return;
47     }
48     else
49     {
50         cout<<"Login success lUserID:"<< lUserID <<endl;
51     }
52 
53     char localIP[16] = "192.168.x.60";//For local Ip setting callback 
54     
55     NET_DVR_NETCFG_V30 myNETCFG;//Configure alarm host and port
56     memcpy(&(myNETCFG.struAlarmHostIpAddr.sIpV4), localIP, sizeof(myNETCFG.struAlarmHostIpAddr.sIpV4));//Memory Copy
57     cout << myNETCFG.struAlarmHostIpAddr.sIpV4 << endl;//Negligible
58     myNETCFG.wAlarmHostIpPort =8000;
59     BOOL isSuccess = NET_DVR_SetDVRMessageCallBack_V30(MessageCallback, &myNETCFG);//Set alarm callback function
60     if (isSuccess)
61     {
62         cout << "Set callback successfully" << endl;
63     }    
64     LONG lHandle;//Enable monitoring
65     lHandle = NET_DVR_StartListen_V30(NULL, 8000, MessageCallback, NULL);
66     if (lHandle < 0)
67     {
68         printf("NET_DVR_StartListen_V30 error, %d\n", NET_DVR_GetLastError());
69         NET_DVR_Logout(lUserID);
70         NET_DVR_Cleanup();
71         return;
72     }
73     Sleep(5000);//This is for testing, so writing a sleep card main thread can also be understood as the listening time
74     if (!NET_DVR_StopListen_V30(lHandle))//End monitoring
75     {
76         printf("NET_DVR_StopListen_V30 error, %d\n", NET_DVR_GetLastError());
77         NET_DVR_Logout(lUserID);
78         NET_DVR_Cleanup();
79         return;
80     }
81     NET_DVR_Logout(lUserID);//Logout user
82     NET_DVR_Cleanup();    //Release SDK resources
83     return;
84 }

 

 

 

Topics: Windows SDK network