人脸识别探测器

人脸识别探测器 Java

  1. 话不多说,咋们直接上代码哈!!!
  2. 实现后的效果就是这样。。。
    人脸识别探测器_第1张图片 人脸识别探测器_第2张图片
package opencv;

import java.io.File;
import java.util.Arrays;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class OpencvTest {

	private static CascadeClassifier faceDetector;

	public static void main(String[] args) {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
		// 初始化人脸探测器
		faceDetector = new CascadeClassifier();
		faceDetector.load("D:\\opencv\\lbpcascade_frontalface.xml");
		//detect("D:\\opencv\\testImg\\bb.jpg", "D:\\opencv\\testImg\\ouput.jpg");
		double res=compare_image("D:\\opencv\\testImg\\baby1.jpg", "D:\\opencv\\testImg\\baby2.jpg");
		System.out.println(res);
	}

	public static void detect(String inImg, String outImg) {
		File file = new File(inImg);
		if (!file.exists()) {
			System.out.println("Image File Not Found");
			return;
		}
		// 读取图像
		Mat image = Imgcodecs.imread(inImg);
		// 用于保存监测到的人脸
		MatOfRect faceDetections = new MatOfRect();
		// 开始监测
		faceDetector.detectMultiScale(image, faceDetections);
		int size = faceDetections.toArray().length;
		System.out.println("人像:" + size);
		int i = 0;
		for (Rect rect : faceDetections.toArray()) {
			// 循环所有监测到的人脸
			Point x = new Point(rect.x, rect.y);
			Point y = new Point(rect.x + rect.width, rect.y + rect.height);
			// 在image图片上画框,x,y可确定框的位置和大小,new Scalar(0, 255, 0)是框的颜色,自行调整
			Imgproc.rectangle(image, x, y, new Scalar(0, 255, 0));
			// 保存监测的人脸小图片
			//Rect r = new Rect(x, y);
			//Mat areaM = new Mat(image, r);
			// 保存监测的人脸小图片到tmp+序号的jpg文件
			//String tmpFilePath = "D:\\opencv\\testImg\\" + "tmp" + (i++) + ".jpg";
			//Imgcodecs.imwrite(tmpFilePath, areaM);
		}
		// 保存画了方框的图片
		Imgcodecs.imwrite(outImg, image);
		// 销毁
		image.release();
	}

	public static double compare_image(String img_1, String img_2) {
		Mat mat_1 = conv_Mat(img_1);
		Mat mat_2 = conv_Mat(img_2);
		Mat hist_1 = new Mat();
		Mat hist_2 = new Mat();
		// 颜色范围
		MatOfFloat ranges = new MatOfFloat(0f, 256f);
		// 直方图大小, 越大匹配越精确 (越慢)
		MatOfInt histSize = new MatOfInt(1000);
		Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
		Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);
		// CORREL 相关系数
		double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
		return res;
	}

	private static Mat conv_Mat(String img_1) {
		Mat image0 = Imgcodecs.imread(img_1);
		Mat image = new Mat();
		// 灰度转换
		Imgproc.cvtColor(image0, image, Imgproc.COLOR_BGR2GRAY);
		MatOfRect faceDetections = new MatOfRect();
		// 探测人脸
		faceDetector.detectMultiScale(image, faceDetections);
		int size = faceDetections.toArray().length;
		System.out.println("人像:" + size);
		// rect中是人脸图片的范围
		for (Rect rect : faceDetections.toArray()) {
			// 切割rect人脸
			Mat mat = new Mat(image, rect);
			return mat;
		}
		return null;
	}

}

核心Xml处理器下载
在这里插入图片描述
来了
来了
它真的来了

重点:核心Xml处理器下载以及引用上面的jar包

你可能感兴趣的:(java,人脸识别探测器,Java)