unable to get ocr text

31 views
Skip to first unread message

Frankie Chan

unread,
Jun 6, 2018, 7:12:34 PM6/6/18
to Google App Engine

I use google vision api to detect the ocr.

However, I got no ocr text.

On the other hand, I uploaded the image to https://cloud.google.com/vision/ and got DBR Weddings\n.

Is it normal?

How should I correct my code?

Thank you very much.

Here is my code:


import requests
from PIL import Image
from io import BytesIO
import cv2
import base64
import ast

url = r'https://drive.google.com/uc?id=1pjofUsy5V4v7DSVsRERw6S-Zo36hO1bZ'
feature_json_obj = [{'type': 'TEXT_DETECTION', 'maxResults': 100}]
img = Image.open(BytesIO(requests.get(url).content))
arr = cv2.cvtColor(np.asarray(img.convert('RGB'), dtype=np.uint8), cv2.COLOR_RGB2BGR)

content_json_obj = {'content': base64.b64encode(a_numpy.tostring()).decode('UTF-8')}
request_list = {'requests': [{'features': feature_json_obj, 'image': content_json_obj, 'imageContext': {'languageHints': "zh-TW"}}]}
req = requests.post(url='https://vision.googleapis.com/v1/images:annotate?key=my_key', data = str.encode(str(request_list)), headers={'Content-Type': 'application/json'}).text
receive_data = req.encode(sys.stdin.encoding, "replace").decode(sys.stdin.encoding)
data = ast.literal_eval(receive_data)['responses'][0]['fullTextAnnotation']['text']#get google-vision result

Kenworth (Google Cloud Platform)

unread,
Jun 7, 2018, 11:44:41 AM6/7/18
to Google App Engine
I tried your image with the following simple request and I got the text description. You can just compare your request with mine and see where the issue is. It could be possible that your usage of traditional Chinese (zh-TW) hint on an English text is causing the issue.

Unfortunately, we do not provide code-level support. If you need further assistance, consider posting on StackOverflow, which we also monitor as this falls outside the scope of this platform reserved for general discussion of GCP's products and services.

{
  "requests": [
    {
      "features": [
        {
          "type": "TEXT_DETECTION"
        }
      ],
      "image": {
        "source": {
          "imageUri": "gs://<your_file_name_here>.jpg"
        }
      }
    }
  ]
}
Reply all
Reply to author
Forward
0 new messages