The record about the outcome of today's study, scala use opencv identify the vehicle interception pictures and counting, of course, we can identify a variety of items go through their own training model, a great help to make training machine learning set, put the key code for all to study reference
Realization of ideas
- Use opencv training vehicle identifier
- Enter a folder traversal of all images, use opencv recognition to all vehicles
- Each picture in the vehicle interception saved
- Each one of the figures vehicles and all counts
Key Code:
def PlDeal(filepath:String , outpath:String): Unit ={
//分别对应输入输出路径
val file = new File(filepath)
val outfile = new File(outpath)
//判断输出目录是否存在
if(!outfile.exists()){
println("文件不存在,开始创建.....")
outfile.mkdir()
println("文件创建成功!")
}
else {
outfile.delete()
println("文件删除完毕!")
}
//判断输入目录是否能够正确打开
if(!file.isDirectory){
System.out.println("请输入正确的文件夹路径")
}
else if(file.isDirectory){
val filelist = file.list
var sum=0
//遍历输入文件夹中的所有图片
for( i <- 0 until filelist.length){
val imgname = filelist(i)
val image : Mat = Imgcodecs.imread(filepath + "/" + imgname)
//图片检测
val vehicleDetections : MatOfRect = new MatOfRect()
//车辆识别的方法
vehicleDetecter.detectMultiScale(image,vehicleDetections)
val rects = vehicleDetections.toArray
println("第"+(i+1)+"次识别的数量"+rects.length)
for (rect <- rects){
val sub: Mat = image.submat(rect)
Imgproc.rectangle(image, new Point(rect.x-2, rect.y-2), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0))
Imgcodecs.imwrite(outpath+"/"+System.currentTimeMillis()+".jpg",sub)
}
sum += vehicleDetections.toArray().length
}
println("总共识别车的数量"+sum)
}
}
operation result:
Console output:
Experimental Image:
screenshot:
Summarizes the advantages and disadvantages
Pros: Simple idea, use opencv own identifier identifying identification code without having to write a lot of
drawbacks: the recognition accuracy is not enough, although he trained model compared to the model that comes with the recognition accuracy has greatly improved, but compared to the use of neural networks or machines learn to identify accuracy or a lot of difference