当前位置:网站首页>Opencv does not rely on any third-party database for face detection

Opencv does not rely on any third-party database for face detection

2022-06-10 18:54:00 QMCY_ jason

design sketch :

Code section : It doesn't involve any complicated Machine learning library or something opencv A few simple api You can run , Simply do an introductory guide   You can find The side face cannot be detected   The face is basically detectable  


void do_findface()
{
    string xml_path = R"(E:\jason\software\opencv\opencv\build\etc\haarcascades\haarcascade_frontalface_alt2.xml)";// Similar to the model file  opencv Self contained 
    string img_path = R"(E:/jason/5D4/multi2.png)"; // Pictures to be tested 

    Mat face_img = imread(img_path,IMREAD_COLOR);   // Normal color map 
    Mat face_gray = imread(img_path,IMREAD_GRAYSCALE);// Open in grayscale mode 

    namedWindow("gray", WINDOW_NORMAL);
    namedWindow("equalizeHist", WINDOW_NORMAL);

    if (face_img.empty() || face_gray.empty())
    {
        cout<<"Load image failed" << endl;
    }

    imshow("gray", face_gray);

    equalizeHist(face_gray,face_gray); // Histogram equalization   Add comparison chart   Do not do   In this example, the effect is not significant   More importantly, it affects the performance estimation 

    
    //imshow("equalizeHist", face_gray);
    
    CascadeClassifier faceCascade;
    if (!faceCascade.load(xml_path))
    {
        cout << "Load xml path failed :["<<xml_path<<"]" << endl;
        return ;
    }

    vector<Rect>faces;  // The detected face rectangle coordinates will be put here 

    faceCascade.detectMultiScale(face_gray,faces,1.2,5,0,Size(30,30));

    int index = 0;
    for (auto b:faces)
    {
        index++;
        cout << "Face pos:[" << index << "][" << b.x << "," << b.y <<","<<b.width<<","<<b.height << "]" << endl;
    }

    if (faces.size() > 0)
    {
        for (size_t i = 0; i < faces.size(); i++)
        {
            putText(face_img,"face",Point(faces[i].x-10,faces[i].y-10), FONT_HERSHEY_COMPLEX_SMALL, 1.0, Scalar(0, 0, 255));
            rectangle(face_img, Point(faces[i].x, faces[i].y), Point(faces[i].x+faces[i].width,faces[i].y+faces[i].height), Scalar(0, 255, 0), 2, 8);
            cout << "faces:------------------>" << faces[i] << endl;
        }
        
    }
    imshow("equalizeHist",face_img);
    waitKey();

    destroyAllWindows();
}




int main()
{
    //do_filter();
    //do_addweight();
    //do_sharpen();
    //do_cvtColor();

    //do_findContours();
    do_findface();
    return 0;
}

原网站

版权声明
本文为[QMCY_ jason]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101808157330.html