PaddleOCR
PaddleOCR copied to clipboard
关于业务代码Java调用服务端hub serving部署的structure_table报错
问题描述 / Problem Description
如题:我在centos7上通过hub serving 启动了structure_table 服务,服务是调通了,但是返回结果是:{"msg":"list index out of range","results":"","status":"101"}不知道是什么意思,肯定是没有成功,服务是通的。
模型如下截图
服务是调通了,截图如下:
运行环境 / Runtime Environment
- OS: centos7 部署服务,windows ide java业务调用服务
- Paddle: 2.6.1
- PaddleOCR: 2.7.3
复现代码 / Reproduction Code
我的java代码如下: package com.freeze;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONObject;
import java.io.File; import java.io.FileInputStream; import java.io.IOException;
public class PaddleHubTableClient {
public static void main(String[] args) {
String serverUrl = "http://172.16.12.229:8868/predict/structure_table"; // PaddleHub Serving默认URL
File imageFile = new File("D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\img\\table.jpg");
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost uploadFile = new HttpPost(serverUrl);
// Read image file and encode to Base64
FileInputStream fileInputStream = new FileInputStream(imageFile);
byte[] imageBytes = new byte[(int) imageFile.length()];
fileInputStream.read(imageBytes);
String encodedImage = Base64.encodeBase64String(imageBytes);
fileInputStream.close();
// Create JSON object with the Base64 encoded image
JSONObject json = new JSONObject();
json.put("images", new String[]{encodedImage});
// Set the request content type to application/json
StringEntity requestEntity = new StringEntity(json.toString(), "UTF-8");
uploadFile.setEntity(requestEntity);
uploadFile.setHeader("Content-Type", "application/json");
// Execute the request
CloseableHttpResponse response = httpClient.execute(uploadFile);
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
response.close();
// Print the response
System.out.println("Original Response:");
System.out.println(responseString);
// Parse the response and decode the text
JSONObject jsonResponse = new JSONObject(responseString);
if (jsonResponse.has("status") && jsonResponse.getInt("status") != 0) {
System.out.println("Error Status: " + jsonResponse.getInt("status"));
System.out.println("Error Message: " + jsonResponse.getString("msg"));
} else if (jsonResponse.has("results")) {
JSONArray resultsArray = jsonResponse.getJSONArray("results");
for (int i = 0; i < resultsArray.length(); i++) {
JSONArray textRegions = resultsArray.getJSONArray(i);
for (int j = 0; j < textRegions.length(); j++) {
JSONObject textRegion = textRegions.getJSONObject(j);
String text = textRegion.getString("text");
String decodedText = StringEscapeUtils.unescapeJava(text);
System.out.println("Decoded Text: " + decodedText);
}
}
} else {
System.out.println("Unexpected response structure.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
完整报错 / Complete Error Message
Original Response: {"msg":"list index out of range","results":"","status":"101"}