zoukankan      html  css  js  c++  java
  • Opencv数据及收集python脚本(生成CSV文件)

    #!/usr/bin/env python
    import sys
    import os.path
    
    # This is a tiny script to help you creating a CSV file from a face
    # database with a similar hierarchie:
    #
    #  philipp@mango:~/facerec/data/at$ tree
    #  .
    #  |-- README
    #  |-- s1
    #  |   |-- 1.pgm
    #  |   |-- ...
    #  |   |-- 10.pgm
    #  |-- s2
    #  |   |-- 1.pgm
    #  |   |-- ...
    #  |   |-- 10.pgm
    #  ...
    #  |-- s40
    #  |   |-- 1.pgm
    #  |   |-- ...
    #  |   |-- 10.pgm
    #
    
    if __name__ == "__main__":
    
        # if len(sys.argv) != 2:
        #   print("usage: create_csv <base_path>")
        #   sys.exit(1)
        BASE_PATH = "E:/DeskTop/FaceOpencv/FaceResource/att_faces_pgm"
        SEPARATOR = ";"
        fh = open("E:/DeskTop/FaceOpencv/FaceResource/att_faces_pgm/at.txt", 'w')
        label = 0
        for dirname, dirnames, filenames in os.walk(BASE_PATH):
            for subdirname in dirnames:
                subject_path = os.path.join(dirname, subdirname)
                for filename in os.listdir(subject_path):
                    abs_path = "%s/%s" % (subject_path, filename)
                    print("%s%s%d" % (abs_path, SEPARATOR, label))
                    fh.write(abs_path)
                    fh.write(SEPARATOR)
                    fh.write(str(label))
                    fh.write("
    ")
                label = label + 1
        fh.close()
    

    1、获取人脸图像时,直接获取的大小与官方给出的模型大小一直进行保存
    https://blog.csdn.net/qq_42449351/article/details/99052241?utm_medium=distribute.pc_relevant.none-task-blog-2

    #include <iostream>
    #include <opencv2opencv.hpp>
    #include <vector>
    #include<stdio.h>
    using namespace std;
    using namespace cv;
    int main()
    {
        CascadeClassifier cascada;
        //将opencv官方训练好的人脸识别分类器拷贝到自己的工程目录中
        cascada.load("F:\video\pic\haarcascade_frontalface_alt2.xml");
        VideoCapture cap(0);  //0表示电脑自带的,如果用一个外接摄像头,将0变成1
        Mat frame, myFace;
        int pic_num = 1;
        while (1) {
            //摄像头读图像
            cap >> frame;
            vector<Rect> faces;//vector容器存检测到的faces
            Mat frame_gray;
            
            cvtColor(frame, frame_gray, COLOR_BGR2GRAY);//转灰度化,减少运算
            
            cascada.detectMultiScale(frame_gray, faces, 1.1, 4, CV_HAAR_DO_ROUGH_SEARCH, Size(70, 70), Size(1000, 1000));
            printf("检测到人脸个数:%d
    ", faces.size());
           
            //识别到的脸用矩形圈出
            for (int i = 0; i < faces.size(); i++)
            {
                rectangle(frame, faces[i], Scalar(255, 0, 0), 2, 8, 0);
            }
            //当只有一个人脸时,开始拍照
            if (faces.size() == 1)
            {
                Mat faceROI = frame_gray(faces[0]);//在灰度图中将圈出的脸所在区域裁剪出
                //cout << faces[0].x << endl;//测试下face[0].x
                resize(faceROI, myFace, Size(92, 112));//将兴趣域size为92*112
                putText(frame, to_string(pic_num), faces[0].tl(), 3, 1.2, (0, 0, 225), 2, 0);//在 faces[0].tl()的左上角上面写序号
                string filename = format("F:\video\%d.jpg", pic_num); //图片的存放位置,frmat的用法跟QString差不对
                imwrite(filename, myFace);//存在当前目录下
                imshow(filename, myFace);//显示下size后的脸
                waitKey(500);//等待500us
                destroyWindow(filename);//:销毁指定的窗口
                pic_num++;//序号加1
                if (pic_num == 11)
                {
                    return 0;//当序号为11时退出循环,一共拍10张照片
                }
            }
            int c = waitKey(10);
            if ((char)c == 27) { break; } //10us内输入esc则退出循环
            imshow("frame", frame);//显示视频流
            waitKey(100);//等待100us
        }
        return 0;
     
    }
    

    2、获取到一个大小的人脸图像,人后利用代码自己进行分割处理
    https://blog.csdn.net/shangpapa3/article/details/60763634?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0.no_search_link&spm=1001.2101.3001.4242

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/objdetect/objdetect.hpp>
    
    using namespace cv;
    
    int main()
    {
        char file_img[160];
        Mat t_img[160];
        vector<Rect> faces;
        CascadeClassifier face_cascade;
        face_cascade.load("haarcascade_frontalface_alt.xml");
    
        //Size dst_size;
        //读取训练样本
        for (int i = 0; i < 160; i++)
        {
            Mat img_gray;
            int d = i + 1;
            sprintf(file_img, "C:\Users\Apple\Desktop\头2\%d.jpg", d);
            Mat img = imread(file_img, 1);
            cvtColor(img, img_gray, COLOR_BGR2GRAY);
            equalizeHist(img_gray, img_gray);
    
            //-- Detect faces;
            //检测人脸
            face_cascade.detectMultiScale(img_gray, faces, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, Size(60, 60),Size(240,240));
            if (faces.size() == 0)
            {
    
                continue;
    
            }
            Mat faceROI = img(faces[0]);
            Mat MyFace;
            resize(faceROI, MyFace, Size(90, 112));
            cvtColor(MyFace, MyFace, COLOR_BGR2GRAY);
            string  str = format("C:\Users\Apple\Desktop\头3\%d.jpg", d);
            imwrite(str, MyFace);
            imshow("ii", MyFace);
            waitKey(10);
        }
    }
    

    https://blog.csdn.net/qq_43131852/article/details/102485061

    -------------------------------------- 适合自己的才是最好的!-----------------------------------------
  • 相关阅读:
    【java8】慎用java8的foreach循环(作废)
    【Java并发系列03】ThreadLocal详解
    【Java并发系列04】线程锁synchronized和Lock和volatile和Condition
    【Java并发系列02】Object的wait()、notify()、notifyAll()方法使用
    【Java并发系列01】Thread及ThreadGroup杂谈
    java安全管理器SecurityManager入门
    【DWR系列06】- DWR日志及js压缩
    时间插件
    springMVC中Restful支持
    面向接口编程
  • 原文地址:https://www.cnblogs.com/retry/p/15329592.html
Copyright © 2011-2022 走看看