随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

OpenCV之头像识别采集训练数据

一、概述

  案例:在进行人脸识别之前需要采集人脸数据进行训练,下面就说说如何简单的采集人脸数据。

  需要使用到的工具:

    1.级联分类器--->识别头像区域

    2.将识别的头像区域保存到磁盘

    3.将头像数据的路径和对应的标签放入文件中备用

二、代码示例

 

复制代码
Face_Collect_Face_Data::Face_Collect_Face_Data(QWidget *parent)
    : MyGraphicsView{parent}
{
    this->setWindowTitle("采集人脸数据");
    QPushButton *btn = new QPushButton(this);
    btn->setText("选择视频文件采集人脸数据");
    connect(btn,&QPushButton::clicked,[=](){
        chooseVideo();
    });
}

void Face_Collect_Face_Data::chooseVideo(){
    QString filePath = QFileDialog::getOpenFileName(this,"选择视频文件","/Users/yangwei/Downloads/","数据文件(*.mp4)");
    qDebug()<<filePath;
    saveCollectFaceData(filePath.toStdString().c_str());
}


void Face_Collect_Face_Data::saveCollectFaceData(const char * filePath){
    //级联分类器文件路径
    string haarFilePath = "/usr/local/share/opencv4/haarcascades/haarcascade_frontalface_alt_tree.xml";
    VideoCapture capture;
    capture.open(filePath);
    if(!capture.isOpened()){
        qDebug()<<"无法打开视频文件";
        return;
    }

    CascadeClassifier faceDetector;//实例化级联分类器
    faceDetector.load(haarFilePath);

    Mat frame;
    vector<Rect> faces;
    int count = 0;
    while(capture.read(frame)){
        faceDetector.detectMultiScale(frame,faces,1.1,3,0,Size(100,100),Size(400,400));
        for(int i=0;i<faces.size();i++){
            //将识别到的人脸区域存储起来
            if(count%10==0){
                Mat result;
                cv::resize(frame(faces[i]),result,Size(100,100));
                String path = format("/Users/yangwei/Documents/tony/opencv/myfaces/face_%d.jpg", count);
                cout << path<<endl;
                imwrite(path, result);
            }
            //将识别到的人脸框绘制出来
            rectangle(frame,faces[i],Scalar(255,0,0),3,8);
        }
        imshow("frame",frame);
        int key = waitKey(1);
        if(key==27){
            break;
        }
        count ++;
    }
}
复制代码

 

三、演示图片

 

posted on   飘杨......  阅读(410)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示