1

I have multiple page pdf document. This document converted to array of image. Image send to python for extract text.

python-code(this work)

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()

And java code

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

when i run the java application i get an error:

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"]-

where is the mistake?

1
  • 1
    I'm not used with this Java network library but I see that you try to send the BufferedImage object (which the library may try to convert to json) while you should send the written PNG file. Commented Jan 10, 2020 at 13:55

1 Answer 1

1

thanks @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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.