How to realize face recognition by Java OpenCV
This article mainly explains "how to achieve face recognition with Java OpenCV". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve face recognition with Java OpenCV".
Prepare:
Download the openCV installation package: https://opencv.org/
The installation package supports multiple locales after installation. Java is used here, java/opencv-320.jar under the openCV directory is introduced into Eclipse, and the openCV library path is configured.
Eclipse configuration openCV
Code implementation:
Package test;import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.core.MatOfRect;import org.opencv.core.Point;import org.opencv.core.Rect;import org.opencv.core.Scalar;import org.opencv.objdetect.CascadeClassifier;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;public class Test {/ / introduce the trained face recognition XML file static String PAHT = "E:/GOF/OpenCV/bin/test/haarcascade_frontalface_alt.xml" Static String IMAGE_PATH = "E:/GOF/OpenCV/src/test/a.jpg"; static String productPath = "E:/GOF/OpenCV"; public static void main (String [] args) {System.loadLibrary (Core.NATIVE_LIBRARY_NAME); String Path = Test.class.getResource ("haarcascade_frontalface_alt.xml"). GetPath (); System.out.println (Path); CascadeClassifier faceDetector = new CascadeClassifier (PAHT); Mat image = Imgcodecs.imread (IMAGE_PATH); MatOfRect faceDetections = new MatOfRect (); faceDetector.detectMultiScale (image, faceDetections) System.out.println (String.format ("Detected% s faces", faceDetections.toArray () .length); 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 = "ouput.png"; System.out.println (String.format ("Writing% s", filename)); boolean flag = Imgcodecs.imwrite (filename, image) }}
Achieve results:
Write a border to the face area
At this point, I believe you have a deeper understanding of "Java OpenCV how to achieve face recognition", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!