|
|
@ -4,6 +4,8 @@ import org.opencv.core.Core;
|
|
|
|
import org.opencv.core.Mat;
|
|
|
|
import org.opencv.core.Mat;
|
|
|
|
import org.opencv.core.MatOfRect;
|
|
|
|
import org.opencv.core.MatOfRect;
|
|
|
|
import org.opencv.core.Rect;
|
|
|
|
import org.opencv.core.Rect;
|
|
|
|
|
|
|
|
import org.opencv.imgcodecs.Imgcodecs;
|
|
|
|
|
|
|
|
import org.opencv.imgproc.Imgproc;
|
|
|
|
import org.opencv.objdetect.CascadeClassifier;
|
|
|
|
import org.opencv.objdetect.CascadeClassifier;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -18,25 +20,20 @@ public class FaceController {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
FaceController demo = new FaceController();
|
|
|
|
|
|
|
|
demo.run();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create a face detector from the cascade file in the resources directory.
|
|
|
|
// Create a face detector from the cascade file in the resources directory.
|
|
|
|
// 创建识别器
|
|
|
|
// 创建识别器
|
|
|
|
CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/haarcascades/lbpcascade_frontalface.xml").getPath());
|
|
|
|
CascadeClassifier faceDetector = new CascadeClassifier("/src/main/resources/haarcascades/lbpcascade_frontalface.xml");
|
|
|
|
|
|
|
|
|
|
|
|
String imgPath = "/src/main/resources/DetectFace/huge.jpg";
|
|
|
|
String imgPath = "/src/main/resources/DetectFace/AverageMaleFace.jpg";
|
|
|
|
Mat image = null; //Highgui.imread(imgPath,3);
|
|
|
|
Mat image = Imgcodecs.imread(imgPath);
|
|
|
|
|
|
|
|
|
|
|
|
String filename = "D:/DetectFace/huge1.png";
|
|
|
|
Mat dst = new Mat();
|
|
|
|
// System.out.println(String.format("Writing %s", filename));
|
|
|
|
Imgproc.Canny(image, dst, 130, 250);
|
|
|
|
|
|
|
|
|
|
|
|
// Detect faces in the image. MatOfRect is a special container class for Rect.
|
|
|
|
// Detect faces in the image. MatOfRect is a special container class for Rect.
|
|
|
|
MatOfRect faceDetections = new MatOfRect();
|
|
|
|
MatOfRect faceDetections = new MatOfRect();
|
|
|
|
faceDetector.detectMultiScale(image, faceDetections);
|
|
|
|
faceDetector.detectMultiScale(dst, faceDetections);
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println(String.format("识别出 %s 张人脸", faceDetections.toArray().length));
|
|
|
|
System.out.println(String.format("识别出 %s 张人脸", faceDetections.toArray().length));
|
|
|
|
|
|
|
|
|
|
|
@ -48,6 +45,5 @@ public class FaceController {
|
|
|
|
// Save the visualized detection.
|
|
|
|
// Save the visualized detection.
|
|
|
|
// System.out.println(String.format("Writing %s", filename));
|
|
|
|
// System.out.println(String.format("Writing %s", filename));
|
|
|
|
//Highgui.imwrite(filename, image);
|
|
|
|
//Highgui.imwrite(filename, image);
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|