Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Document Text Detection Google Vision API English Language Only

Hi All,

I am using Google Vision API Document Text Detection for OCR to extract serial Numbers from an Image. The Serial Number contains only numbers and english alphabets. I have already set language Hint as English. However the extracted texts sometimes contains texts from other langugages such as "BKO" in Turkish or Cyrril language.  The Code snippet is given below:

 

            success, encoded_image = cv2.imencode('.jpg', crop_img1)
            content = encoded_image.tobytes()
            image = vision_v1.types.Image(content=content)
            image_context = vision_v1.ImageContext(language_hints=["en"])
            status_label.config(text="Extracting Texts")
            response = client.document_text_detection(image=image)
            texts = response.text_annotations
 
Is there a way I can tell Google Vision API that the text you are extracting is from English, You should not look for other languages. 

 

 

1 1 3,108
1 REPLY 1

Good day @SAK1,

Welcome to Google Cloud Community!

Language hints in an image context is used to improve the results if the language in an image is known (in some cases), it is not used to restrict the results to a specific language, leaving this empty most of the time will give you the better results since it allows automatic language detection. Sometimes it may cause a problem if the language hint is wrong (For example. you set the hint to english but the language in the image is in other language). You can check this link for more information: https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1p2beta1#imagecontext
One of the possible workarounds is manipulating the results or the response of the Vision API by extracting only the result that you need. Here is an example of the response:

 "textAnnotations": [
        {
          "locale": "en",
          "description": "WAITING?\nPLEASE\nTURN OFF\nYOUR\nENGINE",
          "boundingPoly": {
            "vertices": [
              {
                "x": 310,
                "y": 821
              },
              {
                "x": 2225,
                "y": 821
              },
              {
                "x": 2225,
                "y": 1965
              },
              {
                "x": 310,
                "y": 1965
              }
            ]
          }
        }

Hope this helps!