上篇写了人脸识别,因为人脸识别的训练模型 haarcascade_frontalface_alt.xml 之类的官方已经训练好了可以直接用,但是我们要识别车辆或者其它物体就得训练模型,好在废了一点力 找到了一位大神训练好的模型

?

?核心代码 几乎和人脸识别的差不多

static CascadeClassifier carDetector;
static {
try {
NativeLoader.loader();
String path = System.getProperty("user.dir").concat("/classifier/car/cars.xml");
carDetector = new CascadeClassifier(path);
if (carDetector.empty()) {
System.out.println("cars.xml 文件加载失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Mat image = Imgcodecs.imread("D:/rr/car5.jpg");
Mat hvs = new Mat();
Imgproc.cvtColor(image,hvs,Imgproc.COLOR_BGR2GRAY);
MatOfRect faceDetections = new MatOfRect();
carDetector.detectMultiScale(image,faceDetections);
System.out.println("监测到"+faceDetections.toArray().length+"辆车");
for (Rect rect : faceDetections.toArray()) {
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
Imgcodecs.imwrite("D:/rr/1-55.jpg", image);
HighGui.imshow("车辆识别", image);
System.exit(0);
?可以看到大部分车辆能识别到,估计是训练样本选的不够多得到的模型不过已经很棒了
付一个连接?级联分类器训练?
级联分类器训练 — OpenCV 2.3.2 documentation
|