0
votes

I am using Google Cloud Vision API for OCR purpose. I am able to connect to the API and getting JSON result back as expected. What baffles me is that while the https://cloud.google.com/vision/ url correctly detects the text in the image, the API call often returns inaccurate text data for the same image. Pl. let me know what could be the case. Sample code is attached.

        String url = "https://vision.googleapis.com/v1/images:annotate?key=mykey";
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
        BufferedImage img = ImageIO.read(new File("F://image.jpg"));
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        String fileext = "jpg";
        ImageIO.write(img, fileext, baos );
        baos.flush();
        byte[] imageInByte=baos.toByteArray();
        baos.close();
        String imgstr =  java.util.Base64.getEncoder().encodeToString(imageInByte);
        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
1
ok, I will try and let you know.Pundarik

1 Answers

1
votes

I had a similar issue.

The Google Cloud Vision API says that the "OCR automatically detects latin characters, but sometimes it can fail" or have a strange behavior. The API also says that you can add a parameter to help the ocr to detect better the text, giving a context to the image.

You have to add the following code to the request.

"imageContext": {
        "languageHints": [
          "en"
        ]
 }

The json result should look like this:

{
  "requests": [
    {
      "image": {
            ...
      },
      "features": [{
            type: 'TEXT_DETECTION',
            maxResults:1
        }],
      "imageContext": {
        "languageHints": [
          "en"
        ]
      }
    }
  ]
}

Note that language Hints its an array, so you can add more languages, to give the OCR, precisely, a hint.

You can read more: https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#imagecontext

This helped me to don't get strange characters.