赞
踩
给予一张规范的带铭牌的图片,识别图片中的文字,并解析成一个铭牌对象。
1、python项目的部署
2、java的rpc调用
https://github.com/alisen39/TrWebOCR
使用Centos安装Docker后直接在Docker中运行
我Docker安装参考地址:https://www.runoob.com/docker/docker-tutorial.html
PS:别使用python环境运行,好多问题,解决起来很麻烦你,还不如用Docker,顶多怕网络慢点。
OCR识别项目RPC调用还好,百度搜搜,网络里找找没问题。一定要先把OCR项目部署好,通过web端试一下能成功再往下走。
以下是我的java端实现
package com.hcet.redweb.api; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hcet.redweb.global.GrobleException; import com.hcet.redweb.vo.OCRInfo; import org.junit.Test; import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.io.File; import java.util.ArrayList; import java.util.List; /** * @Description OCR远程识别接口调用 * @Author crzep * @Date 2020/10/29 11:04 * @VERSION 1.0 **/ public class OCRRpc { private String OCR_RPC_URL="http://81.68.252.160:8089/api/tr-run/"; @Test public void test(){ OCRRpc o=new OCRRpc(); o.toOcr("http://81.68.252.160:8089/api/tr-run/", new File("C:\\Users\\crzep\\Desktop\\temp\\model.png")); } /** * 进行OCR识别 * @param file * @return */ public List<OCRInfo> doFileOcr(File file){ return this.toOcr(OCR_RPC_URL,file); } /** * ocr远程调用接口 * @param webOcrUrl OCR远程调用接口 * @param image 需要识别的图片 * @return */ public List<OCRInfo> toOcr(String webOcrUrl, File image){ // 请求执行对象 RestTemplate restTemplate = new RestTemplate(); //设置请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); //设置请求体,注意是LinkedMultiValueMap FileSystemResource fileSystemResource = new FileSystemResource(image); MultiValueMap<String, Object> form = new LinkedMultiValueMap<>(); form.add("file", fileSystemResource); //用HttpEntity封装整个请求报文 HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers); ResponseEntity<String> response = restTemplate.postForEntity(webOcrUrl, files, String.class); // 处理返回信息 JSONObject jsonObject = JSON.parseObject(response.getBody()); if (jsonObject.getInteger("code")==200){ return jsonToOcrInfos(jsonObject.getJSONObject("data").getJSONArray("raw_out")); } throw new GrobleException("OCR识别失败!"); } /** * 将json转化为OCRinfo对象 * @param jsonArray json数组 * @return */ public List<OCRInfo> jsonToOcrInfos(JSONArray jsonArray){ List<OCRInfo> list=new ArrayList<>(); for (int i=0;i<jsonArray.size();i++){ JSONArray area= jsonArray.getJSONArray(i); OCRInfo ocrInfo=new OCRInfo(); ocrInfo.setArea(JSON.parseArray(area.getString(0),Double.class)); ocrInfo.setText(area.getString(1)); ocrInfo.setPercent(area.getDouble(2)); System.out.println(ocrInfo.getText()); list.add(ocrInfo); } return list; } }
package com.hcet.redweb.vo; import java.util.List; /** * @Description TODO * @Author crzep * @Date 2020/10/29 12:14 * @VERSION 1.0 **/ public class OCRInfo { /** * 一共四个参数 * 第1、2个:x起点 y起点像素值 * 第3、4个:识别长、宽像素点 * 第5个:识别矩形的旋转度数(顺时针) */ private List<Double> area; /** * 识别结果 */ private String text; /** * 识别准确度百分比 */ private Double percent; public List<Double> getArea() { return area; } public void setArea(List<Double> area) { this.area = area; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Double getPercent() { return percent; } public void setPercent(Double percent) { this.percent = percent; } @Override public String toString() { return "OCRInfo{" + "area=" + area + ", text='" + text + '\'' + ", percent=" + percent + '}'; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。