本文是关于海康视频及openCv抓拍正脸接口调试:通过海康摄像头获取视频流,opencv转换成Mat类型对象,检测人脸对象是否存在。

海康和opencv库和头文件的添加如下:

win32{
                INCLUDEPATH += $$PWD\..\hikvision\include

                LIBS += -L$$PWD\..\hikvision\lib\ -lHCNetSDK -lHCCore -lPlayCtrl -lGdiPlus \
                 -L$$PWD\..\hikvision\lib\HCNetSDKCom -lHCAlarm -lHCGeneralCfgMgr -lHCPreview \


                 # release openCV
                INCLUDEPATH +=  $$PWD\..\openCV34\opencv-3.4.16-win-64bit-mingw-lib-release-20241101\include\ 
                                                 $$PWD\..\openCV34\openCV34\opencv-3.4.16-win-64bit-mingw-lib-release-20241101\include\opencv2
                LIBS += $$PWD\..\openCV34\opencv-3.4.16-win-64bit-mingw-lib-release-20241101\x64\mingw\lib\libopencv*.a
                LIBS +=-L$$PWD\..\openCV34\opencv-3.4.16-win-64bit-mingw-lib-release-20241101\x64\mingw\bin\
}


unix{
                 INCLUDEPATH += $$PWD/../hikvision_linux/include


                LIBS += -L$$PWD/../hikvision_linux/lib/ -lhcnetsdk -lHCCore -lPlayCtrl -lAudioRender -lSuperRender \
                 -L$$PWD/../hikvision_linux/lib/HCNetSDKCom -lHCAlarm -lHCGeneralCfgMgr -lHCPreview
}

海康视频流数据转换成Mat对象类型的方法:

  #define PHOTO_W 384
    #define PHOTO_H 288
QByteArray captureFace()
{
    QByteArray photo_buf;
    unsigned char* buf = new unsigned char[PHOTO_W*PHOTO_H];
    if(lUserID!=-1)
    {
        NET_DVR_JPEGPARA IpJpegPara2;
        IpJpegPara2.wPicQuality=1;
        IpJpegPara2.wPicSize=WORD(0);
        DWORD sL = 0;

        //***********设备抓拍(注:必须子码流)************************
        bool isCaptureOk = NET_DVR_CaptureJPEGPicture_NEW(lUserID,videoChannel,&IpJpegPara2,buf,PHOTO_W*PHOTO_H,&sL);
        if( isCaptureOk == false) {
            qDebug() << "NET_DVR_CaptureJPEGPicture error;" << "error number is " << NET_DVR_GetLastError();
        }
        else
        {
            std:: vector<Rect> faces;

            方法一:将海康的视频数据流uchar*转为opencv的Mat对象类型
            // 创建一个 320x240 的 8 位无符号型 4 通道全 0 的 Mat 
            cv::Mat mRGBAImg = cv::Mat::zeros(PHOTO_W,PHOTO_H, CV_8UC4);
            mRGBAImg.data = buf;
            方法二:将海康的视频数据流uchar*转为opencv的Mat对象类型
            vector <unsigned char> vec (buf,buf+PHOTO_W*PHOTO_H);
            cv::Mat img=cv::imdecode(vec,cv::IMREAD_COLOR);
                      方法三:将一个已经存在的jpeg格式文件转换为opencv的Mat对象类型
             QImage image;
             QBuffer buffer;
             buffer.open(QIODevice::ReadWrite);
             buffer.write(reinterpret_cast<const char*>(buf), PHOTO_W*PHOTO_H);
             image.loadFromData(buffer.data(), "JPG");
             buffer.close();
             image.save(".\\capture.jpg","JPG", 100);
                        cv::Mat matFrame = cv::imread(".\\capture.jpg", cv::IMREAD_COLOR);

            mFaceCascade.detectMultiScale(matFrame,faces,1.1,3,0,Size(50,50),Size(500,500));

            if(faces.size()>0){//人脸存在
                photo_buf.append(buf, PHOTO_W*PHOTO_H);
            }
            else{
                qDebug()<<"no catch face!";
            }

        }
    }
    delete[] buf;
    buf = nullptr;
    return photo_buf;
}

如果海康威视SDK确实不支持在函数调用时自动添加时间戳,你可以在保存图片后手动为图片加上时间戳。这通常需要使用图像处理库(如OpenCV)来加载图片,添加文本,然后保存。

示例代码:

// 使用OpenCV添加时间戳
cv::Mat image = cv::imread("screenshot.jpg");
std::string timestamp = GetCurrentTimeStamp(); // 自定义函数获取时间戳字符串
cv::putText(image, timestamp, cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255, 255, 255));
cv::imwrite("screenshot_with_timestamp.jpg", image);

 

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部