Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use opencv to develop face recognition function in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to use opencv to develop face recognition functions in Java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.

Background: recently, we need to use face recognition, but do not spend money to use the existing third-party face recognition interface, so we use opencv combined with java for face recognition (ps:opencv is open source, there are some errors in using it for face recognition, and the effect is general).

1. Install opencv

If it is downloaded from the official website, it can be installed without a brain, after the installation is finished.

two。 Introduce pom dependencies into the project

Org.bytedeco.javacpp-presets ffmpeg 4.1-1.4.4 org.bytedeco javacv 1.4.4 org.bytedeco.javacpp-presets ffmpeg-platform 4.1-1.4.4 Org.bytedeco javacv-platform 1.4.4 org.bytedeco.javacpp-presets opencv-platform 4.0.1-1.4.4

1. Import library dependencies

File-- > Project Structure, click Modules, and select the project that needs to use opencv.jar.

Select the direct opencv installation path

2.java Code demo

Package org.Litluecat.utils;import org.apache.commons.lang.StringUtils;import org.opencv.core.*;import org.opencv.highgui.HighGui;import org.opencv.highgui.ImageWindow;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;import org.opencv.objdetect.CascadeClassifier;import org.opencv.videoio.VideoCapture;import org.opencv.videoio.VideoWriter;import org.opencv.videoio.Videoio;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.Arrays / * face comparison tool * @ author Litluecat * @ Title: Opencv image face recognition, real-time camera face recognition * * / public class FaceVideo {private static final Logger log = LoggerFactory.getLogger (FaceVideo.class); private static final String endImgUrl = "C:\ Users\ lenovo\ Desktop\" / * * xml file path for opencv * / private static final String faceDetectorXML2URL = "D:\ Sofeware\ opencv\ sources\ data\ haarcascades\ haarcascade_frontalface_alt.xml"; / * * xml file path for human eye recognition of opencv * / private static final String eyeDetectorXML2URL = "D:\ Sofeware\ opencv\ sources\ data\ haarcascades\ haarcascade_eye.xml" / * histogram size, the larger the precision, the slower the operation * / private static int Matching_Accuracy = 1000000; / * * initialize the face detector * / private static CascadeClassifier faceDetector; / * initialize the human eye detector * / private static CascadeClassifier eyeDetector; private static int idetector; static {System.loadLibrary (Core.NATIVE_LIBRARY_NAME) FaceDetector = new CascadeClassifier (faceDetectorXML2URL); eyeDetector = new CascadeClassifier (eyeDetectorXML2URL);} public static void main (String [] args) {log.info ("start face matching"); long begin = System.currentTimeMillis (); / / 1-Real-time face recognition from the camera, successfully saving the image to the local try {getVideoFromCamera (endImgUrl + "2.jpg") / / it is only used to force an exception to be thrown, thus closing the GUI interface Thread.sleep (1000); int err = 1); / / 2-comparing the face similarity of the two local images (the closer to 1, the more similar) / / double compareHist = FaceVideo.compare_image (endImgUrl + "test1.jpg", endImgUrl + "face.jpg") / / log.info ("matching degree: {}", compareHist); / / if (compareHist > 0.72) {/ / log.info ("face matching"); / /} else {/ / log.info ("face mismatch") / /}} catch (Exception e) {log.info ("start forced shutdown"); log.info ("face matching ends, total time consuming: {} ms", (System.currentTimeMillis ()-begin)); System.exit (0) }} / * OpenCV-4.1.1 read * @ param targetImgUrl compare ID card picture * @ return: void * @ date: 17:20:13 * / public static void getVideoFromCamera (String targetImgUrl) {/ / 1 if you want to obtain video from the camera, write 0 VideoCapture capture = new VideoCapture (0) in the construction method of VideoCapture. Mat video = new Mat (); int index = 0; if (capture.isOpened ()) {while (I = 1) {iTunes; if (iTunes 3) {/ / get the photo Imgcodecs.imwrite (endImgUrl + "face.jpg", image) that matches successfully for the third time FaceVideoThread faceVideoThread = new FaceVideoThread (targetImgUrl, endImgUrl + "face.jpg"); new Thread (faceVideoThread, "face comparison thread") .start ();}} return image;} / * * face screenshot * @ param img * @ return * / public static String face2Img (String img) {String faceImg = null Mat image0 = Imgcodecs.imread (img); Mat image1 = new Mat (); / / Grey Imgproc.cvtColor (image0, image1, Imgproc.COLOR_BGR2GRAY); / / detect MatOfRect faceDetections = new MatOfRect (); faceDetector.detectMultiScale (image1, faceDetections) / / the range of face images in rect for (Rect rect: faceDetections.toArray ()) {faceImg = img+ "_ .jpg"; / / clip imageCut (img, faceImg, rect.x, rect.y, rect.width, rect.height) } if (null = = faceImg) {log.info ("face2Img does not recognize the face in this image, img= {}", img);} return faceImg Face comparison * @ param img_1 * @ param img_2 * @ return * / 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 () / / Color range MatOfFloat ranges = new MatOfFloat (0f, 256f); / / histogram size, the larger the match, the more accurate (slower) MatOfInt histSize = new MatOfInt (Matching_Accuracy); 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 correlation coefficient double res = Imgproc.compareHist (hist_1, hist_2, Imgproc.CV_COMP_CORREL); return res } / * gray face * @ param img * @ return * / public static Mat conv_Mat (String img) {if (StringUtils.isBlank (img)) {return null;} Mat image0 = Imgcodecs.imread (img); Mat image1 = new Mat (); / / Mat image2 = new Mat () / / Grey Imgproc.cvtColor (image0, image1, Imgproc.COLOR_BGR2GRAY); / / histogram uniform / / Imgproc.equalizeHist (image1, image2); / / detect human face MatOfRect faceDetections = new MatOfRect (); faceDetector.detectMultiScale (image1, faceDetections); / / detect human eyes / / MatOfRect eyeDetections = new MatOfRect (); / / eyeDetector.detectMultiScale (image1, eyeDetections) / / the range of face pictures in rect Mat face = null; for (Rect rect: faceDetections.toArray ()) {/ / frame parameter 1 is picture parameter 2 is rectangle parameter 3 is color parameter 4 is line size / / Imgproc.rectangle (image0,rect,new Scalar (0prime0255), 2) / / output image / / Imgcodecs.imwrite (img+ "_ .jpg", image0); face = new Mat (image1, rect);} if (null = = face) {log.info ("conv_Mat does not recognize the face in this image, img= {}", img);} return face;}}

This side of the face recognition is another thread comparison, the code is as follows.

Package org.Litluecat.utils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class FaceVideoThread implements Runnable {private static final Logger log = LoggerFactory.getLogger (FaceVideoThread.class); private String oneImgUrl = null; private String otherImgUrl = null; public FaceVideoThread (String oneImgUrl, String otherImgUrl) {this.oneImgUrl = oneImgUrl; this.otherImgUrl = otherImgUrl } @ Override public void run () {try {double compareHist = FaceVideo.compare_image (oneImgUrl, otherImgUrl); log.info ("match degree: {}", compareHist); if (compareHist > 0.72) {log.info ("face matching");} else {log.info ("face mismatch") } catch (Exception e) {e.printStackTrace ();}

Reminder: if the operation is abnormal, please add your opencv installation address-Djava.library.path=D:Sofewareopencvuildjavax64

These are all the contents of the article "how to use opencv to develop face recognition function in Java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report