【发布时间】:2018-09-29 19:57:33
【问题描述】:
我已经阅读了How to use the Google Vision API for text detection from base64 encoded image?,但它根本没有帮助。 Cloud client library 对我来说是不可取的,因为我在 OCR 之前和期间进行了许多图像处理(例如旋转、裁剪、调整大小等)。将它们保存为新文件并将它们作为 Google Vision API 的输入重新读取是相当低效的。
因此,我直接查看了发布请求的文档:
以下是导致失败的最少代码:
import base64
import requests
import io
# Read the image file and transform it into a base64 string
with io.open("photos/foo.jpg", 'rb') as image_file:
image = image_file.read()
content = base64.b64encode(image)
# Prepare the data for request
# Format copied from https://cloud.google.com/vision/docs/ocr
sending_request = {
"requests": [
{
"image": {
"content": content
},
"features": [
{
"type": "TEXT_DETECTION"
}
]
}
]
}
# Send the request and get the response
# Format copied from https://cloud.google.com/vision/docs/using-python
response = requests.post(
url='https://vision.googleapis.com/v1/images:annotate?key={}'.format(API_KEY),
data=sending_request,
headers={'Content-Type': 'application/json'}
)
# Then get 400 code
response
# <Response [400]>
print(response.text)
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unexpected token.\nrequests=image&reque\n^",
"status": "INVALID_ARGUMENT"
}
}
我去了控制台,看到google.cloud.vision.v1.ImageAnnotator.BatchAnnotateImages 确实存在请求错误,但我不知道发生了什么。是不是因为requests.post中发送data的格式不对?
【问题讨论】:
标签: python ocr google-vision