美文网首页
人脸识别

人脸识别

作者: zjh3029 | 来源:发表于2018-04-07 12:10 被阅读0次
    #include <opencv2/opencv.hpp>
    #include "opencv2/face.hpp"
    #include "opencv2/core.hpp"
    #include <stdlib.h>  
    #include <stdio.h>  
    #include <string.h>  
    
    
    #ifdef linux  
    #include <unistd.h>  
    #include <dirent.h>  
    #endif  
    #ifdef _WIN64  
    #include <direct.h>  
    #include <io.h>  
    #endif  
    
    
    using namespace std;
    using namespace cv;
    
    vector<string> image_label_string;
    int count_label;
    
    vector<string> getFiles(const char* cate_dir, vector<string> &files)//输入文件路径,获得文件下所有的名字
    {
    #ifdef _WIN64  
        __finddata64_t   file;
        __int64   Handle;
        Handle = _findfirst64(cate_dir, &file);
        if (Handle == -1)
            cerr << "wrong!" << endl;
        do
        {
            files.push_back(file.name);
            cout << file.name << endl;
        } while (_findnext64(Handle, &file) == 0);
        _findclose(Handle);
    #endif  
    
    
    #ifdef linux  
        DIR *dir;
        struct dirent *ptr;
        char base[1000];
    
        if ((dir = opendir(cate_dir.c_str())) == NULL)
        {
            perror("Open dir error...");
            exit(1);
        }
    
        while ((ptr = readdir(dir)) != NULL)
        {
            if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)    ///current dir OR parrent dir  
                continue;
            else if (ptr->d_type == 8)    ///file  
                                          //printf("d_name:%s/%s\n",basePath,ptr->d_name);  
                files.push_back(ptr->d_name);
            else if (ptr->d_type == 10)    ///link file  
                                           //printf("d_name:%s/%s\n",basePath,ptr->d_name);  
                continue;
            else if (ptr->d_type == 4)    ///dir  
            {
                files.push_back(ptr->d_name);
                /*
                memset(base,'\0',sizeof(base));
                strcpy(base,basePath);
                strcat(base,"/");
                strcat(base,ptr->d_nSame);
                readFileList(base);
                */
            }
        }
        closedir(dir);
    #endif  
    
        //排序,按从小到大排序  
        sort(files.begin(), files.end());
        return files;
    }
    
    
    
    void read_picture(const char* path,vector<int> &image_label, vector<Mat> &images, vector<string> &images_name,string name)
    {
        count_label++;
        images_name.clear();
        getFiles(path, images_name);
        string str_path = path;
        string str = str_path.substr(0, str_path.length() - 5);
        cout << "str:" << str << endl;
        for (size_t i = 0; i < images_name.size(); i++)
        {
            images.push_back(imread(str +images_name[i], CV_LOAD_IMAGE_GRAYSCALE));
            //cout << "str:" << str + images_name[i] << endl;
            image_label.push_back(count_label);
        }
        image_label_string.push_back(name);
        return;
    }
    
    int main()
    {
        vector<Mat> images;
        vector<int> labels;
        vector<string> images_name;
        int predicted_label = -1;
        double predicted_confidence = 0.0;
    
        read_picture("C:\\Users\\zhaoj\\Downloads\\orl_faces\\s1\\*.pgm", labels,images, images_name,"xiaoming");
        read_picture("C:\\Users\\zhaoj\\Downloads\\orl_faces\\s2\\*.pgm", labels,images, images_name,"xiaohong");
        read_picture("C:\\Users\\zhaoj\\Downloads\\orl_faces\\s3\\*.pgm", labels,images, images_name,"xiaogang");
        read_picture("F:\\picture\\*.jpg", labels, images, images_name, "xiaoqiang");
    
        Ptr<face::FaceRecognizer> model = face::FisherFaceRecognizer::create();
        model->train(images, labels);
        model->save("MyFacePCAModel.xml");
    
        Mat img = imread("C:\\Users\\zhaoj\\Downloads\\orl_faces\\s4\\1.pgm", CV_LOAD_IMAGE_GRAYSCALE);
        model->predict(img, predicted_label,predicted_confidence);
    
        std::cout << "predit:" << image_label_string[predicted_label -1]<< std::endl;
        std::cout << "predit:" << predicted_confidence << std::endl;
    
        system("pause");
    }
    

    相关文章

      网友评论

          本文标题:人脸识别

          本文链接:https://www.haomeiwen.com/subject/zddhcftx.html