cuda10.1+opencv4.4.0-pre 多人脸检测(GPU版本)

**

cuda10.1+opencv4.4.0-pre 多人脸检测(GPU版本)

说明: 代码中使用的模型文件:haarcascade_frontalface_alt.xml ,可以使用opencv的github 的data/haarcascades_cuda/haarcascade_frontalface_default.xml 文件。

#include 
#include 
#include 
#include 
#include 
#include 

using namespace cv;

int main(int argc,char *argv[])  {

    auto gpu_num = cuda::getCudaEnabledDeviceCount();;
    std::cout<<"gpu num:"<<gpu_num<<std::endl;

    auto dev_id = cuda::getDevice();
    std::cout<<"device id:"<<dev_id<<std::endl;

    if (argc != 2) {
        std::cout << argv[0] << " jpgname" << std::endl;
        return 0;
    }

    Mat image = imread(argv[1], 1);
    auto cc = cuda::CascadeClassifier::create("./haarcascade_frontalface_alt.xml");
    if (!cc) {
        std::cout << "加载分类器失败" << std::endl;
        return 0;
    }

    cuda::GpuMat GpuGray;
    cuda::GpuMat GpuImage(image);
    cuda::cvtColor(GpuImage, GpuGray, CV_BGR2GRAY);
    cuda::equalizeHist(GpuGray, GpuGray);

    std::vector<Rect> faces;
    cuda::GpuMat objbuf;
    std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
    cc->detectMultiScale(GpuGray, objbuf);
    std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();

    auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
    cc->convert(objbuf, faces);
    std::cout << "face size:" << faces.size() <<", cost :"<< diff.count()<<" mill"<< std::endl;
    for (auto const& face : faces) {
        rectangle(image, Rect(face.x, face.y, face.width, face.height), Scalar(0,0,255), 3);
    }

    imwrite("res_gpu.jpg", image);

    return 0;
}

效果如下:
cuda10.1+opencv4.4.0-pre 多人脸检测(GPU版本)_第1张图片
cuda10.1+opencv4.4.0-pre 多人脸检测(GPU版本)_第2张图片

你可能感兴趣的:(cuda10.1+opencv4.4.0-pre 多人脸检测(GPU版本))