如何使用文本检测的谷歌API展望从编码图像的base64?(How to use the Googl

2019-09-28 06:24发布

我有一个base64编码的图像。

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

我应该如何进行文字检测与谷歌的云计算愿景Python库?

我的代码如下所示:

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

Answer 1:

使用这里展示云客户端库Python的愿景 。 云客户端库做所有的base64编码为你在幕后的。

用的virtualenv和客户端库初始化您的文件夹:

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

下面的代码片段做OCR的图像文件, 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))


文章来源: How to use the Google Vision API for text detection from base64 encoded image?