【问题标题】:Response 400 from Google Vision API OCR with a base64 string of specified image来自 Google Vision API OCR 的响应 400,带有指定图像的 base64 字符串
【发布时间】: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


    【解决方案1】:

    错误,"message": "Invalid JSON payload received. Unexpected token.\nrequests=image&amp;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')

    【讨论】:

    • json.dumps([sending_request])的行触发TypeError: Object of type 'bytes' is not JSON serializable
    • @ytu 更新了答案。也不要将 json 转储到任何数组中。使用 json.dumps(sending_request) 而不是 json.dumps([sending_request])
    • 解码后生效。谢谢你。在此之前,json.dumps([sending_request])json.dumps(sending_request) 都不起作用。
    猜你喜欢
    • 2019-04-03
    • 2020-10-22
    • 2017-11-16
    • 1970-01-01
    • 2017-08-05
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多