1. 程式人生 > >Python呼叫海康SDK實時顯示網路攝像頭

Python呼叫海康SDK實時顯示網路攝像頭

一. 準備工作

1.下載海康SDK

海康SDK開發頁面根據自己需求下載相應的SDK版本,解壓之後主要用到標頭檔案庫檔案兩部分。

2.安裝配置OpenCV

OpenCV下載頁面下載相應的OpenCV版本Win pack檔案,注意OpenCV3目前只有VS2017和VS2015版本,使用VS2013的話會稍微麻煩一點,所以建議使用VS2017,OpenCV3.4.3,解壓後將D:\OpenCV3\opencv\build\x64\vc15\bin路徑新增到系統環境變數就可以使用了。

3.安裝配置swig

swig下載頁面下載swigwin-3.0.12安裝包,解壓到軟體目錄後,將D:\swigwin-3.0.12\

路徑新增到系統環境變數,重啟就可以使用了

4.下載boost庫

boost下載頁面下載boost庫檔案,解壓到軟體目錄即可使用

5.下載OpenCV-swig介面檔案

二. 使用SWIG編譯生成python和C++資料交換需要的.py和.cxx 檔案

1.HKIPcamera.h

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

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

2.HKIPcamera.cpp

#include
<opencv\cv.h>
#include <opencv\highgui.h> #include <opencv2\opencv.hpp> #include <iostream> #include <time.h> #include <cstdio> #include <cstring> #include <iostream> #include <windows.h> #include "HCNetSDK.h" #include "plaympeg4.h" #define USECOLOR 1
using namespace cv; using namespace std; //-------------------------------------------- int iPicNum = 0;//Set channel NO. LONG nPort = -1; HWND hWnd = NULL; CRITICAL_SECTION g_cs_frameList; list<Mat> g_frameList; LONG lUserID; NET_DVR_DEVICEINFO_V30 struDeviceInfo; HANDLE hThread; 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; for (row = 0; row < height; row++) { idx = row * widthStep; int rowptr = row * width; for (col = 0; col < width; col++) { tmp = (row / 2)*(width / 2) + (col / 2); Y = (unsigned int)inYv12[row*width + col]; U = (unsigned int)inYv12[width*height + width * height / 4 + tmp]; V = (unsigned int)inYv12[width*height + tmp]; outYuv[idx + col * 3] = Y; outYuv[idx + col * 3 + 1] = U; outYuv[idx + col * 3 + 2] = V; } } } //解碼回撥 視訊為YUV資料(YV12),音訊為PCM資料 void CALLBACK DecCBFun(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long 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 EnterCriticalSection(&g_cs_frameList); //g_frameList.push_back(pImg); g_frameList.push_back(cv::cvarrToMat(pImg)); LeaveCriticalSection(&g_cs_frameList); } ///實時流回調 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_Play(nPort, hWnd)) { 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); OutputDebugString(L"PlayM4_InputData failed \n"); } } 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; } } DWORD WINAPI ReadCamera(LPVOID 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); 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) { //HANDLE hThread; //LPDWORD threadID; //--------------------------------------- // 初始化 NET_DVR_Init(); //設定連線時間與重連時間 NET_DVR_SetConnectTime(2000, 1); NET_DVR_SetReconnect(10000, true); OpenCamera(ip, usr, password); InitializeCriticalSection(&g_cs_frameList); hThread = ::CreateThread(NULL, 0, ReadCamera, NULL, 0, 0); } Mat getframe() { Mat frame1; EnterCriticalSection(&g_cs_frameList); while (!g_frameList.size()) { LeaveCriticalSection(&g_cs_frameList); EnterCriticalSection(&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(); // 丟掉舊的幀 LeaveCriticalSection(&g_cs_frameList); return(frame1); } void release() { ::CloseHandle(hThread); NET_DVR_StopRealPlay(lRealPlayHandle); //關閉預覽 NET_DVR_Logout(lUserID); //登出使用者 NET_DVR_Cleanup(); }

3.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"

將之前下載的OpenCV介面檔案lib資料夾下的檔案與上面面三個原始檔放到同一資料夾中,在cmd中cd到該資料夾,輸入以下命令編譯生成HKIPcamera.pyHKIPvamera.cxx檔案:

//將其中的OpenCV路徑改成自己的
swig -ID:/opencv/build/include -python -c++ HKIPcamera.i  

三. 使用VS2017編譯生成動態連結庫

1. 建立空的動態連結庫工程