当前位置:   article > 正文

java调用OCR识别文字,OCR由python编写_python+java实现ocr

python+java实现ocr

一、业务

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

二、使用到的技术

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

三、实现

OCR识别项目:我选用的是开源python项目,如何安装部署、RPC调试请点击连接,参考项目下的说明文档
https://github.com/alisen39/TrWebOCR
  • 1

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

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
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 +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/539478
推荐阅读
相关标签
  

闽ICP备14008679号