Как отправить изображение с java -app на python? - PullRequest
0 голосов
/ 10 января 2020

У меня есть многостраничный 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"]-

где ошибка?

1 Ответ

1 голос
/ 10 января 2020

спасибо @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());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...