Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples for Facemark algorithms #1030

Merged
merged 8 commits into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions samples/KazemiFacemarkExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Kazemi Facemark example for JavaCV
*
* @author Théophile Gonos
*
* Link to Kazemi model :
* https://raw.githubusercontent.com/opencv/opencv_3rdparty/contrib_face_alignment_20170818/face_landmark_model.dat
*/

import java.io.IOException;
import java.net.URISyntaxException;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Point2fVector;
import org.bytedeco.javacpp.opencv_core.Point2fVectorVector;
import org.bytedeco.javacpp.opencv_core.RectVector;
import org.bytedeco.javacpp.opencv_core.Scalar;
import org.bytedeco.javacpp.opencv_face.FacemarkKazemi;
import static org.bytedeco.javacpp.opencv_face.drawFacemarks;
import static org.bytedeco.javacpp.opencv_highgui.cvWaitKey;
import static org.bytedeco.javacpp.opencv_highgui.imshow;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;
import static org.bytedeco.javacpp.opencv_imgcodecs.imwrite;
import static org.bytedeco.javacpp.opencv_imgproc.COLOR_BGR2GRAY;
import static org.bytedeco.javacpp.opencv_imgproc.cvtColor;
import static org.bytedeco.javacpp.opencv_imgproc.equalizeHist;
import org.bytedeco.javacpp.opencv_objdetect.CascadeClassifier;


public class KazemiFacemarkExample {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
// Load Face Detector
CascadeClassifier faceDetector = new CascadeClassifier ("haarcascade_frontalface_alt2.xml");

// Create an instance of Facemark
FacemarkKazemi facemark = FacemarkKazemi.create();

// Load landmark detector
facemark.loadModel("face_landmark_model.dat");

// Load image
Mat img = imread("face.jpg");

// convert to grayscale and equalize histograe for better detection
Mat gray = new Mat ();
cvtColor(img, gray, COLOR_BGR2GRAY);
equalizeHist( gray, gray );

// Find faces on the image
RectVector faces = new RectVector ();
faceDetector.detectMultiScale(gray, faces);

System.out.println ("Faces detected: "+faces.size());
// Variable for landmarks.
// Landmarks for one face is a vector of points
// There can be more than one face in the image.
Point2fVectorVector landmarks = new Point2fVectorVector();

// Run landmark detector
boolean success = facemark.fit(img, faces, landmarks);

if(success) {
// If successful, render the landmarks on each face
for (long i = 0; i < landmarks.size(); i++) {
Point2fVector v = landmarks.get(i);
drawFacemarks(img, v, Scalar.YELLOW);
}
}

// Display results
imshow("Kazemi Facial Landmark", img);
cvWaitKey(0);
// Save results
imwrite ("kazemi_landmarks.jpg", img);
}
92 changes: 92 additions & 0 deletions samples/LBFFacemarkExampleWithVideo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* LBF Facemark example for JavaCV with Video camera and Transparent API
*
* @author Théophile Gonos
*
* you can find the lbfmodel here:
* https://raw.githubusercontent.com/kurnianggoro/GSOC2017/master/data/lbfmodel.yaml
*/

import java.io.IOException;
import java.net.URISyntaxException;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Point2fVector;
import org.bytedeco.javacpp.opencv_core.Point2fVectorVector;
import org.bytedeco.javacpp.opencv_core.RectVector;
import org.bytedeco.javacpp.opencv_core.Scalar;
import org.bytedeco.javacpp.opencv_core.UMat;
import org.bytedeco.javacpp.opencv_face.Facemark;
import org.bytedeco.javacpp.opencv_face.FacemarkLBF;
import static org.bytedeco.javacpp.opencv_face.drawFacemarks;
import static org.bytedeco.javacpp.opencv_highgui.imshow;
import static org.bytedeco.javacpp.opencv_highgui.waitKey;
import static org.bytedeco.javacpp.opencv_imgproc.COLOR_BGR2GRAY;
import static org.bytedeco.javacpp.opencv_imgproc.cvtColor;
import static org.bytedeco.javacpp.opencv_imgproc.equalizeHist;
import org.bytedeco.javacpp.opencv_objdetect.CascadeClassifier;
import org.bytedeco.javacpp.opencv_videoio.VideoCapture;

public class LBFFacemarkExampleWithVideo {

/**
* @param args the command line arguments
* @throws java.io.IOException
* @throws java.net.URISyntaxException
* @throws java.lang.InterruptedException
*/
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
// Load Face Detector
CascadeClassifier faceDetector = new CascadeClassifier ("haarcascade_frontalface_alt2.xml");

// Create an instance of Facemark
Facemark facemark = FacemarkLBF.create();

// Load landmark detector
facemark.loadModel("lbfmodel.yaml");

// Set up webcam for video capture
VideoCapture cam = new VideoCapture (0);
// Variable to store a video frame and its grayscale
Mat frame = new Mat ();

// Read a frame
while(cam.read(frame)) {
// convert to grayscale and equalize histograe for better detection
// + use of transparent API
UMat gray = new UMat ();
frame.copyTo(gray);
cvtColor(gray, gray, COLOR_BGR2GRAY);
equalizeHist( gray, gray );

// Find faces on the image
RectVector faces = new RectVector ();
faceDetector.detectMultiScale(gray, faces);

System.out.println ("Faces detected: "+faces.size());
// Verify is at least one face is detected
// With some Facemark algorithms it crashes if there is no faces
if (!faces.empty()) {

// Variable for landmarks.
// Landmarks for one face is a vector of points
// There can be more than one face in the image.
Point2fVectorVector landmarks = new Point2fVectorVector();

// Run landmark detector
boolean success = facemark.fit(frame, faces, landmarks, null);

if(success) {
// If successful, render the landmarks on the face
for (long i = 0; i < landmarks.size(); i++) {
Point2fVector v = landmarks.get(i);
drawFacemarks(frame, v, Scalar.YELLOW);
}
}
}
// Display results
imshow("LBF Facial Landmark", frame);
// Exit loop if ESC is pressed
if (waitKey(1) == 27) break;
}
}
}
Binary file added samples/face.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading