我正在使用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
答案 0 :(得分:1)
使用Cloud Client library for Python vision as demonstrated here。 Cloud客户端库在幕后为您执行所有base64编码。
使用virtualenv和客户端库初始化您的文件夹:
virtualenv env && source env/bin/activate
pip install google-cloud-vision
以下代码段对图像文件base64 file.png
执行OCR:
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))