Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Vision API text detection strange behaviour - Javascript

Recently something about the Google Vision API changed. I am using it to recognize text on receipts. All good until now. Suddenly, the API started to respond differently to my requests.

I sent the same picture to the API today, and I got a different response (from the past). I ensured nothing was changed in my code, so this is not the culprit.

Another strange thing is that, when I upload the image to https://cloud.google.com/vision/ in the response, under textAnnotations, I get an array of 183 entries. However when I post from my app I get an array of 113 entries. Below you can see my code.

function googleScan(imageData) {
    var deferred = $q.defer();
    var url = "https://vision.googleapis.com/v1/images:annotate?key=<myAPIKey>";
    var payload = {
        requests: {
            image: {
                content: imageData.split(',')[1]
            },
            features: [{
                type: 'TEXT_DETECTION',
                maxResults:50
            }]
        }
    };
    $http.post(url, payload, { headers: { "NoAuthToken": true } }).then(function (response) {
        deferred.resolve(parseAnalyzedResult(response.data.responses[0].textAnnotations));
        console.log(response);
    }, function (error) {
        console.log(error);
    });
    return deferred.promise;

I am wondering if somehow my free subscription got altered and that's why I receive a different response. Is that even possible? Has anyone stumbled upon this kind of issue before?

like image 213
Cosmin Leferman Avatar asked Mar 08 '23 18:03

Cosmin Leferman


2 Answers

I had the same issue. For me, (I don't know why, but...) changing from TEXT_DETECTION to DOCUMENT_TEXT_DETECTION solved the issue. Now the results received from the API are the same seen when I upload the image on Google Vision page.

like image 52
Marlon Giona Avatar answered Mar 24 '23 08:03

Marlon Giona


I had a similar issue.

The Google Cloud Vision API says that the "OCR automaticly detects latin charatecrs, 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 result should look like this:

{
  "requests": [
    {
      "image": {
            content: imageData.split(',')[1]
      },
      "features": [{
            type: 'TEXT_DETECTION',
            maxResults:50
        }],
      "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.

like image 36
DavidQuezada7 Avatar answered Mar 24 '23 07:03

DavidQuezada7