赞
踩
本文参考博客,写得很好:
写本文的目的,也是快速复盘,所以没有很详细
保存视频流到本地可参考下一篇:基于海康SDK实现Python保存海康威视网络摄像头拍摄的视频
https://docs.conda.io/projects/miniconda/en/latest/
https://opencv.org/releases/
https://sourceforge.net/projects/swig/files/swigwin/
https://github.com/renatoGarcia/opencv-swig
https://www.boost.org/users/history/
https://open.hikvision.com/download/5cda567cf47ae80dd41a54b3?type=10
vc14 对应 VS2015
vc15 对应 VS2017
但实际上用VS2013好像也可以
用户变量
和系统变量
的路径配置为:D:\DeveloperTools\OpenCV\opencv\build\x64\vc14\bin
系统变量
的路径配置为:D:\DeveloperTools\SWIG\swigwin-3.0.12
D:\CYT\Desktop\HKSDK\
文件夹下D:\CYT\Desktop\HKSDK\
文件夹下创建HicVision_python_SDK
文件夹HKIPcamera.h
、HKIPcamera.cpp
、HKIPcamera.i
HKIPcamera.h
#include <opencv2/opencv.hpp>
using namespace cv;
void init(char* ip, char* usr, char* password);
Mat getframe();
void release();
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();
}
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-swig-master
中的lib\
目录下的opencv\
文件夹和opencv.i
复制到自己创建的HicVision_python_SDK\
文件夹下。boost_1_70_0
文件夹也一并复制到HicVision_python_SDK\
文件夹下。.\opencv\build\include\opencv2\
文件夹也复制到HicVision_python_SDK\
文件夹下。HicVision_python_SDK\
文件夹,执行如下命令:swig -IE:你的opencv安装路径 -python -c++ HKIPcamera.i
# 根据自己opencv安装路径来写,例如我的命令如下:
swig -IE:D:/DeveloperTools/OpenCV/opencv/build/include -python -c++ HKIPcamera.i
HKIPcamera_wrap.cxx
和 HKIPcamera.py
两个文件。CH-HCNetSDKV6.1.9.48_build20230410_win64
,如果是此本版以前的版本,可能会需要修改SDK中的plaympeg4.h
文件,可参考文章最开头给出的参考文章。VisualStudio
这个软件用的少,所以下面步骤详细点,这里用的是VisualStudio2015HicVision_python_SDK\
文件夹下VC++目录
->包含目录
中添加如下头文件路径:(请根据自己的实际安装位置填写)# 我的路径如下:
D:\DeveloperTools\Miniconda3\envs\HKSDK\include
D:\DeveloperTools\Miniconda3\envs\HKSDK\Lib\site-packages\numpy\core\include
D:\CYT\Desktop\HKSDK\HicVision_python_SDK\boost_1_70_0
D:\CYT\Desktop\HKSDK\CH-HCNetSDKV6.1.9.48_build20230410_win64\include
D:\DeveloperTools\OpenCV\opencv\build\include\opencv2
D:\DeveloperTools\OpenCV\opencv\build\include\opencv
D:\DeveloperTools\OpenCV\opencv\build\include
VC++目录
->库目录
中添加如下头文件路径:(请根据自己的实际安装位置填写)# 我的路径如下:
D:\DeveloperTools\Miniconda3\envs\HKSDK\libs
D:\CYT\Desktop\HKSDK\HicVision_python_SDK\boost_1_70_0\libs
D:\CYT\Desktop\HKSDK\CH-HCNetSDKV6.1.9.48_build20230410_win64\lib
D:\CYT\Desktop\HKSDK\CH-HCNetSDKV6.1.9.48_build20230410_win64\lib\HCNetSDKCom
D:\DeveloperTools\OpenCV\opencv\build\x64\vc14\lib
C/C++
->预处理器
->预处理器定义
中添加如下内容:WIN32
NDEBUG
_CONSULE
_CRT_SECURE_NO_WARNINGS
链接器
->输入
->附加依赖项
添加如下内容:opencv_world3416.lib # 这里一定要根据自己的opencv版本来写
HCNetSDK.lib
GdiPlus.lib
HCAlarm.lib
HCCore.lib
HCGeneralCfgMgr.lib
HCPreview.lib
PlayCtrl.lib
常规
->字符集
中选择使用多字节字符集
C/C++
->代码生成
中选择多线程DLL(/MD)
include\
文件夹下
D:\DeveloperTools\Miniconda3\envs\HKSDK\include\
object.h
中的第56行注释掉(以自己实际的行数为准)pyconfig.h
中的第317~319行注释掉(以自己实际的行数为准)完成以上步骤后,右键工程项目名选择生成
x64\Release\
文件夹下生成.dll
和.lib
文件现在,可以将之前python3.7中的 include\
文件夹下的object.h
和pyconfig.h
中注释掉的行数恢复原样(不恢复貌似也没问题)
将.\HKIPcamera\x64\Release\
下生成的HKIPcamera.dll
文件复制一份,并将文件名称和后缀名修改为_HKIPcamera.pyd
将海康SDK中的 .\CH-HCNetSDKV6.1.9.48_build20230410_win64\lib\
文件夹下的所有文件和文件夹拷贝到.\HKIPcamera\x64\Release\
文件夹下
将之前在.\HicVision_python_SDK
下生成的HKIPcamera.py
文件也拷贝到.\HKIPcamera\x64\Release\
文件夹下
将_HKIPcamera.pyd
文件,包含目录路径,添加到系统环境变量Path中,并将_HKIPcamera.pyd
所在文件夹目录
,也添加到系统环境变量目录中
HKIPcamera
包,没有报错,就是成功了import HKIPcamera
import time
import numpy as np
import matplotlib.pyplot as plt
import cv2
.\HKIPcamera\x64\Release\
中的文件和文件夹全部复制一份到.\CH-HCNetSDKV6.1.9.48_build20230410_win64\Demo\5_Python_Demo\1-预览取流解码Demo\lib\win\
目录下CH-HCNetSDKV6.1.9.48_build20230410_win64_20230905143859.zip\CH-HCNetSDKV6.1.9.48_build20230410_win64\Demo示例\5- Python开发示例\1-预览取流解码Demo\test_main.py
,测试可行到底了
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/753989
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。