|
@@ -0,0 +1,216 @@
|
|
|
+package com.baidu;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通用文字识别
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+public class GenTest {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重要提示代码中所需工具类
|
|
|
+ * FileUtil,Base64Util,HttpUtil,GsonUtils请从
|
|
|
+ * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
|
|
|
+ * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
|
|
|
+ * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
|
|
|
+ * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
|
|
|
+ * 下载
|
|
|
+ */
|
|
|
+ public static String generalBasic() {
|
|
|
+
|
|
|
+ // 请求url
|
|
|
+ //ip = 内网ip地址 , port = docker中ocr容器映射的端口
|
|
|
+ // 端口号可参考troulbe_shooting.sh返回结果
|
|
|
+// String url = "http://ip:port/GeneralClassifyService/classify";
|
|
|
+ String url = "http://120.48.163.3:8278/GeneralClassifyService/classify";
|
|
|
+ try {
|
|
|
+ // 本地文件路径
|
|
|
+ String filePath = "D:\\SYSTEM\\Desktop\\temp\\aa\\ttt.jpg";
|
|
|
+ byte[] imgData = FileUtil.readFileByBytes(filePath);
|
|
|
+ String imgStr = Base64Util.encode(imgData);
|
|
|
+ //此处需按照接口文档中的参数填写
|
|
|
+ String request_st1 = "object_type=plate&complete_tag=true&version=v1";
|
|
|
+ String data = request_st1 + "&image=" + imgStr;
|
|
|
+ byte[] ddata = data.getBytes();
|
|
|
+ String params = Base64Util.encode(ddata);
|
|
|
+
|
|
|
+ Map<String, Object> map1 = new HashMap<>();
|
|
|
+ map1.put("data", params);
|
|
|
+ String request_str1 = GsonUtils.toJson(map1);
|
|
|
+ String contentType = "application/json";
|
|
|
+ String encoding = "UTF-8";
|
|
|
+ String result = HttpUtil.postGeneralUrl(url, contentType, request_str1, encoding);
|
|
|
+
|
|
|
+ JSONObject jso = new JSONObject(result);
|
|
|
+ String res = jso.getStr("result");
|
|
|
+ byte[] decode = Base64.getDecoder().decode(res.getBytes(StandardCharsets.UTF_8));
|
|
|
+ String str = new String(decode, StandardCharsets.UTF_8);
|
|
|
+ System.err.println(str);
|
|
|
+ JSONObject obj = new JSONObject(str);
|
|
|
+ JSONArray arr = obj.getJSONArray("ret");
|
|
|
+ JSONObject ooo = arr.getJSONObject(0);
|
|
|
+ String number = ooo.getStr("plate_number");
|
|
|
+ String color = ooo.getStr("plate_color");
|
|
|
+ System.err.println(number + "\t" + color);
|
|
|
+// System.out.println();
|
|
|
+
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ batchV5();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void batchV5() throws Exception {
|
|
|
+ String url = "http://120.48.163.3:8278/GeneralClassifyService/classify";
|
|
|
+ File directory = new File("D:\\Project\\2025\\02.成都停车项目POC\\parkimage0606\\parkimage0606");
|
|
|
+ List<PlateVO> successList = new ArrayList<>();
|
|
|
+ List<PlateVO> failList = new ArrayList<>();
|
|
|
+ List<PlateVO> noList = new ArrayList<>();
|
|
|
+ if (directory.isDirectory()) {
|
|
|
+ File[] files = directory.listFiles();
|
|
|
+ if (files != null) {
|
|
|
+ for (File file : files) {
|
|
|
+ String oldName = file.getName();
|
|
|
+ String[] tmp = oldName.split("-");
|
|
|
+ String plateCode = tmp[0].replace(".jpg", "");
|
|
|
+ if (!file.isFile()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ byte[] imgData = FileUtil.readFileByBytes(file.getPath());
|
|
|
+ String imgStr = Base64Util.encode(imgData);
|
|
|
+ //此处需按照接口文档中的参数填写
|
|
|
+ String request_st1 = "object_type=plate&complete_tag=true&prob_tag=true&version=v5";
|
|
|
+ String data = request_st1 + "&image=" + imgStr;
|
|
|
+ byte[] ddata = data.getBytes();
|
|
|
+ String params = Base64Util.encode(ddata);
|
|
|
+
|
|
|
+ Map<String, Object> map1 = new HashMap<>();
|
|
|
+ map1.put("data", params);
|
|
|
+ String request_str1 = GsonUtils.toJson(map1);
|
|
|
+ String contentType = "application/json";
|
|
|
+ String encoding = "UTF-8";
|
|
|
+ String result = HttpUtil.postGeneralUrl(url, contentType, request_str1, encoding);
|
|
|
+
|
|
|
+ JSONObject jso = new JSONObject(result);
|
|
|
+ String res = jso.getStr("result");
|
|
|
+
|
|
|
+ byte[] decode = Base64.getDecoder().decode(res.getBytes(StandardCharsets.UTF_8));
|
|
|
+ String str = new String(decode, StandardCharsets.UTF_8);
|
|
|
+ JSONObject obj = new JSONObject(str).getJSONArray("ret").getJSONObject(0).getJSONObject("ret");
|
|
|
+ if (obj != null) {
|
|
|
+ JSONObject ooo = obj.getJSONArray("plate_number").getJSONObject(0);
|
|
|
+ String word = ooo.getStr("word");
|
|
|
+ String info = ooo.getStr("cover_info");
|
|
|
+ Double score = ooo.getDouble("det_score");
|
|
|
+ String color = ooo.getStr("plate_color");
|
|
|
+ PlateVO vo = new PlateVO();
|
|
|
+ vo.setName(plateCode);
|
|
|
+ if (StringUtils.isEmpty(word)) {
|
|
|
+ String o = file.getPath();
|
|
|
+ String n = "D:\\Project\\2025\\02.成都停车项目POC\\test\\no\\" + file.getName();
|
|
|
+ copy(o, n);
|
|
|
+ noList.add(vo);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ vo.setPlate(word);
|
|
|
+ vo.setScore(score);
|
|
|
+ vo.setColor(color);
|
|
|
+
|
|
|
+ if (word.equals(plateCode)) {
|
|
|
+ String o = file.getPath();
|
|
|
+ String n = "D:\\Project\\2025\\02.成都停车项目POC\\test\\success\\" + file.getName();
|
|
|
+ copy(o, n);
|
|
|
+ successList.add(vo);
|
|
|
+ } else {
|
|
|
+ String o = file.getPath();
|
|
|
+ String n = "D:\\Project\\2025\\02.成都停车项目POC\\test\\fail\\" + file.getName();
|
|
|
+ copy(o, n);
|
|
|
+ failList.add(vo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println(new String(decode, StandardCharsets.UTF_8));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ toExcel(successList, failList, noList);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void toExcel(List<PlateVO> successList, List<PlateVO> failList, List<PlateVO> noList) {
|
|
|
+ try (Workbook workbook = new XSSFWorkbook(); FileOutputStream outputStream = new FileOutputStream("D:\\Project\\2025\\02.成都停车项目POC\\test\\车牌识别结果" + System.currentTimeMillis() + ".xlsx")) {
|
|
|
+ String[] names = {"图片车牌", "图片地址", "识别出的车牌", "车牌颜色", "置信度"};
|
|
|
+ Sheet sheet = workbook.createSheet();
|
|
|
+ workbook.setSheetName(0, "正确识别");
|
|
|
+ sheet(sheet, names, successList);
|
|
|
+ Sheet sheet1 = workbook.createSheet();
|
|
|
+ workbook.setSheetName(1, "错误识别");
|
|
|
+ sheet(sheet1, names, failList);
|
|
|
+ Sheet sheet2 = workbook.createSheet();
|
|
|
+ workbook.setSheetName(2, "未识别");
|
|
|
+ sheet(sheet2, names, noList);
|
|
|
+ workbook.write(outputStream);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void sheet(Sheet sheet, String[] names, List<PlateVO> list) {
|
|
|
+ Row title = sheet.createRow(0);
|
|
|
+ for (int i = 0; i < names.length; i++) {
|
|
|
+ Cell cell = title.createCell(i);
|
|
|
+ cell.setCellValue(names[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ AtomicInteger index = new AtomicInteger(1);
|
|
|
+ list.forEach(vo -> {
|
|
|
+ Row row = sheet.createRow(index.get());
|
|
|
+ Cell[] cells = new Cell[names.length];
|
|
|
+ for (int i = 0; i < names.length; i++) {
|
|
|
+ cells[i] = row.createCell(i);
|
|
|
+ }
|
|
|
+ cells[0].setCellValue(vo.getName());
|
|
|
+ if (vo.getScore() != null) {
|
|
|
+ cells[2].setCellValue(vo.getPlate());
|
|
|
+ cells[3].setCellValue(vo.getColor());
|
|
|
+ cells[4].setCellValue(vo.getScore());
|
|
|
+ }
|
|
|
+ index.getAndIncrement();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void copy(String o, String n) {
|
|
|
+ Path oldPath = Paths.get(o);
|
|
|
+ Path newPath = Paths.get(n);
|
|
|
+
|
|
|
+ try {
|
|
|
+ Files.copy(oldPath, newPath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|