How to use the Google Vision API for text detectio

2019-07-27 07:47发布

I am having a base64 encoded image.

imageData = 'data:image/png;base64,iVBORw0rrfwfwHReger32QRQWr...'

How I should proceed for text detection with google cloud vision python library?

My Code looks like :

from google.cloud import vision
client = vision.Client()
imageData = 'data:image/png;base64,iVBORw0rrfwfwHReger32QRQWr...'
image = client.image(content=imageData)
texts = image.detect_text()
print texts[0].description

1条回答
霸刀☆藐视天下
2楼-- · 2019-07-27 08:32

Use the Cloud Client library for Python vision as demonstrated here. The Cloud client library does all of the base64 encoding for you behind the scenes.

Initialize your folder with a virtualenv and the client library:

virtualenv env && source env/bin/activate
pip install google-cloud-vision

The following snippet does OCR on an image file, base64 file.png:

from google.cloud import vision
from google.cloud.vision import types

client = vision.ImageAnnotatorClient()

content = 'iVBORw0KGgoAAAANSUhEUgAAAGQAAAAZCAYAAADHXotLAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAENElEQVRo3u1ay0u6Sxh+PjWzLI0vxRZWdCEqughGCbWxVu2jG7WIdm3CRYuoP6CoKLpA0CYIg6hNQRTYoqhlBQVBFF1Nu2lmRqBl71n9Bj1mxzodz89zvgcGZuad533Hefzme2eUIyKCgN8GImEJBEEECIIIgggQBBEEYRgcHATHcSFlZGSEjdHpdB+OcTgcYfkfFYPB8GlMjuMgl8tRXFyMnp4ePD4+hp231+vF0NAQysvLoVQqIZfLUVRUhN7eXrhcrn9HEfoBDA8PE4CQMjY2xsbo9foQu0gkIqfTSWazmXie/9DHn0tFRQURUcScrKwsstlsIXO22WxUWFgYlqfRaGh3d5eiDfyUo9PTU0pKSiIAxPM8nZ2dBdl9Ph9NTk6yD9zU1ERutztojFKpZPzNzc2g0tbWFiTIZ5z5+XkyGAwsVmNjYxDH6/VSSUkJs7e0tNDS0hKtrq6SyWQiiURCAEilUpHdbo9NQQIXR6PRfGhfWVlhi9DZ2fkl/vj4OMnlcjIajRFxHh4eSKFQEABKSEig19dXZuvr62Pz6OrqCok1PT0d9MURBAnD/2rMwKfk7u6O9aenpxMAkslk5HA4Qnjv7++Um5tLAEgsFtP19XXUBPnPZllEhJubGwBAQkICeJ4HABwfH8NqtQIAqqurkZqaGsLlOA4NDQ0AAL/fj42NjajNW/JPOPX5fFhfXw/p39/fj4h/e3sLjuOC+mpqarC2thYR/+7uDoODgzg/PwcAtLe3QywWAwBOTk7YuIyMjLA+MjMzWT2QE5OCuFwuGI3GqD4RH4kIAC0tLRgaGmJtj8fD6iqVKqw/tVrN6k9PT7EtyN8Fz/NYXFwM6ktJSfmyH7FYDI7j8PLyAqlUCgBITk5mdofDEZZ7f3/P6gqFIrYF0Wg0bP8OxOrqKmpra/+SHxcXh6qqqm+L+Pz8jJmZGczOzmJmZgZOpxPLy8sAgJycHMa5uLgI6y/Qlp2dHVsHw98xy/L7/ZSfn/9hlqXVagkAxcfHh82y8vLyhCzrM0xNTUEmk6GysjKyOyGRCKWlpaxts9lYvaOjg12d9Pf3h3BnZ2dxdHQEAKirq0NaWtr/+x3y+vqKra2toL6dnR14vV54vd6I/QS+L+x2O3Q6HQDAZDLBbDbj4OAAAwMDuL6+Rn19PaRSKSwWC0ZHR9k2GJgQxMyWNTc3x7aBXyU9PZ0WFhbYmNbWVna1AoAkEgmVlpaS2+0ms9lMKpUqorssvV4fccyJiQlmS05OJovFwmxWq5UKCgrCxlGr1bS9vR31u6wf2bIuLy9xdXUV1Ge1WtkB7NcZ5Pn5mbXf3t6wt7cHn88Hu93+acbz3ZhtbW1obm6GUqmEx+MJSjS0Wi12d3cxMDCAsrIyJCUlITExEYWFheju7sbh4SH0en3UdwdO+JOD8AOVAEEQQRAB38QfQtVPbkCRvTgAAAAASUVORK5CYII='

image = types.Image(content=content.decode('base64'))
texts = client.text_detection(image=image).text_annotations

for text in texts:
    print('\n"{}"'.format(text.description))
查看更多
登录 后发表回答