海康摄像头

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.lib

GdiPlus.lib
HCCore.lib
HCNetSDK.LIB
PlayCtrl.lib
HCAlarm.lib
HCGeneralCfgMgr.lib
HCPreview.lib
MSVCRTD.lib
libfacedetect-x64.lib
libfacedetect.lib

代码:

#include 
#include 
#include 
#include "Windows.h"
#include "HCNetSDK.h"
#include "plaympeg4.h"
#include 
#include 
#include 
#include 
#include "facedetect-dll.h"
#pragma comment(lib,"libfacedetect.lib")
#define DETECT_BUFFER_SIZE 0xC004

using 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 !!!不要释放pResults
        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 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/HTTP

    lRealPlayHandle = 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;
}

你可能感兴趣的:(VS2010+OpenCV)