1、我转换的是表格,谢天鞋底有个接口叫:

表格文字识别接口

自动识别表格线及表格内容,结构化输出表头、表尾及每个单元格的文字内容。

表格文字识别接口为异步接口,分为两个API:提交请求接口、获取结果接口。

需要注意两点:

1、只有每天50的免费使用量

2、特别注意:表格【边框】必须有【实线】,excel的那种线也是不可识别

3、背景干净最好,这样识别率比较高

4、数字的、和,是分不清的
5、长图片不可识别

我实现的代码,有大神觉得不好的地方,万万指出:

 @Controller
public class OCRController extends BaseController {private static final Log logger = LogFactory.getLog(OCRController.class);public JSONObject sample(AipOcr client, String image) {// 传入可选参数调用接口HashMap<String, String> options = new HashMap<String, String>();// options.put("recognize_granularity", "big");options.put("detect_direction", "true");// options.put("vertexes_location", "true");// 参数为本地路径JSONObject res = client.basicAccurateGeneral(image, options);return res;}public JSONObject sample1(AipOcr client, String image) {// 传入可选参数调用接口HashMap<String, String> options = new HashMap<String, String>();// 参数为本地路径JSONObject res = client.form(image, options);return res;}public static JSONObject sample2(AipOcr client, String image) {// 传入可选参数调用接口HashMap<String, String> options = new HashMap<String, String>();// 参数为本地路径JSONObject res = client.tableRecognitionAsync(image, options);return res;/** // 参数为二进制数组 byte[] file = readFile("test.jpg"); res =* client.tableRecognitionAsync(file, options);* System.out.println(res.toString(2));*/}public static JSONObject sample3(AipOcr client, String requestId) {// 传入可选参数调用接口HashMap<String, String> options = new HashMap<String, String>();options.put("result_type", "json");// 表格识别结果JSONObject res = client.tableResultGet(requestId, options);return res;}public static void main(String[] args) throws JSONException {// 初始化一个AipOcrAipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);// 可选:设置网络连接参数client.setConnectionTimeoutInMillis(2000);client.setSocketTimeoutInMillis(60000);// 调用接口String path = "d:\\a2.png";Map<String, Object> json = new HashMap<String, Object>();String reqestId = getReqestId(json, path);System.out.println(reqestId);System.out.println("===================1==============");net.sf.json.JSONArray ja = new net.sf.json.JSONArray();net.sf.json.JSONArray headerArr = new net.sf.json.JSONArray();imageTransformation(ja, headerArr, reqestId, "20190101-天马.png");System.out.println("==================2===============");}private void drawRect(String path) {File _file = new File(path); // 读入文件try {// 构造Image对象Image src = javax.imageio.ImageIO.read(_file);int width = src.getWidth(null); // 得到源图宽int height = src.getHeight(null); // 得到源图长BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics graphics = image.getGraphics();graphics.drawImage(src, 0, 0, width, height, null); // 绘制图// 背景那么干净,取灰度,找个阈值,二值化一下,然后腐蚀,系数调大点,寻找黑色区域的轮廓的外接正矩形// 画边框graphics.setColor(Color.BLACK);graphics.drawRect(1, 0, width - 1, height - 1);// graphics.drawRect(1, 1, width - 1, height - 1);// graphics.drawRect(0, 0, width-2, height- 2);FileOutputStream out = new FileOutputStream(path); // 输出到文件流JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(image);out.close();} catch (IOException e) {e.printStackTrace();}}private void delFile(String pic, String name) {logger.info("============================删除原文件3个:" + pic + "=========================");// 删除原文件3个String prepareForUpload ="";String filename ="";String[] arr = filename.split(".");if (arr != null && arr.length > 1) {if ("0".equals(name)) {filename = arr[0].substring(0, arr[0].length() - 1) + "2" + arr[1];logger.info("============================路径名+文件2号图=删除文件:" + filename + "=========================");UploadHelper.delTempFile(filename, prepareForUpload);filename = arr[0].substring(0, arr[0].length() - 1) + "1" + arr[1];logger.info("============================路径名+文件1号图=删除文件:" + filename + "=========================");UploadHelper.delTempFile(filename, prepareForUpload);}filename = arr[0].substring(0, arr[0].length() - 1) + arr[1];logger.info("============================路径名+文件原图=删除文件:" + filename + "=========================");UploadHelper.delTempFile(filename, prepareForUpload);}}private static Map<String, Object> imageTransformation(net.sf.json.JSONArray ja, net.sf.json.JSONArray headerArr, String requestId, String fileName)throws JSONException {Map<String, Object> map = new HashMap<String, Object>();List<String> worng = new ArrayList<String>();String str = toRequestOCR(requestId);if (StringUtils.isBlank(str)) {map.put("code", "数据为空");return map;}List<OcrDTO> list = getList(str, fileName);getHeader(headerArr, list);return map;}/*** 获取主数据* * @param ja* @param list*/private static List<String> getBody(net.sf.json.JSONArray ja, List<OcrDTO> list) {logger.info("=======================获取主数据===========================");List<OcrDTO> t = new ArrayList<OcrDTO>();for (int i = 0; i < list.size(); i++) {t.add(list.get(i));}for (int i = 0; i < list.size() - 1; i++) {for (int j = list.size() - 1; j > i; j--) {if (list.get(j).getColumn() == (list.get(i).getColumn())) {list.remove(j);}}}net.sf.json.JSONObject column = new net.sf.json.JSONObject();for (int i = 0; i < list.size(); i++) {net.sf.json.JSONArray rowJsonArray = new net.sf.json.JSONArray();Map<String, Integer> m = new HashMap<String, Integer>();for (int j = 0; j < t.size(); j++) {if (list.get(i).getColumn() == t.get(j).getColumn() && list.get(i).getRow() != t.get(j).getRow()) {column.put("row", t.get(j).getRow());column.put("name", t.get(j).getWord());column.put("flag", 0);if (!m.containsKey("row" + t.get(0).getRow())) {m.put("row" + t.get(j).getRow(), t.get(j).getRow());rowJsonArray.add(column);}}}ja.add(rowJsonArray);}List<String> wrong = new ArrayList<String>();for (int i = 0; i < ja.size(); i++) {net.sf.json.JSONArray arr = (net.sf.json.JSONArray) ja.get(i);wrong = isWrong(arr);logger.info("=========================for:wrong" + wrong + "============================");if (CollectionUtils.isNotEmpty(wrong)) {for (int j = 0; j < wrong.size(); j++) {net.sf.json.JSONObject obj = (net.sf.json.JSONObject) arr.get(Integer.parseInt(wrong.get(j)));obj.put("flag", 1);}break;}}logger.info("=========================wrong" + wrong + "============================");return wrong;}/*** 获取头部信息* * @param headerArr* @param list*/private static void getHeader(net.sf.json.JSONArray headerArr, List<OcrDTO> list) {net.sf.json.JSONObject head = new net.sf.json.JSONObject();net.sf.json.JSONArray harr = new net.sf.json.JSONArray();for (int i = 0; i < list.size(); i++) {if (list.get(i).getRow() == 0) {harr.add(list.get(i).getWord());}}/** if (null != harr) { harr.add("日期"); harr.add("类目"); }*/head.put("head", harr);headerArr.add(head);}/*** 获取所有的数据,并将数据存到list* * @param str* @return*/private static List<OcrDTO> getList(String str, String fileName) {List<OcrDTO> list = new ArrayList<OcrDTO>();net.sf.json.JSONObject fromObject = net.sf.json.JSONObject.fromObject(str);net.sf.json.JSONArray jsonArray = fromObject.getJSONArray("forms");net.sf.json.JSONArray jsonbody = null;for (int i = 0; i < jsonArray.size(); i++) {net.sf.json.JSONObject json1 = (net.sf.json.JSONObject) jsonArray.get(i);jsonbody = json1.getJSONArray("body");}for (int i = 0; i < jsonbody.size(); i++) {net.sf.json.JSONObject json1 = (net.sf.json.JSONObject) jsonbody.get(i);String column = json1.getString("column");String row = json1.getString("row");String word = json1.getString("word");OcrDTO ocr = new OcrDTO(Integer.parseInt(row.substring(1, row.length() - 1)), Integer.parseInt(column.substring(1, column.length() - 1)), word);list.add(ocr);}int num = 0;int colum = 0;for (int i = 0; i < jsonbody.size(); i++) {net.sf.json.JSONObject json1 = (net.sf.json.JSONObject) jsonbody.get(i);String column = json1.getString("column");int col = Integer.parseInt(column.substring(1, column.length() - 1));if (col == 0) {num += 1;}colum = col;if (col > colum) {colum = col;}}String[] arr = fileName.split("-");for (int i = 0; i < num; i++) {if (i == 0) {OcrDTO ocr = new OcrDTO(i, colum + 2, "日期");list.add(ocr);continue;}String word = arr[0];OcrDTO ocr = new OcrDTO(i, colum + 2, word);list.add(ocr);}for (int i = 0; i < num; i++) {if (i == 0) {OcrDTO ocr = new OcrDTO(i, colum + 3, "类目");list.add(ocr);continue;}String word = arr[1].substring(0, arr[1].length() - 4);OcrDTO ocr = new OcrDTO(i, colum + 3, word);list.add(ocr);}Collections.sort(list, new Comparator<OcrDTO>() {@Overridepublic int compare(OcrDTO o1, OcrDTO o2) {if (o1.getColumn() == o2.getColumn()) {return o1.getRow() - (o2.getRow());}return o1.getColumn() - o2.getColumn();}});//logger.info("=====================list:" + list + "========================");return list;}/*** 开始请求ocr接口,获取返回* * @param requestId* @return* @throws JSONException*/private static String toRequestOCR(String requestId) throws JSONException {String[] arr = requestId.split(" ");AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);if (arr[0].equals("1")) {client = new AipOcr(APP_ID1, API_KEY1, SECRET_KEY1);}if (arr[0].equals("2")) {client = new AipOcr(APP_ID2, API_KEY2, SECRET_KEY2);}if (arr[0].equals("3")) {client = new AipOcr(APP_ID3, API_KEY3, SECRET_KEY3);}if (arr[0].equals("4")) {client = new AipOcr(APP_ID4, API_KEY4, SECRET_KEY4);}requestId = arr[1];JSONObject res = sample3(client, requestId);String err = res.toString(2);if (!res.has("result")) {res = isb(client, requestId);}if (res.toString().contains("error_msg")) {logger.info("=====================err" + err + "========================");return null;}if (!res.has("result")) {return null;}String str = cycleRequest(requestId, client, err, res);return str;}/*** 假如报错,循环请求* * @param client* @param requestId* @return*/private static JSONObject isb(AipOcr client, String requestId) {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}JSONObject res1 = sample3(client, requestId);if (res1.toString().contains("error_msg")) {boolean f = true;int i = 0;while (f) {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}i += 1;res1 = sample3(client, requestId);logger.info("假如报错,循环请求" + res1.toString());if (!res1.toString().contains("error_msg")) {f = false;}if (i > 5) {f = false;}}}return res1;}/*** 假如未完成,继续请求* * @param requestId* @param client* @param err* @param json* @return* @throws JSONException*//*** @param requestId* @param client* @param err* @param res* @return* @throws JSONException*/private static String cycleRequest(String requestId, AipOcr client, String err, JSONObject res) throws JSONException {JSONObject json = res.getJSONObject("result");logger.info("=====================假如未完成,继续请求err" + err + "========================");int num = 0;while (true) {num += 1;String str = json.getString("ret_msg");if ("已完成".equals(str)) {break;}try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}res = sample3(client, requestId);// 假如错误的if (res.toString().contains("error_msg")) {err = res.toString();break;}if (res.has("result"))json = res.getJSONObject("result");// 假如正确的if (json.has("ret_msg")) {str = json.getString("ret_msg");if ("未开始".equals(str) || "进行中".equals(str)) {logger.info("===============进行中未开始err=" + str + "=========================");try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}} else if ("已完成".equals(str)) {err = res.toString();// logger.info("===============已完成err=" + err +// "=========================");break;}}if (num > 8) {err = res.toString();break;}logger.info("=====================while最后一行假如未完成,继续请求ret_msg" + str + "========================");}if (err.contains("error_msg")) {logger.info("=====================err" + err + "========================");return null;}String str = null;if (json.has("result_data")) {str = json.getString("result_data");// logger.info("=====================json.has(result_data)" +// json.toString() + "========================");return str;}// logger.info("=====================假如未完成,继续请求strisEmpty" + str +// "========================");return str;}
}

百度云文字识别OCR【表格】相关推荐

  1. 百度云 文字识别API在线调用测试

    1.0:百度云文字识别应用创建 1.1:找到文字识别产品 1.2:点击使用文字识别 1.3:点击创建应用 1.4:点击  查看应用 ,并输入基本信息,点击立即创建 2.0:token 获取 2.1:找 ...

  2. java做百度语言识别_java实现百度云文字识别接口代码

    java实现百度云文字识别接口代码 发布时间:2020-09-16 13:35:23 来源:脚本之家 阅读:52 作者:syy363250763 本文实例为大家分享了java实现百度云文字识别的接口具 ...

  3. java实现自动识别验证码并自动填写提交(调用百度通用文字识别OCR接口+大图找小图之图像识别算法+模拟鼠标键盘动作)

    一.使用的技术: 1.调用百度AI通用文字识别OCR接口 2.图像识别算法 二.实现方案与步骤: 1.得到验证码的图片的坐标:先将验证码截图,然后再对整个电脑屏幕截图,通过大图找小图的方法,计算出小图 ...

  4. python 百度云文字识别 proxy_python使用百度文字识别功能方法详解

    介绍python使用百度智能去的文字识别功能,可以识别截图中的文,登陆路验证码等等., 登陆百度智能云,选择产品服务. 选择"人工智能"---文字识别. 点击创建应用. 如图下面有 ...

  5. java怎么实现华为云文字识别,OCR文字识别服务快速入门教程

    [入门介绍]OCR服务快速入门汇总 1.[快速入门 01] 申请华为文字识别服务 现阶段,华为文字识别服务处于公测阶段,需要先申请公测,再申请开通服务,具体操作步骤如下: 步骤1       申请华为 ...

  6. 百度云 文字识别 身份证识别

    身份识别 package ai1;import java.util.HashMap;import org.json.JSONObject;import com.baidu.aip.ocr.AipOcr ...

  7. 用Python实现最简单的文字识别:基于百度云文字识别API

    Python版本:3.6.5 百度云提供的文字识别技术,准确率还是非常高的,而且每天还有5w次免费的调用量,对于用来学习或者偶尔拿来用用,已经完全足够了.文章提供一个模板,稍加修改就可以直接套用.注释 ...

  8. python 百度云文字识别 proxy_Python基于百度AI的文字识别的示例

    Python基于百度AI的文字识别的示例 发布时间:2020-10-03 14:52:27 来源:脚本之家 阅读:66 使用百度AI的文字识别库,做出的调用示例,其中filePath是图片的路径,可以 ...

  9. JavaWeb使用百度云文字识别上传图片内容

    今天有童鞋问我,android端实现了拍照识别图片内容,那JavaWeb的话,是不是也可以上传图片进行识别呢? 答案是一定的. 效果图走一波 选择文件点击确认上传 识别后返回识别结果 整体来说还是比较 ...

  10. PaddleOCR百度开源—文字识别OCR windows端口本地部署使用

    目录 更新时间:2022年02月07日 新增: 本地调用OCR 什么是Paddle OCR? Paddle OCR特性: 导言 下载源码 配置环境 安装依赖库 报错处理办法: 下载模型 文本检测模型 ...

最新文章

  1. 打包无法识别lombok
  2. 用Helper对类的行为进行修饰以便复用(附:外三篇)
  3. iOS.ObjC.Basic-Knowledge
  4. 蚂蚁森林:不存在网友反馈的“没有造林”的情况 干旱造成梭梭矮小
  5. 【ElasticSearch】Es 源码之 NodeService 源码解读
  6. C++静态数据成员与静态函数(案例分析)
  7. 怎么改wps表格中折线图的横坐标?
  8. 3.11 Ext JS 下拉框(Combobox)使用
  9. android alertdialog view,Android AlertDialog 方法setView(view,0,0,0,0)开发自定义对话框
  10. Node.js模块化开发(非常详细,满满的干货)
  11. Ubuntu20.04配置NTP服务器
  12. 安卓优雅的svg实践绚丽效果
  13. 软交换总结之二-七号信令
  14. 【stm32CubeMX】STM32F103c8t6串口通信
  15. 其他,HC6800-EM3 V30原理图
  16. 如何將人臉變漂亮(三)
  17. 赶紧收下这四个安卓手机通讯录丢失怎么恢复的技巧吧
  18. 使用css绘制弹幕,实例详解CSS3 实现弹幕
  19. 数字与中文大写数字互转(5千万亿亿亿亿以上的数字也支持转换)
  20. Leapfrog Triejoin:最坏情况下的最优连接算法

热门文章

  1. 对股票进行可视化分析
  2. 忘记linux虚拟机密码怎么办
  3. SpriteKit在iOS8和OSX10.10中的新特性(强悍来袭)
  4. Servlet概述及其原理
  5. 免费计算机网络基础ppt,计算机网络基础概念课件.ppt
  6. 最新单片机毕业设计题目大全
  7. 单片机c语言毕业设计,单片机毕业设计的总结.docx
  8. 打印机打印字体轻,像针式的故障处理(图)
  9. InstallShield教程-打包.NET程序
  10. Windows安装杜比音效驱动