java集成opencv人脸识别

发布时间:2023-09-04 11:30

一、下载,https://opencv.org/releases/

\"java集成opencv人脸识别_第1张图片\"

二、安装

三、配置环境变量

1、新建OPENCV_HOME

\"java集成opencv人脸识别_第2张图片\"

2、添加path

\"\"

三、新建springboot项目,jdk选择8

1、pom添加依赖

        
            org
            opencv
            4.5.1
        

2、Edit Configuration,添加 -Djava.library.path=D:\\Dev\\opencv\\opencv\\build\\java\\x64

\"java集成opencv人脸识别_第3张图片\"

3、代码,新建FaceDetectTest测试类

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class FaceDetectTest {

    @Test
    void contextLoads() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        // Create a face detector from the cascade file in the resources
        //分类器文档地址
        CascadeClassifier faceDetector = new CascadeClassifier(\"D:\\\\Dev\\\\opencv\\\\opencv\\\\sources\\\\data\\\\lbpcascades\\\\lbpcascade_frontalface.xml\");
        if(faceDetector.empty()){
            System.out.println(\"读取配置文件失败\");
            return;
        }
        //识别的文档地址
        Mat image = Imgcodecs.imread(\"D:\\\\face.png\");
        // Detect faces in the image.
        // MatOfRect is a special container class for Rect.
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);
        System.out.println(String.format(\"Detected %s faces\", faceDetections.toArray().length));
        // Draw a bounding box around each face.
        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
        }
        //识别后文档地址
        String filename = \"D:\\\\faceDetection.png\";
        System.out.println(String.format(\"Writing %s\", filename));
        Imgcodecs.imwrite(filename, image);
    }

}

四、结果对比

1\"java集成opencv人脸识别_第4张图片\"\"java集成opencv人脸识别_第5张图片\"

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号