书单推荐:成为Java顶级程序员架构师 ,这20来本(高薪)必看点击获取
准备 :
下载openCV安装包 : https://opencv.org/
安装包安装之后支持多种语言环境,此处使用Java,在Eclipse中引入 openCV目录下的java/opencv-320.jar,同时配置openCV库路径。
Eclipse配置openCV
代码实现 :
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 { //引入训练好的人脸识别XML文件 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); } }
实现效果 :
对人脸区域写入边框
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持谷谷点程序。
转载请注明:谷谷点程序 » Java OpenCV实现人脸识别过程详解