首页 > 解决方案 > 如何将图像从 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"]-

错误在哪里?

标签: javapythonresthttp

解决方案


谢谢@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());

推荐阅读