Linux Python利用海康相机C++SDK(丝滑取流)

众所周知,海康的相机SDK里面没有python模块,因此python不能调用,需要做转换,方法如下。

准备的包

  • OpenCV(用于源码编译,链接库的拷贝)
  • 海康SDK
  • Swig(桥梁,将C++SDk转化为python模块)
  • OpenCV-swig接口
  • code blocks(编译工具)
    我使用的OpenCV是3.4.8,系统是Ubuntu18.04

链接:
OpenCV下载地址
海康SDK下载地址
OpenCV-swig接口文件

@TOC

1.OpenCV编译与安装

mkdir build
cd build
# 精简版(推荐)
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
# cuda版(不建议使用,会有几个头文件问题)
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D WITH_CUBLAS=1 -D WITH_NVCUVID=on -D CUDA_GENERATION=Auto ..
make -j16
sudo make install

2.配置环境


新建配置文件

sudo gedit /etc/ld.so.conf.d/opencv.conf  

输入内容

/usr/local/lib   

使配置生效

sudo ldconfig


配置bash

sudo gedit /etc/bash.bashrc  

输入内容

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH

3.root下生效

进入root

sudo su

生效

source /etc/bash.bashrc  

退出root,更新

exit
sudo updatedb

4.测试OpenCV是否安装成功

新建以下几个测试文件

i. Display.cpp

#include <stdio.h>  
#include <opencv2/opencv.hpp>  
#include <iostream>  
using namespace cv;  

int main()  
{  
    Mat image = imread("1.jpg", 1);  
    if (image.empty())  
    {  
        std::cout << "Image file is empty." << std::endl;  
        return -1;  
    }  

    namedWindow("Test");  
    imshow("Test", image);  

    waitKey(0);  

    return 0;  
}  

ii. CMakeLists.txt

cmake_minimum_required(VERSION 2.8)  
project( Display )  
find_package( OpenCV REQUIRED )  
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( Display Display.cpp )  
target_link_libraries( Display ${OpenCV_LIBS} ) 

运行测试

cmake .  //注意cmake后面是一个空格加一个黑点,这其实是cmake CMakeLists.txt的省略写法  
make  
./Display lena.jpg  //任意图片,命名lena.jpg  

若报错

CMake Warning at /home/e/OpenCV/cmake/OpenCVConfig.cmake:166 (message):
  Found OpenCV Windows Pack but it has no binaries compatible with your
  configuration.

  You should manually point CMake variable OpenCV_DIR to your build of OpenCV
  library.
Call Stack (most recent call first):
  CMakeLists.txt:10 (find_package)

CMake Error at CMakeLists.txt:10 (find_package):
  Found package configuration file:

    /home/e/OpenCV/cmake/OpenCVConfig.cmake

  but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
  NOT FOUND.

则在find_package前添加

include_directories( ${OpenCV_INCLUDE_DIRS} )
# 或者
set( OpenCV_FOUND 1 )
# 或者
set(OpenCV_DIR /home/***/opencv-2.4.13/build)

若报错

/usr/bin/ld: cannot find -lopencv_dep_cudart 

则继续添加

set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)  

5.安装Code::Blocks和swig

sudo apt-get install codeblocks
sudo apt-get install swig

6.使用swig生成接口文件

OpenCV-swig接口文件lib文件夹下的文件,与下面三个源文件放到一起。

//HKIPcamera.cpp

#include <opencv2/opencv.hpp>
#include <iostream>
#include <time.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include "HCNetSDK.h"
#include "LinuxPlayM4.h"
#include <unistd.h>
#include <pthread.h>

#define USECOLOR 1
#define WINAPI

using namespace cv;
using namespace std;

//--------------------------------------------
int iPicNum = 0;//Set channel NO.
LONG nPort = -1;
HWND hWnd = NULL;
pthread_mutex_t g_cs_frameList;
list<Mat> g_frameList;
LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
LONG lRealPlayHandle = -1;

void yv12toYUV(char *outYuv, char *inYv12, int width, int height, int widthStep)
{
int col, row;
unsigned int Y, U, V;
int tmp;
int idx;

//printf("widthStep=%d.\n",widthStep);

for (row = 0; row<height; row++)
{
idx = row * widthStep;
int rowptr = row*width;

for (col = 0; col<width; col++)
{
//int colhalf=col>>1;
tmp = (row / 2)*(width / 2) + (col / 2);
//         if((row==1)&&( col>=1400 &&col<=1600))
//         {
//          printf("col=%d,row=%d,width=%d,tmp=%d.\n",col,row,width,tmp);
//          printf("row*width+col=%d,width*height+width*height/4+tmp=%d,width*height+tmp=%d.\n",row*width+col,width*height+width*height/4+tmp,width*height+tmp);
//         }
Y = (unsigned int)inYv12[row*width + col];
U = (unsigned int)inYv12[width*height + width*height / 4 + tmp];
V = (unsigned int)inYv12[width*height + tmp];
//         if ((col==200))
//         {
//         printf("col=%d,row=%d,width=%d,tmp=%d.\n",col,row,width,tmp);
//         printf("width*height+width*height/4+tmp=%d.\n",width*height+width*height/4+tmp);
//         return ;
//         }
if ((idx + col * 3 + 2)> (1200 * widthStep))
{
//printf("row * widthStep=%d,idx+col*3+2=%d.\n",1200 * widthStep,idx+col*3+2);
}
outYuv[idx + col * 3] = Y;
outYuv[idx + col * 3 + 1] = U;
outYuv[idx + col * 3 + 2] = V;
}
}
//printf("col=%d,row=%d.\n",col,row);
}

//解码回调 视频为YUV数据(YV12),音频为PCM数据
void CALLBACK DecCBFun(int nPort, char * pBuf, int nSize, FRAME_INFO * pFrameInfo, void * nReserved1, int nReserved2)
{
long lFrameType = pFrameInfo->nType;

if (lFrameType == T_YV12)
{
#if USECOLOR
//int start = clock();
static IplImage* pImgYCrCb = cvCreateImage(cvSize(pFrameInfo->nWidth, pFrameInfo->nHeight), 8, 3);//???????????????????Y????????????
yv12toYUV(pImgYCrCb->imageData, pBuf, pFrameInfo->nWidth, pFrameInfo->nHeight, pImgYCrCb->widthStep);//????????????????RGB????????
static IplImage* pImg = cvCreateImage(cvSize(pFrameInfo->nWidth, pFrameInfo->nHeight), 8, 3);
cvCvtColor(pImgYCrCb, pImg, CV_YCrCb2RGB);
//int end = clock();
#else
static IplImage* pImg = cvCreateImage(cvSize(pFrameInfo->nWidth, pFrameInfo->nHeight), 8, 1);
memcpy(pImg->imageData, pBuf, pFrameInfo->nWidth*pFrameInfo->nHeight);
#endif
//printf("%d\n",end-start);

//Mat frametemp(pImg), frame;

//frametemp.copyTo(frame);
//      cvShowImage("IPCamera",pImg);
//      cvWaitKey(1);
pthread_mutex_lock(&g_cs_frameList);
Mat mat=cvarrToMat(pImg);
g_frameList.push_back(mat);
pthread_mutex_unlock(&g_cs_frameList);

#if USECOLOR
//      cvReleaseImage(&pImgYCrCb);
//      cvReleaseImage(&pImg);
#else
/*cvReleaseImage(&pImg);*/
#endif
//此时是YV12格式的视频数据,保存在pBuf中,可以fwrite(pBuf,nSize,1,Videofile);
//fwrite(pBuf,nSize,1,fp);
}
/***************
else if (lFrameType ==T_AUDIO16)
{
//此时是音频数据,数据保存在pBuf中,可以fwrite(pBuf,nSize,1,Audiofile);

}
else
{

}
*******************/

}

///实时流回调
void CALLBACK fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser)
{
DWORD dRet;
switch (dwDataType)
{
case NET_DVR_SYSHEAD:    //系统头
if (!PlayM4_GetPort(&nPort)) //获取播放库未使用的通道号
{
break;
}
if (dwBufSize > 0)
{
if (!PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 1024 * 1024))
{
dRet = PlayM4_GetLastError(nPort);
break;
}
//设置解码回调函数 只解码不显示
if (!PlayM4_SetDecCallBack(nPort, DecCBFun))
{
dRet = PlayM4_GetLastError(nPort);
break;
}

//设置解码回调函数 解码且显示
//if (!PlayM4_SetDecCallBackEx(nPort,DecCBFun,NULL,NULL))
//{
//  dRet=PlayM4_GetLastError(nPort);
//  break;
//}

//打开视频解码
if (!PlayM4_Play(nPort, hWnd))
{
dRet = PlayM4_GetLastError(nPort);
break;
}

//打开音频解码, 需要码流是复合流
//          if (!PlayM4_PlaySound(nPort))
//          {
//              dRet=PlayM4_GetLastError(nPort);
//              break;
//          }     
}
break;

case NET_DVR_STREAMDATA:   //码流数据
if (dwBufSize > 0 && nPort != -1)
{
BOOL inData = PlayM4_InputData(nPort, pBuffer, dwBufSize);
while (!inData)
{
sleep(10);
inData = PlayM4_InputData(nPort, pBuffer, dwBufSize);
cout << (L"PlayM4_InputData failed \n") << endl;
}
}
break;
}
}

void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
{
char tempbuf[256] = { 0 };
switch (dwType)
{
case EXCEPTION_RECONNECT:    //预览时重连
printf("----------reconnect--------%d\n", time(NULL));
break;
default:
break;
}
}

bool OpenCamera(char* ip, char* usr, char* password)
{
lUserID = NET_DVR_Login_V30(ip, 8000, usr, password, &struDeviceInfo);
if (lUserID == 0)
{
cout << "Log in success!" << endl;
return TRUE;
}
else
{
printf("Login error, %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return FALSE;
}
}
void * ReadCamera(void* IpParameter)
{
//---------------------------------------
//设置异常消息回调函数
NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL);

//cvNamedWindow("Mywindow", 0);
//cvNamedWindow("IPCamera", 0);

//HWND  h = (HWND)cvGetWindowHandle("Mywindow");
//h = cvNamedWindow("IPCamera");
//---------------------------------------
//启动预览并设置回调数据流 
NET_DVR_CLIENTINFO ClientInfo;
ClientInfo.lChannel = 1;        //Channel number 设备通道号
ClientInfo.hPlayWnd = NULL;     //窗口为空,设备SDK不解码只取流
ClientInfo.lLinkMode = 1;       //Main Stream
ClientInfo.sMultiCastIP = NULL;

LONG lRealPlayHandle;
lRealPlayHandle = NET_DVR_RealPlay_V30(lUserID, &ClientInfo, fRealDataCallBack, NULL, TRUE);
if (lRealPlayHandle<0)
{
printf("NET_DVR_RealPlay_V30 failed! Error number: %d\n", NET_DVR_GetLastError());
//return -1;
}
else
cout << "码流回调成功!" << endl;
sleep(-1);
//fclose(fp);
//---------------------------------------
//关闭预览
if (!NET_DVR_StopRealPlay(lRealPlayHandle))
{
printf("NET_DVR_StopRealPlay error! Error number: %d\n", NET_DVR_GetLastError());
return 0;
}
//注销用户
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
//return 0;
}

void init(char* ip, char* usr, char* password){
pthread_t hThread;
cout << "IP:" << ip << "    UserName:" << usr << "    PassWord:" << password << endl;
NET_DVR_Init();
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
OpenCamera(ip, usr, password);
pthread_mutex_init(&g_cs_frameList, NULL);
//hThread = ::CreateThread(NULL, 0, ReadCamera, NULL, 0, 0);
pthread_create(&hThread, NULL, ReadCamera, NULL);
}

Mat getframe(){
Mat frame1;
pthread_mutex_lock(&g_cs_frameList);
while (!g_frameList.size()){
pthread_mutex_unlock(&g_cs_frameList);
pthread_mutex_lock(&g_cs_frameList);
}
list<Mat>::iterator it;
it = g_frameList.end();
it--;
Mat dbgframe = (*(it));
(*g_frameList.begin()).copyTo(frame1);
frame1 = dbgframe;
g_frameList.pop_front();
//imshow("camera", frame1);
//waitKey(1);

g_frameList.clear();
pthread_mutex_unlock(&g_cs_frameList);
return(frame1);
}

void release(){
//close(hThread);
NET_DVR_StopRealPlay(lRealPlayHandle);
//注销用户
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
}

//HKIPcamera.h

//HKIPcamera.h
#include <opencv2/opencv.hpp>
using namespace cv;

void init(char* ip, char* usr, char* password);
Mat getframe();
void release();

// HKIPcamera.i

/*  Example of wrapping a C function that takes a C double array as input using
 *  numpy typemaps for SWIG. */
%module HKIPcamera
%include <opencv/mat.i>
%cv_mat__instantiate_defaults
%header %{
    /*  Includes the header in the wrapper code */
    #include "HKIPcamera.h"
%}

%include "HKIPcamera.h"

命令行输入下面命令生成HKIPcamera_wrap.cxx文件(cd到源文件文件夹下,opencv路径改成自己的路径)

swig -I/home/skzeng/opencv-3.4.8/modules/core/include/ -python -c++ HKIPcamera.i

7.编译动态链接库

codeblocks建立动态链接库工程,右键工程-Add files,将HKIPcameracpp,HKIPcamera.h,HKIPcamera_wrap.cxx加入到工程。
/因为codeblocks新建的工程和,上面新建的三个文件不在一个文件夹。
把上面三个文件copy到codeblocks新建的动态链接库工程下面,然后使用Add files就可以有如下效果了。
/

然后我们在codeblocks已经建好的工程下右键,选择release——Build options——Linker settings——linker libraries中添加opencv的所有so文件(为防万一,全部导入,当然也可以导入自己用的部分),如下图所示(so文件在安装好的opencv目录中的release或者buid中的lib目录下):
在这里插入图片描述
添加海康SDK中的所有so文件CH_HCNetSDK_Linux64/lib 以及CH_HCNetSDK_Linux64/lib/HCNetSDKCom下的所有so文件

然后在project – build option – search directories – Compiler中添加/usr/include/python2.7、/usr/include/boost、/usr/local/include/ 、/usr/local/include/opencv、/usr/local/include/opencv2三个OpenCV头文件路径以及海康SDK头文件路径CH_HCNetSDK_Linux64/incCn

project – build option – search directories – Linker中添加 /usr/local/lib路径

project – build option – Compiler settings – Other compiler options 添加 -fPIC


主界面 – Settings – Compiler settings – linker settings – linker libraries 添加 /usr/lib/x86_64-linux-gnu/libpthread.a 右边 other linker options 添加 -lpthread,如下图所示:

在这里插入图片描述
将HKIPcamera动态链接库地址添加进系统路径
在/etc/ld.so.conf文件结尾添加网络sdk库的路径,如/XXX和/XXX/HCNetSDKCom/,保存之后,然后执行ldconfig

sudo gedit /etc/ld.so.conf
# 末尾添加
CH_HCNetSDK_Linux64/lib  #根据自己的so保存路径改变
CH_HCNetSDK_Linux64/lib/HCNetSDKCom
# 保存退出
sudo ldconfig

最后右键工程build或者rebuild,就会在工程目录下HKIPcamera/bin/Release或者Debug生成liblibHKIPcamera.so动态链接库文件

  • 若在LinuxPlayM4.h行报错:
    则在海康SDK里面搜索该文件,然后copy到code blocks新建的动态链接项目下面即可.

8.测试生成的链接库能否使用

将liblibHKIPcamera.so改名为_HKIPcamera.so,与步骤2.3生成的HKIPcamera.py文件一同放到test文件夹,新建test.py文件,输入:

import HKIPcamera
import time
import numpy as np
import matplotlib.pyplot as plt
import cv2

ip = str('192.168.0.0')  # 摄像头IP地址,要和本机IP在同一局域网
name = str('admin')       # 管理员用户名
pw = str('123456')        # 管理员密码
HKIPcamera.init(ip, name, pw)
# HKIPcamera.getfram()
for i in range(100):
    t = time.time()
    fram = HKIPcamera.getframe()
    t2 = time.time()
    cv2.imshow('123', np.array(fram))
    cv2.waitKey(1)
    print(t2-t)
    time.sleep(0.1)
HKIPcamera.release()

至此取流成功~

参考链接

https://blog.csdn.net/ustczhang/article/details/79030715?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7Eessearch%7Evector-3.fixedcolumn&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7Eessearch%7Evector-3.fixedcolumn

OpenCV编译出错则参考这篇

https://blog.csdn.net/weixin_41869700/article/details/104259008

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注