Search code examples
javaopencvface-recognition

How to recognized face which are already detected and saved is local system as .jpg?


I have used OpenCV, JavaFX to detect and save the face image in local folder. Now I want to recognized face if already saved in local system. I need a some guidance and some sample code or links so I can move further.

For saving face

Rect rectCrop = null;
    int i =1;
    for (Rect rect : facesArray) {
        Imgproc.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                new Scalar(0, 255, 0));
        rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);

        Mat image_roi = new Mat(frame,rectCrop);
         Imgcodecs.imwrite("./face"+ i +".jpg",image_roi);
         i++;
    }

enter image description here


Solution

  • try below code using openCV 3.4

    static Mat processFace(Net net, Mat img) {
    Mat blob = Dnn.blobFromImage(img, 1./255, new Size(96,96), Scalar.all(0), 
    true, false);
    net.setInput(blob);
    return net.forward().clone();
    }
    
      public static void main(String[] args) {
    // Load the native library.
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    
    Net net  = Dnn.readNetFromTorch("openface.nn4.small2.v1.t7");
    
    Mat img1 = Imgcodecs.imread("../img/face1.png");
    Mat img2 = Imgcodecs.imread("../img/face2.jpg");
    
    Mat feature1 = processFace(net, img1);
    Mat feature2 = processFace(net, img2);
    double distance = Core.norm(feature1, feature2);
    System.out.println("distance: " + distance);
    }