我无法在人脸检测应用程序中突出显示图像中的人脸。我正在hadoop环境中运行这个人脸检测。我尝试了haarcascade分类器和lbpcascade分类器,但都无法检测和突出显示人脸。代码如下:
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);
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!