java - 如何将图像从 java-app 发送到 python?
问题描述
我有多页 pdf 文档。该文档转换为图像数组。图像发送到 python 以提取文本。
python代码(这项工作)
from PIL import Image
import pytesseract
from flask import Flask, request
app = Flask(__name__)
@app.route('/api/text_from_image', methods=['POST'])
def get_image():
imagefile = request.files.get('imagefile', '')
print(imagefile)
img = Image.open(imagefile)
img.load()
text = pytesseract.image_to_string(img, lang="rus")
return text
if __name__ == '__main__':
app.debug = True # enables auto reload during development
app.run()
和java代码
ClassLoader classLoader = getClass().getClassLoader();
File initialFile = new File(Objects.requireNonNull(classLoader.getResource("rrr.pdf")).getFile());
InputStream targetStream = new FileInputStream(initialFile);
PDDocument document = PDDocument.load(targetStream);
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
ImageIOUtil.writeImage(bim, "aaa" + "-" + (page + 1) + ".png", 300);
MultiValueMap<String, Image> body = new LinkedMultiValueMap<>();
body.add("imagefile", bim);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Image>> request = new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://localhost:5000/api/text_from_image", request, String.class);
System.out.println(stringResponseEntity.toString());
当我运行 java 应用程序时,我得到一个错误:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: java.awt.Rectangle["bounds2D"]->java.awt.Rectangle["bounds2D"]->java.awt.Rectangle["bounds2D"]->java.awt.Rectangle["bounds2D"]->java.awt.Rectangle["bounds2D"]->java.awt.Rectangle["bounds2D"]->java.awt.Rectangle["bounds2D"]->java.awt.Rectangle["bounds2D"]-
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion
(StackOverflowError) (through reference chain: java.awt.Rectangle["bounds2D"]-
>java.awt.Rectangle["bounds2D"]->java.awt.Rectangle["bounds2D"]-
>java.awt.Rectangle["bounds2D"]-
错误在哪里?
解决方案
谢谢@Michael Butscher
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
ImageIOUtil.writeImage(bim, "aaa" + "-" + (page + 1) + ".png", 300);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("imagefile", new FileSystemResource("test/"+"aaa" + "-" + (page + 1) + ".png"));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://localhost:5000/api/text_from_image", request, String.class);
System.out.println(stringResponseEntity.toString());
推荐阅读
- python - 在 pyvista 中保存屏幕截图,包括交互/旋转
- python - .reset_index 之后的 fillna() 用于创建 DataFrame
- scrapy - 如何从scrapy spider返回数据?
- fluid-framework - 如何在 Tinylicious 会话之间保留 Fluid 文档数据?
- azure - 如何使用策略强制执行 Azure 应用服务计划层
- asp.net-core - ABP 将控制器中的 API 路由重命名为服务
- javascript - Firebase 存储上传错误:无法读取未定义的属性“子”
- c# - 退出时如何保存 int 值
- java - 使用 lambda 表达式进行比较
- python - 由于 Numpy ufunc 签名,Numba guvectorize 异常