代码解释: 我想对100个车牌图片进行识别,怎么做public static void main(String[] args) { // 多张车牌图片路径 String[] imgPaths = {"res/image/test_image/plate_locate.jpg", "res/image/test_image/test.jpg", "res/image/test_image/plate_detect.jpg", "res/general_test/京A88731.jpg"}; int sum = imgPaths.length; // 总共处理的图片数量 int errNum = 0; // 识别错误的数量 int sumTime = 0; // 总耗时 long longTime = 0; // 最长处理时长 for (int i = 0; i < sum; i++) { opencv_core.Mat src = opencv_imgcodecs.imread(imgPaths[i]); String[] ret = multiPlateRecognise(src); long now = System.currentTimeMillis(); System.err.println(Arrays.toString(ret)); long s = System.currentTimeMillis() - now; if (s > longTime) { longTime = s; } sumTime += s; boolean flag =false;//是否有一个车牌号识别错误 for (String plate:ret) { if (plate != null && !"苏EUK722".equals(plate) && !"苏A0CP56".equals(plate) && !"沪BS7821".equals(plate) && !"京A88731".equals(plate)) { flag = true; break; } } if (flag) { errNum++; } } BigDecimal errSum = new BigDecimal(errNum); BigDecimal sumNum = new BigDecimal(sum); BigDecimal c = sumNum.subtract(errSum).divide(sumNum,2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)); System.err.println("总耗时:" + sumTime + "ms,平均处理时长:" + sumTime/sum + "ms,错误数量:" + errNum + ",正确识别率:" + c + "%"); } }
时间: 2024-02-14 13:21:15 浏览: 60
这段代码实现了对100个车牌图片进行识别的功能。首先定义了一个包含多张车牌图片路径的字符串数组imgPaths,然后通过循环遍历数组中的每张图片,分别调用multiPlateRecognise方法进行识别,并记录处理时间、错误数量等信息。最后计算出正确识别率并打印输出。其中BigDecimal类用于处理精度问题,RoundingMode.HALF_UP表示四舍五入。
相关问题
我想对100个车牌图片进行识别,main函数怎么改 public static void main(String[] args) { // 多张车牌图片路径 String[] imgPaths = {"res/image/test_image/plate_locate.jpg", "res/image/test_image/test.jpg", "res/image/test_image/plate_detect.jpg", "res/general_test/京A88731.jpg"}; int sum = imgPaths.length; // 总共处理的图片数量 int errNum = 0; // 识别错误的数量 int sumTime = 0; // 总耗时 long longTime = 0; // 最长处理时长 for (int i = 0; i < sum; i++) { opencv_core.Mat src = opencv_imgcodecs.imread(imgPaths[i]); String[] ret = multiPlateRecognise(src); long now = System.currentTimeMillis(); System.err.println(Arrays.toString(ret)); long s = System.currentTimeMillis() - now; if (s > longTime) { longTime = s; } sumTime += s; boolean flag =false;//是否有一个车牌号识别错误 for (String plate:ret) { if (plate != null && !"苏EUK722".equals(plate) && !"苏A0CP56".equals(plate) && !"沪BS7821".equals(plate) && !"京A88731".equals(plate)) { flag = true; break; } } if (flag) { errNum++; } } BigDecimal errSum = new BigDecimal(errNum); BigDecimal sumNum = new BigDecimal(sum); BigDecimal c = sumNum.subtract(errSum).divide(sumNum,2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)); System.err.println("总耗时:" + sumTime + "ms,平均处理时长:" + sumTime/sum + "ms,错误数量:" + errNum + ",正确识别率:" + c + "%"); } }
如果想对100个车牌图片进行识别,可以修改imgPaths数组中的路径,将其替换为100张车牌图片的路径,例如:
```
String[] imgPaths = {"res/image/test_image/plate_locate.jpg", "res/image/test_image/test.jpg", "res/image/test_image/plate_detect.jpg", "res/general_test/京A88731.jpg", "path/to/image1.jpg", "path/to/image2.jpg", ..., "path/to/image100.jpg"};
```
然后在循环中,将sum的值修改为100即可:
```
int sum = 100; // 总共处理的图片数量
```
运行程序即可对100张车牌图片进行识别并输出结果。
我想对100个车牌图片进行识别,并计算出正确识别率,代码应该怎么修改public static String[] multiPlateRecognise(opencv_core.Mat mat) { PlateDetect plateDetect = new PlateDetect(); plateDetect.setPDLifemode(true); Vector<opencv_core.Mat> matVector = new Vector<opencv_core.Mat>(10); if (0 == plateDetect.plateDetect(mat, matVector)) { CharsRecognise cr = new CharsRecognise(); String[] results = new String[matVector.size()]; for (int i = 0; i < matVector.size(); ++i) { String result = cr.charsRecognise(matVector.get(i)); results[i] = result; } return results; } return null; } public static void main(String[] args) { // 多张车牌图片路径 String[] imgPaths = {"res/image/test_image/plate_locate.jpg", "res/image/test_image/test.jpg", "res/image/test_image/plate_detect.jpg", "res/general_test/京A88731.jpg"}; int sum = imgPaths.length; // 总共处理的图片数量 int errNum = 0; // 识别错误的数量 int sumTime = 0; // 总耗时 long longTime = 0; // 最长处理时长 for (int i = 0; i < sum; i++) { opencv_core.Mat src = opencv_imgcodecs.imread(imgPaths[i]); String[] ret = multiPlateRecognise(src); long now = System.currentTimeMillis(); System.err.println(Arrays.toString(ret)); long s = System.currentTimeMillis() - now; if (s > longTime) { longTime = s; } sumTime += s; boolean flag =false;//是否有一个车牌号识别错误 for (String plate:ret) { if (plate != null && !"苏EUK722".equals(plate) && !"苏A0CP56".equals(plate) && !"沪BS7821".equals(plate) && !"京A88731".equals(plate)) { flag = true; break; } } if (flag) { errNum++; } } BigDecimal errSum = new BigDecimal(errNum); BigDecimal sumNum = new BigDecimal(sum); BigDecimal c = sumNum.subtract(errSum).divide(sumNum,2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)); System.err.println("总耗时:" + sumTime + "ms,平均处理时长:" + sumTime/sum + "ms,错误数量:" + errNum + ",正确识别率:" + c + "%"); } }
您可以将代码修改为以下形式:
```java
public static void main(String[] args) {
String[] imgPaths = {"res/image/test_image/plate_locate.jpg", "res/image/test_image/test.jpg", "res/image/test_image/plate_detect.jpg", "res/general_test/京A88731.jpg"};
int sum = imgPaths.length;
int errNum = 0;
int sumTime = 0;
long longTime = 0;
for (int i = 0; i < sum; i++) {
opencv_core.Mat src = opencv_imgcodecs.imread(imgPaths[i]);
String[] ret = multiPlateRecognise(src);
long now = System.currentTimeMillis();
System.err.println(Arrays.toString(ret));
long s = System.currentTimeMillis() - now;
if (s > longTime) {
longTime = s;
}
sumTime += s;
boolean flag = false;
for (String plate : ret) {
if (plate == null) {
continue;
}
String targetPlate = getTargetPlate(imgPaths[i]);
if (!plate.equals(targetPlate)) {
flag = true;
break;
}
}
if (flag) {
errNum++;
}
}
BigDecimal errSum = new BigDecimal(errNum);
BigDecimal sumNum = new BigDecimal(sum);
BigDecimal c = sumNum.subtract(errSum).divide(sumNum, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
System.err.println("总耗时:" + sumTime + "ms,平均处理时长:" + sumTime / sum + "ms,错误数量:" + errNum + ",正确识别率:" + c + "%");
}
private static String getTargetPlate(String imgPath) {
if (imgPath.contains("苏EUK722")) {
return "苏EUK722";
} else if (imgPath.contains("苏A0CP56")) {
return "苏A0CP56";
} else if (imgPath.contains("沪BS7821")) {
return "沪BS7821";
} else if (imgPath.contains("京A88731")) {
return "京A88731";
} else {
return null;
}
}
public static String[] multiPlateRecognise(opencv_core.Mat mat) {
PlateDetect plateDetect = new PlateDetect();
plateDetect.setPDLifemode(true);
Vector<opencv_core.Mat> matVector = new Vector<opencv_core.Mat>(10);
if (0 == plateDetect.plateDetect(mat, matVector)) {
CharsRecognise cr = new CharsRecognise();
String[] results = new String[matVector.size()];
for (int i = 0; i < matVector.size(); ++i) {
String result = cr.charsRecognise(matVector.get(i));
results[i] = result;
}
return results;
}
return null;
}
```
其中,`getTargetPlate` 方法根据图片路径返回对应的正确车牌号。在 `main` 方法中,对每张图片的识别结果进行判断,如果与正确车牌号不符,则将错误数量加一。最后计算正确识别率并输出。
注意,您需要根据实际情况修改 `getTargetPlate` 方法中的车牌号。
阅读全文