0

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);
        }
    }
}
sohna pathak
  • 37
  • 2
  • 5
  • Try adjusting the default parameters of `detectMultiScale` – ZdaR Jun 23 '17 at 05:54
  • i modified the detectMultiScale as `faceDetector.detectMultiScale(image, faceDetections, double scaleFactor=1.05, int minNeighbors=3, int flags=0, Size minSize = Size(), Size maxSize = Size());` but it shows me an error – sohna pathak Jun 23 '17 at 06:56
  • What is the error ? – ZdaR Jun 23 '17 at 07:15
  • _/home/hduser1/video analytics/mipr/includes_OpenCV/src/main/java/experiments/FaceDetectionOpenCV.java:[51,76] '.class' expected_ , _/home/hduser1/video analytics/mipr/includes_OpenCV/src/main/java/experiments/FaceDetectionOpenCV.java:[51,113] expected_ , _/home/hduser1/video analytics/mipr/includes_OpenCV/src/main/java/experiments/FaceDetectionOpenCV.java:[51,125] ';' expected_, _ /home/hduser1/video analytics/mipr/includes_OpenCV/src/main/java/experiments/FaceDetectionOpenCV.java:[51,154] ';' expected_ – sohna pathak Jun 23 '17 at 11:11
  • @ZdaR i tried as `faceDetector.detectMultiScale(image, faceDetections,1.05, 3, 0, new Size(), new Size())` and it doesn't shows me any error and compile successfully but still am not able to highlight the face in the image – sohna pathak Jun 23 '17 at 12:28
  • Then adjust the values `1.05, 3, 0` to suit your needs, Read the Docs carefully and get the instinct of these variables. – ZdaR Jun 23 '17 at 12:52
  • I tried but still not able to get the highlighted face in the image – sohna pathak Jun 23 '17 at 13:34

0 Answers0