来自 Google Vision API OCR 的响应 400 带有指定图像的 base64 字符串

Response 400 from Google Vision API OCR with a base64 string of specified image

我读过 How to use the Google Vision API for text detection from base64 encoded image? but it doesn't help at all. 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中sent data格式错误?

错误 "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')