I am not able to highlight the face in the image in my face detection application. I am running this face detection in the hadoop environment. I tried both haarcascade classifier and lbpcascade classifier but not able to detect and highlight the face. Code is as follow:
public class FaceDetectionOpenCV {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String input = args[0];
String output = args[1];
Job job = new MiprConfigurationParser().getOpenCVJobTemplate();
job.setJarByClass(FaceDetectionOpenCV.class);
job.setMapperClass(FaceDetectorMapper.class);
job.setInputFormatClass(MatImageInputFormat.class);
job.setOutputFormatClass(MatImageOutputFormat.class);
Path outputPath = new Path(output);
FileInputFormat.setInputPaths(job, input);
FileOutputFormat.setOutputPath(job, outputPath);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(MatImageWritable.class);
job.waitForCompletion(true);
}
public static class FaceDetectorMapper extends OpenCVMapper<NullWritable, MatImageWritable, NullWritable, MatImageWritable> {
@Override
protected void map(NullWritable key, MatImageWritable value, Context context) throws IOException, InterruptedException {
Mat image = value.getImage();
if (image != null) {
//CascadeClassifier faceDetector = new CascadeClassifier("lbpcascade_frontalface.xml");
CascadeClassifier faceDetector = new CascadeClassifier("haarcascade_frontalface_default.xml");
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
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),5);
}
MatImageWritable matiw = new MatImageWritable(image);
matiw.setFormat("jpg");
matiw.setFileName(value.getFileName() + "_result");
context.write(NullWritable.get(), matiw);
}
}
}