First of all, of course, the need to install OPENCV, I use opencv2.4.13. After the download can be installed directly, the installation process is very simple, the next step directly next, I will not.
Next find the jar package under OPENCV, for example, I install directly on the C drive, my jar package is in C:\opencv\build\java.
The jar package is then copied to the Lib directory and configured in the idea
Then find the Lbpcascade_frontalface.xml under the OpenCV path. For example, mine is C:\opencv\sources\data\lbpcascades. It is then copied to the SRC directory.
So that the environment is already set up, you can start to write code.
Directly on the code
public static void Main (string[] args) { //Load the native library. System.loadlibrary (core.native_library_name); String url = "G:\\web\\uploadpicture\\src\\main\\resources\\assets\\4.jpg"; New Detectfacedemo (). Go (URL, "g:\\1.jpg"); }
public void Go (String srcfilename,string newpath) {Mat image = null; Cascadeclassifier facedetector = null; String Xmlfilepath = DetectFaceDemo.class.getClassLoader (). GetResource ("Lbpcascade_frontalface.xml"). GetPath (). SUBSTRING (1); try {facedetector = new cascadeclassifier (Xmlfilepath); Image = Highgui.imread (srcfilename); }catch (Exception e) {e.printstacktrace (); }//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 ()) {Core.rectangle (image, New Point (Rect.x, Rect.y), New Point (rect.x + rect.width, Rect.y + rect.height), new Scalar (0, 255, 0)); }//Save the visualized detection. System.out.println (String.Format ("Writing%s", NewPath)); Highgui.imwrite (NewPath, image); }
This way you can run it directly, but you will get an error.
You can then open edit,
Modify VM option-djava.library.path=c:\opencv\build\java\x64
Then you can run it again.
If you want to deploy to the server, the OpenCV jar package must be placed in the LIB, I have created a new folder in the Lib under the jar package in the inside, has been reported ClassNotFound exception.
Then add the path to the Tomcat VM options
But there is a fatal problem, he must be a file path, OPENCV does not provide for the flow processing of the package, this is not in line with the idea of Java, and does not meet the needs of the project, especially now many of the pictures are base64 bits of the stream. So this needs to be wrapped up with the jar of STORMCV, thanks again to Apache.
So we can change the recognition function to.
public void Run (String imgstr) { Base64decoder decoder = new Base64decoder (); Mat image = null; Cascadeclassifier facedetector = null; String Xmlfilepath = DetectFaceDemo.class.getClassLoader (). GetResource ("Lbpcascade_frontalface.xml"). GetPath (). SUBSTRING (1); try { facedetector = new Cascadeclassifier (xmlfilepath); Byte[] B = Decoder.decodebuffer (IMGSTR); Decodes a base64 bitstream into a binary file image = Imageutils.bytes2mat (b); Convert the binary file to Mat }catch (Exception e) { e.printstacktrace (); } 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));}
All right, it's done.
Java+opencv+intellij idea realizes face recognition.