opencv 3 java face recognition example requested

asked 2015-08-06 07:48:40 -0500

zaguia gravatar image

updated 2015-08-06 08:50:59 -0500

thdrksdfthmn gravatar image

For now on opencv 3 I'm succeeding face detection. but I could not even start face recognition. I could not find facerec.dll and I do not have the compilers to have it from cpp files... Can somebody give an opencv3 java example running face recognition against a database?

//here is the face detection example : (you should change the paths to files according to your computer...)
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 static org.opencv.imgcodecs.Imgcodecs.imread;
import static org.opencv.imgcodecs.Imgcodecs.imwrite;
import static org.opencv.imgproc.Imgproc.rectangle;
//import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.CascadeClassifier;
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
class DetectFaceDemo {
  public void run() {
    System.out.println("\nRunning DetectFaceDemo");
    // Create a face detector from the cascade file in the resources
    // directory.
    CascadeClassifier faceDetector = new CascadeClassifier("C:\\Users\\HP\\Downloads\\opencv\\opencv\\sources\\data\\lbpcascades\\lbpcascade_frontalface.xml");
    Mat image = imread("C:\\Users\\HP\\Desktop\\day12-face-detection-master\\2faces2.png");
    // 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()) {
        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.
    String filename = "faceDetection.png";
    System.out.println(String.format("Writing %s", filename));
    imwrite(filename, image);
    //FaceRecognizer fr;//= new LBPHFaceRecognizer();
public class HelloOpenCV {
  public static void main(String[] args) {
    System.out.println("Hello, OpenCV");
    // Load the native library.
    //System.load("C:\\Users\\HP\\Downloads\\opencv 2.4.11\\opencv\\build\\java\\x64\\opencv_java2411.dll");
    new DetectFaceDemo().run();
edit retag flag offensive close merge delete


  1. download/clone the opencv_contrib repo
  2. change to: ocv_define_module(face opencv_core opencv_imgproc opencv_objdetect WRAP python java)
  3. rebuild/install the main opencv repo (together with the contrib parts) starting with cmake
  4. import org.opencv.face.*;FaceRecognizer fr = Face.createLBPHFaceRecognizer();
berak gravatar imageberak ( 2015-08-06 13:27:47 -0500 )edit

well I'm incapable of step 3.. I did not install visual studio because of it's terms... apparently cmake requires it (on windows) is't there a compiled !

zaguia gravatar imagezaguia ( 2015-08-16 05:11:00 -0500 )edit

" I did not install visual studio" - how did you build the opencv libs then ?

berak gravatar imageberak ( 2015-08-16 07:24:53 -0500 )edit

I did not there was one allready build in the zip of opencv "opencv_java300.dll"..

zaguia gravatar imagezaguia ( 2015-08-17 01:44:58 -0500 )edit