系统相关
首页 > 系统相关> > 在Ubuntu下使用opencv调用海康威视的网络摄像头

在Ubuntu下使用opencv调用海康威视的网络摄像头

作者:互联网

经过很多天的配置,终于能在工程中调用海康威视的网络摄像头了,为了后人能快速上手,我整理内容如下:


在本文中我的电脑是使用的Ubuntu16.04 64位,使用工具是QT5.7.1,opencv2.4.9.
一、根据电脑的型号下载海康威视的SDK包
网址:http://www.hikvision.com/cn/download_61.html
我的笔记本时linux64位的,所以下载的 ————设备网络SDK_Linux64
二、 用下面的方法之一配置SDK
如果HCNetSDKCom目录以及libhcnetsdk.so、libhpr.so、libHCCore文件和可执行文件在同一级目录下,则使用同级目录下的库文件;
如果不在同一级目录下,则需要将以上文件的目录加载到动态库搜索路径中,设置的方式有以下几种:
1.    将网络SDK各动态库路径加入到LD_LIBRARY_PATH环境变量
    1.在终端输入:export  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/XXX:/XXX/HCNetSDKCom      只在当前终端起作用
    2. 修改~/.bashrc或~/.bash_profile,最后一行添加 export  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/XXX:/XXX/HCNetSDKCom,保存之后,使用source  .bashrc执行该文件 ,当前用户生效
    3. 修改/etc/profile,添加内容如第2条,同样保存之后使用source执行该文件  所有用户生效
 

2.在/etc/ld.so.conf文件结尾添加网络sdk库的路径,如/XXX和/XXX/HCNetSDKCom/,保存之后,然后执行ldconfig。

三. 在Ubuntu16.04 的qt中新建一个工程
1.  在工程的 ×.pro文件中添加以下内容:

INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_calib3d.so
/usr/local/lib/libopencv_ml.so.2.4.9
/usr/local/lib/libopencv_calib3d.so.2.4
/usr/local/lib/libopencv_nonfree.so
/usr/local/lib/libopencv_calib3d.so.2.4.9
/usr/local/lib/libopencv_nonfree.so.2.4
/usr/local/lib/libopencv_contrib.so
/usr/local/lib/libopencv_nonfree.so.2.4.9
/usr/local/lib/libopencv_contrib.so.2.4
/usr/local/lib/libopencv_objdetect.so
/usr/local/lib/libopencv_contrib.so.2.4.9
/usr/local/lib/libopencv_objdetect.so.2.4
/usr/local/lib/libopencv_core.so
/usr/local/lib/libopencv_objdetect.so.2.4.9
/usr/local/lib/libopencv_core.so.2.4
/usr/local/lib/libopencv_ocl.so
/usr/local/lib/libopencv_core.so.2.4.9
/usr/local/lib/libopencv_ocl.so.2.4
/usr/local/lib/libopencv_features2d.so
/usr/local/lib/libopencv_ocl.so.2.4.9
/usr/local/lib/libopencv_features2d.so.2.4
/usr/local/lib/libopencv_photo.so
/usr/local/lib/libopencv_features2d.so.2.4.9
/usr/local/lib/libopencv_photo.so.2.4
/usr/local/lib/libopencv_flann.so
/usr/local/lib/libopencv_photo.so.2.4.9
/usr/local/lib/libopencv_flann.so.2.4
/usr/local/lib/libopencv_stitching.so
/usr/local/lib/libopencv_flann.so.2.4.9
/usr/local/lib/libopencv_stitching.so.2.4
/usr/local/lib/libopencv_gpu.so
/usr/local/lib/libopencv_stitching.so.2.4.9
/usr/local/lib/libopencv_gpu.so.2.4
/usr/local/lib/libopencv_superres.so
/usr/local/lib/libopencv_gpu.so.2.4.9
/usr/local/lib/libopencv_superres.so.2.4
/usr/local/lib/libopencv_highgui.so
/usr/local/lib/libopencv_superres.so.2.4.9
/usr/local/lib/libopencv_highgui.so.2.4
/usr/local/lib/libopencv_highgui.so.2.4.9
/usr/local/lib/libopencv_video.so
/usr/local/lib/libopencv_imgproc.so
/usr/local/lib/libopencv_video.so.2.4
/usr/local/lib/libopencv_imgproc.so.2.4
/usr/local/lib/libopencv_video.so.2.4.9
/usr/local/lib/libopencv_imgproc.so.2.4.9
/usr/local/lib/libopencv_videostab.so
/usr/local/lib/libopencv_legacy.so
/usr/local/lib/libopencv_videostab.so.2.4
/usr/local/lib/libopencv_legacy.so.2.4
/usr/local/lib/libopencv_videostab.so.2.4.9
/usr/local/lib/libopencv_legacy.so.2.4.9
/usr/local/lib/libopencv_ml.so

INCLUDEPATH += /home/ding/opencvprojects/CH_HCNetSDK_V5.2.7.4build20170606_Linux64/incCn

LIBS += /home/ding/opencv_projects/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/lib/libhcnetsdk.so</span>
/home/ding/opencv_projects/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/lib/libHCCore.so</span>
/home/ding/opencv_projects/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/lib/libhpr.so</span>
/home/ding/opencv_projects/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/lib/libPlayCtrl.so</span>
/home/ding/opencv_projects/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/lib/libSuperRender.so</span>
/home/ding/opencv_projects/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/lib/libAudioRender.so2. 在main.cpp函数中添加以下内容:


 
  1. #include "opencv2/opencv.hpp"
  2. #include <QDir>
  3. #include <fstream>
  4. #include <unistd.h>
  5. //#include "auto_entercs.h"
  6. #include "HCNetSDK.h"
  7. #include "PlayM4.h"
  8. #include "LinuxPlayM4.h"
  9. #define HPR_ERROR -1
  10. #define HPR_OK 0
  11. #define USECOLOR 0
  12. static cv::Mat dst;
  13. HWND h = NULL;
  14. LONG nPort= -1;
  15. LONG lUserID;
  16. pthread_mutex_t mutex;
  17. std:: list<cv::Mat> g_frameList;
  18. FILE *g_pFile = NULL;
  19. void CALLBACK PsDataCallBack(LONG lRealHandle, DWORD dwDataType,BYTE *pPacketBuffer,DWORD nPacketSize, void* pUser)
  20. {
  21. if (dwDataType == NET_DVR_SYSHEAD)
  22. {
  23. //写入头数据
  24. g_pFile = fopen( "/home/lds/source/ps.dat", "wb");
  25. if (g_pFile == NULL)
  26. {
  27. printf( "CreateFileHead fail\n");
  28. return;
  29. }
  30. //写入头数据
  31. fwrite(pPacketBuffer, sizeof( unsigned char), nPacketSize, g_pFile);
  32. printf( "write head len=%d\n", nPacketSize);
  33. }
  34. else
  35. {
  36. if(g_pFile != NULL)
  37. {
  38. fwrite(pPacketBuffer, sizeof( unsigned char), nPacketSize, g_pFile);
  39. printf( "write data len=%d\n", nPacketSize);
  40. }
  41. }
  42. }
  43. //void CALLBACK DecCBFun(LONG nPort, char *pBuf, LONG nSize, FRAME_INFO *pFrameInfo, LONG nReserved1, LONG nReserved2)
  44. void CALLBACK DecCBFun(LONG nPort, char *pBuf, LONG nSize, FRAME_INFO *pFrameInfo, void* nReserved1, LONG nReserved2)
  45. {
  46. long lFrameType = pFrameInfo->nType;
  47. if (lFrameType == T_YV12)
  48. {
  49. //cv::Mat dst(pFrameInfo->nHeight, pFrameInfo->nWidth,
  50. // CV_8UC3); // 8UC3表示8bit uchar无符号类型,3通道值
  51. dst.create(pFrameInfo->nHeight, pFrameInfo->nWidth,
  52. CV_8UC3);
  53. cv::Mat src(pFrameInfo->nHeight + pFrameInfo->nHeight / 2, pFrameInfo->nWidth, CV_8UC1, (uchar *)pBuf);
  54. cv::cvtColor(src, dst, CV_YUV2BGR_YV12);
  55. pthread_mutex_lock(&mutex);
  56. g_frameList.push_back(dst);
  57. pthread_mutex_unlock(&mutex);
  58. }
  59. usleep( 1000);
  60. //cv::Mat src(pFrameInfo->nHeight + pFrameInfo->nHeight / 2, pFrameInfo->nWidth, CV_8UC1, (uchar *)pBuf);
  61. //cv::cvtColor(src, dst, CV_YUV2BGR_YV12);
  62. //cv::imshow("bgr", dst);
  63. //pthread_mutex_lock(&mutex);
  64. //g_frameList.push_back(dst);
  65. //pthread_mutex_unlock(&mutex);
  66. //vw << dst;
  67. //cv::waitKey(10);
  68. }
  69. void CALLBACK g_RealDataCallBack_V30(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize,void* dwUser)
  70. {
  71. /*
  72. if (dwDataType == 1)
  73. {
  74. PlayM4_GetPort(&nPort);
  75. PlayM4_SetStreamOpenMode(nPort, STREAME_REALTIME);
  76. PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 1024 * 1024);
  77. PlayM4_SetDecCallBackEx(nPort, DecCBFun, NULL, NULL);
  78. PlayM4_Play(nPort, h);
  79. }
  80. else
  81. {
  82. BOOL inData = PlayM4_InputData(nPort, pBuffer, dwBufSize);
  83. }*/
  84. DWORD dRet;
  85. switch (dwDataType)
  86. {
  87. case NET_DVR_SYSHEAD: //系统头
  88. if (!PlayM4_GetPort(&nPort)) //获取播放库未使用的通道号
  89. {
  90. break;
  91. }
  92. if (dwBufSize > 0) {
  93. if (!PlayM4_SetStreamOpenMode(nPort, STREAME_REALTIME)) {
  94. dRet = PlayM4_GetLastError(nPort);
  95. break;
  96. }
  97. if (!PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 1024 * 1024)) {
  98. dRet = PlayM4_GetLastError(nPort);
  99. break;
  100. }
  101. //设置解码回调函数 只解码不显示
  102. // if (!PlayM4_SetDecCallBack(nPort, DecCBFun)) {
  103. // dRet = PlayM4_GetLastError(nPort);
  104. // break;
  105. // }
  106. //设置解码回调函数 解码且显示
  107. if (!PlayM4_SetDecCallBackEx(nPort, DecCBFun, NULL, NULL))
  108. {
  109. dRet = PlayM4_GetLastError(nPort);
  110. break;
  111. }
  112. //打开视频解码
  113. if (!PlayM4_Play(nPort, h))
  114. {
  115. dRet = PlayM4_GetLastError(nPort);
  116. break;
  117. }
  118. //打开音频解码, 需要码流是复合流
  119. if (!PlayM4_PlaySound(nPort)) {
  120. dRet = PlayM4_GetLastError(nPort);
  121. break;
  122. }
  123. }
  124. break;
  125. //usleep(500);
  126. case NET_DVR_STREAMDATA: //码流数据
  127. if (dwBufSize > 0 && nPort != -1) {
  128. BOOL inData = PlayM4_InputData(nPort, pBuffer, dwBufSize);
  129. while (!inData) {
  130. sleep( 100);
  131. inData = PlayM4_InputData(nPort, pBuffer, dwBufSize);
  132. std:: cerr << "PlayM4_InputData failed \n" << std:: endl;
  133. }
  134. }
  135. break;
  136. }
  137. }
  138. void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
  139. {
  140. char tempbuf[ 256] = { 0};
  141. std:: cout << "EXCEPTION_RECONNECT = " << EXCEPTION_RECONNECT << std:: endl;
  142. switch(dwType)
  143. {
  144. case EXCEPTION_RECONNECT: //预览时重连
  145. printf( "pyd----------reconnect--------%d\n", time( NULL));
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. void *RunIPCameraInfo(void *)
  152. {
  153. char IP[] = "192.168.**.***"; //海康威视网络摄像头的ip
  154. char UName[] = "****"; //海康威视网络摄像头的用户名
  155. char PSW[] = "*****"; //海康威视网络摄像头的密码
  156. NET_DVR_Init();
  157. NET_DVR_SetConnectTime( 2000, 1);
  158. NET_DVR_SetReconnect( 1000, true);
  159. NET_DVR_SetLogToFile( 3, "./sdkLog");
  160. NET_DVR_DEVICEINFO_V30 struDeviceInfo = { 0};
  161. NET_DVR_SetRecvTimeOut( 5000);
  162. lUserID = NET_DVR_Login_V30(IP, 8000, UName, PSW, &struDeviceInfo);
  163. NET_DVR_SetExceptionCallBack_V30( 0, NULL, g_ExceptionCallBack, NULL);
  164. long lRealPlayHandle;
  165. NET_DVR_CLIENTINFO ClientInfo = { 0};
  166. ClientInfo.lChannel = 1;
  167. ClientInfo.lLinkMode = 0;
  168. ClientInfo.hPlayWnd = 0;
  169. ClientInfo.sMultiCastIP = NULL;
  170. //lRealPlayHandle = NET_DVR_RealPlay_V30(lUserID, &ClientInfo, PsDataCallBack, NULL, 0);
  171. lRealPlayHandle = NET_DVR_RealPlay_V30(lUserID, &ClientInfo, g_RealDataCallBack_V30, NULL, 0);
  172. //NET_DVR_SaveRealData(lRealPlayHandle, "/home/lds/source/yuntai.mp4");
  173. if (lRealPlayHandle < 0)
  174. {
  175. printf( "pyd1---NET_DVR_RealPlay_V30 error\n");
  176. }
  177. sleep( -1);
  178. NET_DVR_Cleanup();
  179. }
  180. int main(int argc, char *argv[])
  181. {
  182. pthread_t getframe;
  183. pthread_mutex_init(&mutex, NULL);
  184. int ret;
  185. ret = pthread_create(&getframe, NULL, RunIPCameraInfo, NULL);
  186. if(ret!= 0)
  187. {
  188. printf( "Create pthread error!\n");
  189. }
  190. cv::Mat image;
  191. while( 1)
  192. {
  193. pthread_mutex_lock(&mutex);
  194. if(g_frameList.size())
  195. {
  196. std:: list<cv::Mat>::iterator it;
  197. it = g_frameList.end();
  198. it--;
  199. image = (*(it));
  200. if (!image.empty())
  201. {
  202. imshow( "frame from camera",image);
  203. cv::waitKey( 1);
  204. }
  205. g_frameList.pop_front();
  206. }
  207. g_frameList.clear(); // 丢掉旧的帧
  208. pthread_mutex_unlock(&mutex);
  209. }
  210. return 0;
  211. }


标签:lib,威视,libopencv,opencv,so.2,so,usr,Ubuntu,local
来源: https://blog.csdn.net/xujianjun229/article/details/118583394