我读过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
确实存在请求错误,但我不知道发生了什么。是因为data
中发送requests.post
的格式错误了吗?
答案 0 :(得分:0)
错误"message": "Invalid JSON payload received. Unexpected token.\nrequests=image&reque\n^",
表示您传递的是非json格式,需要是json。因此,您应该将其转换为json并将其传递给请求,如下所示。
response = requests.post(
url='https://vision.googleapis.com/v1/images:annotate?key={}'.format(API_KEY),
# import json module
# dumps the object to JSON
data=json.dumps(sending_request),
headers={'Content-Type': 'application/json'}
它会触发typeError: Object of type 'bytes' is not JSON serializable at the line of json.dumps([sending_request])
,因为您没有解码b64encode图像。所以,首先执行此操作并发送请求
content = base64.b64encode(image).decode('UTF-8')