java调用OCR识别文字,OCR由python编写

一、业务

给予一张规范的带铭牌的图片,识别图片中的文字,并解析成一个铭牌对象。

二、使用到的技术

1、python项目的部署
2、java的rpc调用

三、实现

OCR识别项目:我选用的是开源python项目,如何安装部署、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端实现

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;
    }

}
OCRinfo对象
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 +
                '}';
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*crzep

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值