Make writing a habit together! This is the fifth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Welcome to my GitHub

Here classification and summary of xinchen all original (including supporting source code) : github.com/zq2599/blog…

This paper gives an overview of

  • We use the camera to generate A large number of face photos for the two extras, as shown below, the picture of extras A is saved in E:\temp\202112\18\001\man, B’s photo is saved in E:\ Temp \202112\18\001\ Woman:

  • The photos are ready, and each photo has been identified, this is to use the above photos to generate a model file, the future of the new face can be examined in this model
  • The training can be represented by the following figure, consisting of six photos and two categories. After the training is completed, the model file facerecognizer.xml is obtained:

coding

  • The training code is very simple, in a Java file to fix it, simple-grab-push is the whole “JavaCV camera field” series has been used for the project, now the project added file TrainFromDirectory. Java, the complete code is as follows: There are a few caveats mentioned later:
public class TrainFromDirectory {

    /** ** from the specified directory *@param dirs
     * @param outputPath
     * @throws IOException
     */
    private void train(String[] dirs, String outputPath) throws IOException {
        int totalImageNums = 0;

        // Count the number of photos under each path
        for(String dir : dirs) {
            List<String> files = getAllFilePath(dir);
            totalImageNums += files.size();
        }

        System.out.println("total : " + totalImageNums);

        // This is used to save the serial number of each photo and the Mat object of the photo
        MatVector imageIndexMatMap = new MatVector(totalImageNums);

        Mat lables = new Mat(totalImageNums, 1, CV_32SC1);

        // This is used to save the serial number of each photo and the category of the photo
        IntBuffer lablesBuf = lables.createBuffer();

        // Category number, starting at 1, each directory in dirs is a category
        int kindIndex = 1;

        // Start from 0
        int imageIndex = 0;

        // The photos in each directory are traversed
        for(String dir : dirs) {
            // Get the absolute path of all photos in the current directory
            List<String> files = getAllFilePath(dir);

            // Process each photo in a directory with a different serial number and the same category
            for(String file : files) {
                // imageIndexMatMap places the number of the photo and the Mat object
                imageIndexMatMap.put(imageIndex, read(file));
                // bablesBuf contains the photo number and category
                lablesBuf.put(imageIndex, kindIndex);
                // Add one to the photo number
                imageIndex++;
            }

            // Each time a directory is traversed, the category is incremented by one
            kindIndex++;
        }

        // Instantiate the face recognition class
        FaceRecognizer faceRecognizer = FisherFaceRecognizer.create();
        // Training, entry is the image collection and classification collection
        faceRecognizer.train(imageIndexMatMap, lables);
        // After training, the model is saved in the specified location
        faceRecognizer.save(outputPath);
        // Release resources
        faceRecognizer.close();
    }

    /** * Read the grayscale of the specified image and adjust it to the specified size *@param path
     * @return* /
    private static Mat read(String path) {
        Mat faceMat = opencv_imgcodecs.imread(path,IMREAD_GRAYSCALE);
        resize(faceMat, faceMat, new Size(Constants.RESIZE_WIDTH, Constants.RESIZE_HEIGHT));
        return faceMat;
    }

    /** * Puts the absolute path of all files under the specified path into the list collection *@param path
     * @return* /
    public static List<String> getAllFilePath(String path) {
        List<String> paths = new LinkedList<>();

        File file = new File(path);

        if (file.exists()) {
            // Lists all files in this directory
            File[] files = file.listFiles();

            for (File f : files) {
                if(! f.isDirectory()) {// Put the absolute path of each file in the listpaths.add(f.getAbsolutePath()); }}}return paths;
    }

    public static void main(String[] args) throws IOException {

        String base = "E:\\temp\\202112\\18\\001\\";

        // Two directories for storing images
        // The man directory saves all the face photos of the extras A,
        // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
        String[] dirs = {base + "man", base + "woman"};

        // Start training and specify the model output position
        new TrainFromDirectory().train(dirs, base + "faceRecognizer.xml"); }}Copy the code
  • There are a few things to note about this code:
  1. The static method read is used to convert an image to Mat
  2. The static getAllFilePath method iterates through all files in a specified directory, returning their absolute paths
  3. Train obtained photos in man and Woman. The category of photos in man is 1, and the category of photos in women is 2
  4. The recognition class is the FisherFaceRecognizer, which will be used for both current training and the next recognition

perform

  • Facerecognizer.xml has been generated under E:\temp\202112\18\001: facerecognizer.xml

  • So far, the task of this part has been completed, the next part enters the ultimate actual combat, using the model trained in this part to recognize the face in the camera, and showing the recognition results on the preview page;

Download the source code

  • The full source code for JavaCV Camera In Action is available on GitHub at github.com/zq2599/blog…
The name of the link note
Project home page Github.com/zq2599/blog… The project’s home page on GitHub
Git repository address (HTTPS) Github.com/zq2599/blog… The project source warehouse address, HTTPS protocol
Git repository address (SSH) [email protected]:zq2599/blog_demos.git The project source warehouse address, SSH protocol
  • The Git project has multiple folders. The source code for this project is in the Javacv-tutorials folder, as shown in the red box below:

  • Javacv-tutorials have many subprojects. The Code for the JavacV Camera Hands-on series was based on the Simply-grab-push project:

Welcome to the Nuggets: programmer Chen Chen

Learning on the road, you are not alone, Xinchen original all the way accompanied by…