客户端访问PaddleOCR并获得OCR结果
PaddleOCR
Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
项目地址:https://gitcode.com/gh_mirrors/pa/PaddleOCR
免费下载资源
·
一、设置JSON格式
服务器端的请求格式:
post.request(url, header, data)
其中data严格要求json格式:
data = {"images":["xxxxxx"]}
值得注意的是data的value是一个数组,这是我和服务器那边都踩了的坑。
二、发送POST请求
核心代码
public static void sendPost(String postUrl, String data){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection=null;
try {
Gson gson = new Gson();
URL url = new URL(postUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
//TODO 设置请求方式 GET / POST 一定要大小
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "application/json");
connection.connect();
//TODO 传图片到服务器
DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
jsonBean jsonBean = new jsonBean(new String[]{data});
dos.writeBytes(gson.toJson(jsonBean));
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException("HTTP error code" + responseCode);
}
String resulText = getStringByStream(connection.getInputStream());
if (resulText == null) {
Log.d("HL", "failed");
}else{
//TODO 解析服务器返回的JSON
JsonRootBean jsonRootBean = gson.fromJson(resulText, JsonRootBean.class);
List<List<Results>> results = jsonRootBean.getResults();
//TODO 遍历结果,获得识别的文本
for(List<Results> result : results){
for(Results res : result){
Log.d("HL", res.getText());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
三、解析返回来的数据
返回的数据是一个很复杂的JSON格式数据。通过在线的JSON转JavaBean工具得到JavaBean格式,进行解析。
(工具地址:https://www.bejson.com/json2javapojo/new/)
//TODO 解析服务器返回的JSON
JsonRootBean jsonRootBean = gson.fromJson(resulText, JsonRootBean.class);
List<List<Results>> results = jsonRootBean.getResults();
//TODO 遍历结果,获得识别的文本
for(List<Results> result : results){
for(Results res : result){
Log.d("HL", res.getText());
}
}
结果:
GitHub 加速计划 / pa / PaddleOCR
41.53 K
7.59 K
下载
Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
最近提交(Master分支:3 个月前 )
7bbda2bc
6 天前
1d4e7a80
8 天前
更多推荐
已为社区贡献5条内容
所有评论(0)