当前位置:   article > 正文

python、java 调用百度文字识别API_java 调研 python ocr

java 调研 python ocr

在这里插入图片描述
百度智能云文字识别-官网

使用百度智能云文字识别API的前提是,在百度智能云中创建文字识别应用。
创建成功后我们能获取该应用的 APP_IDAPI_KeySecret_Key

python部分

1.获取授权的access_token

import requests 
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=PcPyC85uM6bcAbCbfMGvCvg3&client_secret=OyItsZFYxBb0GAMsO6Zp1FN3rIQn2zUS'
response = requests.get(host)
if response:
    print(response.json())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.进行本地图片的识别

import requests
import base64
import ssl,sys
#使用	通用文字识别
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
#url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general'    高精度文字识别
 
data = {}  
data['access_token']='24.2289cc8cff01bb58216efc99016fc9d9.3592000.1575883789.282335-17732285'
            
            
#读取图片   
file=open('D:/test.png','rb')    
image= file.read()  
file.close()  
      
data['image'] = base64.b64encode(image)
headers={
    "Content-Type":"application/x-www-form-urlencoded",
    "apikey":"PcPyC85uM6bcAbCbfMGvCvg2" 
}
    
res = requests.post(url=url,headers=headers,data=data)
result = res.json() 
# print(result)
# 将图中所以文字输出
print(result['words_result'])
for word in result['words_result']:
    print(word['words'])
  • 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

java部分

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@CrossOrigin
@ResponseBody
@RestController
@RequestMapping("/api/orc")
public class OrcController {

    @Value("${ORC_PATH}")
    private String ORCPATH;

    public static final String APP_ID = "17732275";
    public static final String API_KEY = "PcPyC85uM6bcAbCbfMGvDvg2";
    public static final String SECRET_KEY = "OyItsZFYxBb0GAMsO6Zp1FN3rIQn2zCS";

    private AipOcr client;

    public OrcController(){
        client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);//类构造时,创建一个AipOcr单例
    }

    @RequestMapping(value = "/img/upload",method = RequestMethod.POST)
    public String[] ImgUpload(@RequestParam("files") MultipartFile[] files,
                             HttpServletRequest request){
        String[] result = new String[files.length];
        int c=0;
        for(MultipartFile file:files){
            File desFile = new File(ORCPATH+"/"+file.getOriginalFilename());
            if(!desFile.getParentFile().exists()){
                desFile.getParentFile().mkdirs();   //创建父级文件路径
            }
            try {
                file.transferTo(desFile);
            }catch (IllegalStateException | IOException e){
                e.printStackTrace();
            }

            JSONObject res = client.basicGeneral(desFile.getAbsolutePath(), new HashMap<String, String>());
            try {
            	//解析通用文字识别返回的数据
                JSONArray words_result = res.getJSONArray("words_result");
                String words ="";
                for (int i = 0; i < words_result.length(); i++) {
                    JSONObject jo = words_result.getJSONObject(i);
                    words += jo.getString("words");
                }
                result[c]=words;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            c++;
        }
        return result;
    }
}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/539447
推荐阅读
相关标签
  

闽ICP备14008679号