海康摄像头
SDK百度云:
包含目錄: D:\opencv\build\include D:\opencv\build\include\opencv D:\opencv\build\include\opencv2 D:\CH-HCNetSDK\include庫目錄: D:\opencv\build\x86\vc12\lib D:\CH-HCNetSDK\庫文件 D:\CH-HCNetSDK\庫文件\HCNetSDKCom D:\CH-HCNetSDK\libfacedetect鏈接器->附加依賴項 opencv_calib3d2410.lib opencv_contrib2410.lib opencv_core2410.lib opencv_features2d2410.lib opencv_flann2410.lib opencv_gpu2410.lib opencv_highgui2410.lib opencv_imgproc2410.lib opencv_legacy2410.lib opencv_ml2410.lib opencv_nonfree2410.lib opencv_objdetect2410.lib opencv_ocl2410.lib opencv_photo2410.lib opencv_stitching2410.lib opencv_superres2410.lib opencv_ts2410.lib opencv_video2410.lib opencv_videostab2410.lib opencv_calib3d2410d.lib opencv_contrib2410d.lib opencv_core2410d.lib opencv_features2d2410d.lib opencv_flann2410d.lib opencv_gpu2410d.lib opencv_highgui2410d.lib opencv_imgproc2410d.lib opencv_legacy2410d.lib opencv_ml2410d.lib opencv_nonfree2410d.lib opencv_objdetect2410d.lib opencv_ocl2410d.lib opencv_photo2410d.lib opencv_stitching2410d.lib opencv_superres2410d.lib opencv_ts2410d.lib opencv_video2410d.lib opencv_videostab2410d.libGdiPlus.lib HCCore.lib HCNetSDK.LIB PlayCtrl.lib HCAlarm.lib HCGeneralCfgMgr.lib HCPreview.lib MSVCRTD.lib libfacedetect-x64.lib libfacedetect.lib代碼:
#include <cstdlib> #include <cstring> #include <iostream> #include "Windows.h" #include "HCNetSDK.h" #include "plaympeg4.h" #include <opencv2\opencv.hpp> #include <time.h> #include <string> #include <sstream> #include "facedetect-dll.h" #pragma comment(lib,"libfacedetect.lib") #define DETECT_BUFFER_SIZE 0xC004using namespace std; using namespace cv;LONG nPort = -1; #define NUM_FRAME 300 volatile int gbHandling = 3;void detectAndDisplay(Mat frame) {//float detect_time ;//double t;Mat gray;cvtColor(frame, gray, CV_BGR2GRAY);int * pResults = NULL;//如果你在多個線程中調用函數,請為每個線程創建一個緩沖區unsigned char* pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);if(!pBuffer){fprintf(stderr, "Can not alloc buffer.\n");//return -1;}//pResults = facedetect_frontal(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,// 1.2f, 2, 48);//printf("%d faces detected.\n", (pResults ? *pResults : 0));//重復運行////print the detection results//for (int i = 0; i < (pResults ? *pResults : 0); i++)//{// short * p = ((short*)(pResults + 1)) + 6 * i;// int x = p[0];// int y = p[1];// int w = p[2];// int h = p[3];// int neighbors = p[4];// printf("face_rect=[%d, %d, %d, %d], neighbors=%d\n", x, y, w, h, neighbors);// Point left(x, y);// Point right(x + w, y + h);// rectangle(frame, left, right, Scalar(230, 255, 0), 4);/////////////////////////////////////////// frontal face detection 正面面部檢測// it's fast, but cannot detect side view faces它的速度很快,但不能檢測側視面////////////////////////////////////////////!!! The input image must be a gray one (single-channel)輸入圖像必須是灰色(單通道)//!!! DO NOT RELEASE pResults !!!不要釋放pResultspResults = facedetect_frontal(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,1.2f, 2, 48);printf("%d faces detected.\n", (pResults ? *pResults : 0));//print the detection results輸出檢測結果for(int i = 0; i < (pResults ? *pResults : 0); i++){short * p = ((short*)(pResults+1))+6*i;int x = p[0];int y = p[1];int w = p[2];int h = p[3];int neighbors = p[4];printf("face_rect=[%d, %d, %d, %d], neighbors=%d\n", x,y,w,h,neighbors);Point left(x, y);Point right(x + w, y + h);rectangle(frame, left, right, Scalar(230, 255, 0), 4);}///////////////////////////////////////////////// frontal face detection designed for video surveillance正面面部檢測設計用于視頻監控//// it can detect faces with bad illumination。它可以檢測具有不良照明的面部////////////////////////////////////////////////!!! The input image must be a gray one (single-channel)////!!! DO NOT RELEASE pResults !!!//pResults = facedetect_frontal_surveillance(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,// 1.2f, 2, 48);//printf("%d faces detected.\n", (pResults ? *pResults : 0));////print the detection results//for(int i = 0; i < (pResults ? *pResults : 0); i++)//{// short * p = ((short*)(pResults+1))+6*i;// int x = p[0];// int y = p[1];// int w = p[2];// int h = p[3];// int neighbors = p[4];// printf("face_rect=[%d, %d, %d, %d], neighbors=%d\n", x,y,w,h,neighbors);// Point left(x, y);// Point right(x + w, y + h);// rectangle(frame, left, right, Scalar(230, 255, 0), 4);//}///////////////////////////////////////////////// multiview face detection //// it can detect side view faces, but slower than facedetect_frontal().////////////////////////////////////////////////!!! The input image must be a gray one (single-channel)////!!! DO NOT RELEASE pResults !!!//pResults = facedetect_multiview(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,// 1.2f, 5, 48);//printf("%d faces detected.\n", (pResults ? *pResults : 0));////print the detection results//for(int i = 0; i < (pResults ? *pResults : 0); i++)//{// short * p = ((short*)(pResults+1))+6*i;// int x = p[0];// int y = p[1];// int w = p[2];// int h = p[3];// int neighbors = p[4];// int angle = p[5];// printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle);// Point left(x, y);// Point right(x + w, y + h);// rectangle(frame, left, right, Scalar(230, 255, 0), 4);//}///////////////////////////////////////////////// reinforced multiview face detection 加強多視圖面部檢測//// it can detect side view faces, better but slower than facedetect_multiview().////////////////////////////////////////////////!!! The input image must be a gray one (single-channel)////!!! DO NOT RELEASE pResults !!!//pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,// 1.2f, 5, 48);// printf("%d faces detected.\n", (pResults ? *pResults : 0));////print the detection results//for(int i = 0; i < (pResults ? *pResults : 0); i++)//{// short * p = ((short*)(pResults+1))+6*i;// int x = p[0];// int y = p[1];// int w = p[2];// int h = p[3];// int neighbors = p[4];// int angle = p[5];// printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle);// Point left(x, y);// Point right(x + w, y + h);// rectangle(frame, left, right, Scalar(230, 255, 0), 4);//}// //release the buffer// free(pBuffer);imshow("ss", frame); }//解碼回調 視頻為YUV數據(YV12),音頻為PCM數據 void CALLBACK DecCBFun(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long nReserved2) {if (gbHandling){gbHandling--;return;}long lFrameType = pFrameInfo->nType;if (lFrameType == T_YV12){Mat pImg(pFrameInfo->nHeight, pFrameInfo->nWidth, CV_8UC3);Mat src(pFrameInfo->nHeight + pFrameInfo->nHeight / 2, pFrameInfo->nWidth, CV_8UC1, pBuf);cvtColor(src, pImg, CV_YUV2BGR_YV12);//Mat frame=imread("E:\\malu.jpg");detectAndDisplay(pImg);waitKey(1);}gbHandling = 3;}///實時流回調 void CALLBACK fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser) {switch (dwDataType){case NET_DVR_SYSHEAD: //系統頭if (!PlayM4_GetPort(&nPort)) //獲取播放庫未使用的通道號{break;}//m_iPort = lPort; //第一次回調的是系統頭,將獲取的播放庫port號賦值給全局port,下次回調數據時即使用此port號播放if (dwBufSize > 0){if (!PlayM4_SetStreamOpenMode(nPort, STREAME_REALTIME)) //設置實時流播放模式{break;}if (!PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 10 * 1024 * 1024)) //打開流接口{break;}if (!PlayM4_Play(nPort, NULL)) //播放開始{break;}if (!PlayM4_SetDecCallBack(nPort, DecCBFun)){break;}}break;case NET_DVR_STREAMDATA: //碼流數據if (dwBufSize > 0 && nPort != -1){if (!PlayM4_InputData(nPort, pBuffer, dwBufSize)){cout << "error" << PlayM4_GetLastError(nPort) << endl;break;}}break;default: //其他數據if (dwBufSize > 0 && nPort != -1){if (!PlayM4_InputData(nPort, pBuffer, dwBufSize)){break;}}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;} }void main() {// 初始化NET_DVR_Init();//設置連接時間與重連時間NET_DVR_SetConnectTime(2000, 1);NET_DVR_SetReconnect(10000, true);//---------------------------------------// 注冊設備LONG lUserID;NET_DVR_DEVICEINFO_V30 struDeviceInfo; ////////////////////////////////////////////////////////////////////////////////////////////////////lUserID = NET_DVR_Login_V30("172.28.168.16", 8000, "admin", "admin12345", &struDeviceInfo); ////////////////////////////////////////////////////////////////////////////////////////////////////if (lUserID < 0){printf("Login error, %d\n", NET_DVR_GetLastError());NET_DVR_Cleanup();return;}//---------------------------------------//設置異常消息回調函數NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL);//---------------------------------------//啟動預覽并設置回調數據流LONG lRealPlayHandle;NET_DVR_PREVIEWINFO struPlayInfo = { 0 };struPlayInfo.hPlayWnd = NULL; //需要SDK解碼時句柄設為有效值,僅取流不解碼時可設為空struPlayInfo.lChannel = 1; //預覽通道號struPlayInfo.dwStreamType = 0; //0-主碼流,1-子碼流,2-碼流3,3-碼流4,以此類推struPlayInfo.dwLinkMode = 0; //0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTPlRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, fRealDataCallBack, NULL);if (lRealPlayHandle < 0){printf("NET_DVR_RealPlay_V40 error\n");printf("%d\n", NET_DVR_GetLastError());NET_DVR_Logout(lUserID);NET_DVR_Cleanup();return;}waitKey();Sleep(-1);//---------------------------------------//關閉預覽NET_DVR_StopRealPlay(lRealPlayHandle);//注銷用戶NET_DVR_Logout(lUserID);//釋放SDK資源NET_DVR_Cleanup();//cvReleaseVideoWriter(&pWriter);cvDestroyWindow("Mywindow");return; } 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: XML--视频--人脸VOC
- 下一篇: 7.30