python 调用海康linux下demo的可执行程序,设置回调,实现人脸识别 以及实现抓拍功能

python使用os.system调用海康
 
CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/consoleDemo下的demo生成的可执行文件,
生成文件为sdkTest python调用代码如下:
def callAndRunCpp():###这个是人脸对比的python接口,
    restartTime = 0
    while True:##人脸相机对比接口有时候会意外退出,因此退出后我就重启
        restartTime += 1
        print("启动c++可执行文件来获取相机的信息,%d次启用",restartTime)
        os.system(
            r'"/home/caobin/chike/chike/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/consoleDemo/linux64/lib/sdkTest" '
            r'"192.168.1.64" 8000 "admin" "guoji123" 3')
        time.sleep(2)
 
  
#####抓图调用语句
 
  
 os.system(
            r'"/home/caobin/chike/chike/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/consoleDemo/linux64/lib/sdkTest" '
            r'"192.168.1.64" 8000 "admin" "guoji123" 4')

 
  
//C++方面,将main函数带上参数,然后根据传进来的参数对相机进行操作,对C++consoleMain的修改如下
//这里我主要实现的是人脸对比和抓拍图片两个功能,其中人脸对比结果直接存到了数据库,抓拍图片则是以时间格式直接保存的图片
 /*
* Copyright(C) 2010,Hikvision Digital Technology Co., Ltd 

* File   name��consoleMain.cpp
* Discription��
* Version    ��1.0
* Author     ��panyadong
* Create Date��2010_3_25
* Modification History��
*/


#ifndef __APPLE__




#include
#include
#include
#include "GetStream.h"
#include "public.h"
#include "ConfigParams.h"
#include "Alarm.h"
#include "CapPicture.h"
#include "playback.h"
#include "Voice.h"
#include "tool.h"


using namespace std;


int main(int argc,char* argv[])
{
//argv[]
//argv[0] 相机IP地址
//argv[1] 用户名
//argv[2] 密码
//argv[3] 功能选择
    NET_DVR_Init();
    Demo_SDK_Version();
    NET_DVR_SetLogToFile(3, "./sdkLog");
    
    //Login device
    NET_DVR_DEVICEINFO_V30 struDeviceInfo = {0};
    for(int i = 0;i<=argc;i++){cout<<"第"<//    printf("打印出来端口参数:%d\n",atoi(argv[2]));
LONG lUserID = NET_DVR_Login_V30(argv[1],atoi(argv[2]),argv[3],argv[4],&struDeviceInfo);
    if (lUserID < 0)
    {
        printf("pyd---Login error, %d\n", NET_DVR_GetLastError());
        NET_DVR_Cleanup();
        return HPR_ERROR;
    }
    switch (atoi(argv[5]))
    {
    case 1:
        Demo_GetStream_V30(lUserID); //Get stream.
        break;
    case 2:
        Demo_ConfigParams(lUserID);  //Setting params.
        break;
    case 3://主要修改的选项
    printf("启动监听!!!!!!!!!!!\n");
        Demo_Alarm(argc,argv);         //Alarm & listen.
        break;
    case 4://主要修改的选项
        Demo_Capture(argc,argv);
        break;
    case 5:
        Demo_PlayBack((int)lUserID);     //record & playback
        break;
    case 6:
        Demo_Voice();
        break;
    case 7:
        Demo_SDK_Ability();
        break;
case 8:
Demo_DVRIPByResolveSvr();
break;
    default:
    printf("跳出switch  case语句!\n");
        break;
    }
/*
    while ('q' != cUserChoose)
    {
        printf("\n");
        printf("Input 1, Test GetStream\n");
        printf("      2, Test Configure params\n");
        printf("      3, Test Alarm\n");
        printf("      4, Test Capture Picture\n");
        printf("      5, Test play back\n");
        printf("      6, Test Voice\n");
        printf("      7, Test SDK ability\n");
        printf("      8, Test tool interface\n");
/*
        printf("      7, Test Matrix decode\n");
        printf("      8, Test PTZ\n");
        printf("      9, Test Format\n");
        printf("      0, Test Update\n");
        printf("      a, Test Serial trans\n");
        printf("      b, Test Configure Params\n");
        printf("      c, Test VCA && IVMS\n");
        *//*
        printf("      q, Quit.\n");
        printf("Input:");


        cin>>cUserChoose;
        switch (cUserChoose)
        {
        case '1':
            Demo_GetStream_V30(lUserID); //Get stream.
            break;
        case '2':
            Demo_ConfigParams(lUserID);  //Setting params.
            break;
        case '3':
            Demo_Alarm();         //Alarm & listen.
            break;
        case '4':
            Demo_Capture();
            break;
        case '5':
            Demo_PlayBack((int)lUserID);     //record & playback
            break;
        case '6':
            Demo_Voice();
            break;
        case '7':
            Demo_SDK_Ability();
            break;
case '8':
Demo_DVRIPByResolveSvr();
break;
        default:
            break;
        }
    }
*/


    //logout
    NET_DVR_Logout_V30(lUserID);
    NET_DVR_Cleanup();
    return 0;
}


#endif
 
  


//以下代码是CPP实现人脸对比,并将捕获到的人脸对比信息存入到数据库的操作,修改的是Alarm.cpp文件

/*
 * Copyright(C) 2010,Hikvision Digital Technology Co., Ltd 
 * 
 * File   name��Alarm.cpp
 * Discription��
 * Version    ��1.0
 * Author     ��panyd
 * Create Date��2010_3_25
 * Modification History��
 */


#include
#include
#include
#include
#include
#include
#include
#include "public.h"
#include "Alarm.h"
using namespace std;
#ifdef _WIN32
#elif defined(__linux__) || defined(__APPLE__)
#include    
#include "/usr/include/mysql/mysql.h"
#endif
//时间解释宏定义
#define GET_YEAR(_time_)      (((_time_)>>26) + 2000)
#define GET_MONTH(_time_)     (((_time_)>>22) & 15)
#define GET_DAY(_time_)       (((_time_)>>17) & 31)
#define GET_HOUR(_time_)      (((_time_)>>12) & 31)
#define GET_MINUTE(_time_)    (((_time_)>>6)  & 63)
#define GET_SECOND(_time_)    (((_time_)>>0)  & 63)
// 代码转换操作类
class CodeConverter {
private:
iconv_t cd;
public:
// 构造
CodeConverter(const char *from_charset,const char *to_charset) {
cd = iconv_open(to_charset,from_charset);
}


// 析构
~CodeConverter() {
iconv_close(cd);
}


// 转换输出
int convert(char *inbuf,int inlen,char *outbuf,int outlen) {
char **pin = &inbuf;
char **pout = &outbuf;


memset(outbuf,0,outlen);
return iconv(cd,pin,(size_t *)&inlen,pout,(size_t *)&outlen);
}
};
void CALLBACK MessageCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser)
{
int i;
NET_DVR_ALARMINFO_V30 struAlarmInfo;
memcpy(&struAlarmInfo, pAlarmInfo, sizeof(NET_DVR_ALARMINFO_V30));
printf("callback have been run!!!!!!!!");
printf("lCommand is %d, alarm type is %d\n", lCommand, struAlarmInfo.dwAlarmType);
switch(lCommand) 
{       
case COMM_ALARM_V30:
{
switch (struAlarmInfo.dwAlarmType)
{
case 3: //�ƶ���ⱨ��
for (i=0; i<16; i++)   //#define MAX_CHANNUM   16  //���ͨ����
{
if (struAlarmInfo.byChannel[i] == 1)
{
printf("Motion detection %d\n", i+1);
}
}       
break;
default:
break;
}
}
break;
//2018.3.1 add by caobin
case COMM_SNAP_MATCH_ALARM:
{




// cin.imbue(locale::classic()); ///使用经典的C的locale从标准流中读取数据
// cout.imbue(locale("zh_CN"));


printf("人脸识别执行!!!!!!!!!!\n");
NET_VCA_FACESNAP_MATCH_ALARM struFaceMatchAlarm = {0};
memcpy(&struFaceMatchAlarm,pAlarmInfo,sizeof(NET_VCA_FACESNAP_MATCH_ALARM));
if (struFaceMatchAlarm.fSimilarity < 0.3){
printf("识别到的人脸匹配度太低,已忽略!!\n");
break;}
printf("\n\n\n\n\n\n\n\n\n");
// locale langLocale("");
// cout.imbue(langLocale);
// cout<// CodeConverter("gb2312","utf-8");
NET_DVR_TIME struAbsTime = {0};
struAbsTime.dwYear = GET_YEAR(struFaceMatchAlarm.struSnapInfo.dwAbsTime);
struAbsTime.dwMonth = GET_MONTH(struFaceMatchAlarm.struSnapInfo.dwAbsTime);
struAbsTime.dwDay = GET_DAY(struFaceMatchAlarm.struSnapInfo.dwAbsTime);
struAbsTime.dwHour = GET_HOUR(struFaceMatchAlarm.struSnapInfo.dwAbsTime);
struAbsTime.dwMinute = GET_MINUTE(struFaceMatchAlarm.struSnapInfo.dwAbsTime);
struAbsTime.dwSecond = GET_SECOND(struFaceMatchAlarm.struSnapInfo.dwAbsTime);
// char arrTime[255];
// sprintf(arrTime,"%d-%d-%d %d:%d:%d",struAbsTime.dwYear,struAbsTime.dwMonth,struAbsTime.dwDay,struAbsTime.dwHour,struAbsTime.dwMinute,struAbsTime.dwSecond);
// string strTime(arrTime);
printf("已经识别到人脸了!获取信息!\n");
BYTE sex = struFaceMatchAlarm.struBlackListInfo.struBlackListInfo.struAttribute.bySex;
char strName[32];
memcpy(strName,struFaceMatchAlarm.struBlackListInfo.struBlackListInfo.struAttribute.byName,
sizeof(struFaceMatchAlarm.struBlackListInfo.struBlackListInfo.struAttribute.byName));
strName[sizeof(struFaceMatchAlarm.struBlackListInfo.struBlackListInfo.struAttribute.byName)]= '\0';
CodeConverter cc = CodeConverter("gb2312","utf-8");
char NameOut[255];
cc.convert(strName,strlen(strName),NameOut,255);
// printf("++++++++++++++++++++++++++++++++++\n");
// cout << "姓名输出:";
// cout  << NameOut << endl;
char arrSql[255];
if(sex == NULL){
sprintf(arrSql,"insert into ck_service_info(customer_name, gender,dtime) value('%s', '男','%04d-%02d-%02d %02d:%02d:%02d');",NameOut,struAbsTime.dwYear,struAbsTime.dwMonth,struAbsTime.dwDay,struAbsTime.dwHour,struAbsTime.dwMinute,struAbsTime.dwSecond);
}else{
sprintf(arrSql,"insert into ck_service_info(customer_name, gender,dtime) value('%s', '女','%04d-%02d-%02d %02d:%02d:%02d');",NameOut,struAbsTime.dwYear,struAbsTime.dwMonth,struAbsTime.dwDay,struAbsTime.dwHour,struAbsTime.dwMinute,struAbsTime.dwSecond);
}
printf("人脸信息获取完成,准备开始写入数据库!!\n");
string sql(arrSql);
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql,"localhost","root","gj123","chike",3306,NULL,0);
mysql_query(&mysql,"SET NAMES UTF8");
mysql_query(&mysql, sql.c_str());
        mysql_close(&mysql);
        printf("已经完成数据库的存储等操作,C++输出流无法输出!!!\n");
cout<< "相似度:"<< struFaceMatchAlarm.fSimilarity << endl;
cout << "名单注册ID号1:"<< struFaceMatchAlarm.struBlackListInfo.struBlackListInfo.dwRegisterID< char strID[32];
memcpy(strID,struFaceMatchAlarm.struBlackListInfo.struBlackListInfo.struAttribute.byCertificateNumber,
sizeof(struFaceMatchAlarm.struBlackListInfo.struBlackListInfo.struAttribute.byCertificateNumber));
cout<< "证件号:"< cout<< "---------------------- \n\n"< printf("人脸信息处理完成!!\n");
// cout.imbue(locale("zh_CN.UTF-8"));
}
break;


default:
break;
}
}




//Alarm Test
int Demo_Alarm(int argc,char* argv[])
{
//     if (Demo_AlarmListen() == HPR_ERROR)
//     {
//         return HPR_ERROR;
//     }
if (Demo_AlarmFortify(argc,argv) == HPR_ERROR)
{
return HPR_ERROR;
}


    return HPR_OK;
}


//Alarm listen
int Demo_AlarmListen()
{
    BOOL iRet;
    
    //Init
    iRet = NET_DVR_Init();
    if (!iRet)
    {
        printf("   pyd---Alarm. NET_DVR_Init fail!\n");
        return HPR_ERROR;
    }
//open
int iHandle = NET_DVR_StartListen_V30("0.0.0.0", 7200, MessageCallback, NULL);
if (iHandle < 0)
{
printf("   pyd---Alarm. NET_DVR_StartListen_V30 fail!%d\n", NET_DVR_GetLastError());
return HPR_ERROR;
}
    //close
    iRet = NET_DVR_StopListen_V30(iHandle);
    if (!iRet)
    {
        printf("   pyd---Alarm. NET_DVR_StopListen fail!%d\n", NET_DVR_GetLastError());
        return HPR_ERROR;
    }


    //clean up
    NET_DVR_Cleanup();


    return HPR_ERROR;
}


// Fortify ����
int Demo_AlarmFortify(int argc,char* argv[])
{
LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
lUserID = NET_DVR_Login_V30(argv[1],atoi(argv[2]),argv[3],argv[4],&struDeviceInfo);
// lUserID = NET_DVR_Login_V30("192.168.1.64", 8000, "admin", "guoji123", &struDeviceInfo);
if (lUserID < 0)
{
printf("Login error, %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup(); 
return HPR_ERROR;
}


//���ñ����ص�����
cout<< "设置回调函数!!!" << endl;
NET_DVR_SetDVRMessageCallBack_V30(MessageCallback, NULL);
cout << "回调设置完成!!!"<< endl;
//����
LONG lHandle;
lHandle = NET_DVR_SetupAlarmChan_V30(lUserID);
if (lHandle < 0)
{
printf("NET_DVR_SetupAlarmChan_V30 error, %d\n", NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup(); 
return HPR_ERROR;
}


#ifdef _WIN32
    Sleep(5000);  //millisecond
#elif  defined(__linux__) || defined(__APPLE__)
    sleep(500);   //second
#endif


//���������ϴ�ͨ��
if (!NET_DVR_CloseAlarmChan_V30(lHandle))
{
printf("NET_DVR_CloseAlarmChan_V30 error, %d\n", NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup(); 
return HPR_ERROR;
}


//ע���û�
NET_DVR_Logout(lUserID);
//�ͷ�SDK��Դ
NET_DVR_Cleanup();
return HPR_OK;

}



///以下代码是抓图的代码,修改的文件是:CapPicture.cpp

/*
* Copyright(C) 2010,Hikvision Digital Technology Co., Ltd 
* 
* File   name��CapPicture.cpp
* Discription��
* Version    ��1.0
* Author     ��panyd
* Create Date��2010_3_25
* Modification History��
*/

#include "public.h"
#include "CapPicture.h"
#include 
#include 
#include 
#include 

/*******************************************************************
      Function:   Demo_Capture
   Description:   Capture picture.
     Parameter:   (IN)   none 
        Return:   0--success��-1--fail.   
**********************************************************************/
int Demo_Capture(int argc,char* argv[])
{
    NET_DVR_Init();
    long lUserID;
    //login
    NET_DVR_DEVICEINFO_V30 struDeviceInfo;
    lUserID = NET_DVR_Login_V30(argv[1],atoi(argv[2]),argv[3],argv[4],&struDeviceInfo);
//    lUserID = NET_DVR_Login_V30("172.4.1.162", 8000, "admin", "12345", &struDeviceInfo);
    if (lUserID < 0)
    {
        printf("pyd1---Login error, %d\n", NET_DVR_GetLastError());
        return HPR_ERROR;
    }

    //
    NET_DVR_JPEGPARA strPicPara = {0};
    strPicPara.wPicQuality = 0;
    strPicPara.wPicSize = 0;
    int iRet;
    printf("抓图\n");
    time_t timep;
    time(&timep);
    char tmp[64];
    strftime(tmp,sizeof(tmp),"./pic/%Y-%m-%d %H:%M:%S.jpeg",localtime(&timep));
    printf("获取到的图片名称:%s",tmp);
    for(int i = 0;i<20;i++){//考虑到抓图有一定的失败率,所以在不成功的时候反复抓拍图片
        iRet = NET_DVR_CaptureJPEGPicture(lUserID, struDeviceInfo.byStartChan, &strPicPara,tmp);
        if (!iRet)
        {
           printf("pyd1---NET_DVR_CaptureJPEGPicture error, %d\n", NET_DVR_GetLastError());
           if(i == 19){
               return HPR_ERROR;
           }
        }else{
            break;
        }
    }

    printf("抓图成功!!\n");

    //logout
    NET_DVR_Logout_V30(lUserID);
    NET_DVR_Cleanup();

    return HPR_OK;

}
 
  
 
  
C++文件修改完后,编译即可

你可能感兴趣的:(海康相机)