Face alignment using openImaj API libraries

786 views Asked by At

I want to align several faces I have at my disposal here using openImaj. I want to read a jpg face photo, align it and finally save it as in jpg after alignment. Here is where I am stuck. See below

     public class FaceImageAlignment {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here

        BufferedImage img = null;
        img = ImageIO.read(new File("D:/face_test.jpg"));

        //How to align face image using openImaj
        //This is where I am stuck on doing face alignment. I tried doing the following
        AffineAligner imgAlign = new AffineAligner();
        //but I could not figure out how to do face alignment with it



        BufferedImage imgAligned = new BufferedImage(//I will need to put aligned Image here as a BufferedImage);
        File f = new File("D:\\face_aligned.jpg");
        ImageIO.write(imgAligned, "JPEG", f);

    }
}

What Code do I need to have there to face align face_test.jpg to face_aligned.jpg ?

1

There are 1 answers

3
Jon On

Aligners work in combination with face detectors, so you need to use a detector to find the face(s) and then pass that to the aligner. Different aligners are tied to different detector implementations as they require different information to perform the alignment; for example the affine aligner needs facial key points found by the FKEFaceDetector. Basic code looks something like this:

FImage img = ImageUtilities.readF(new File("..."));
FKEFaceDetector detector = new FKEFaceDetector();
FaceAligner<KEDetectedFace> aligner = new AffineAligner();
KEDetectedFace face = detector.detectFaces(img).get(0);
FImage alignedFace = aligner.align(face);
ImageUtilities.write(alignedFace, new File("aligned.jpg"));