来自Google Vision API OCR的响应400,带有指定图像的base64字符串 [英] Response 400 from Google Vision API OCR with a base64 string of specified image

查看:138
本文介绍了来自Google Vision API OCR的响应400,带有指定图像的base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读云客户端库对我来说是不可取的,因为我正在做很多图像处理(例如在OCR之前和期间进行旋转,裁剪,调整大小等).将它们另存为新文件并重新读取为Google Vision API的输入是非常低效的.

I've read How to use the Google Vision API for text detection from base64 encoded image? but it doesn't help at all. Cloud client library is undesirable for me because I am doing many image processing (e.g. rotating, cropping, resizing, etc.) before and during OCR. Saving them as new files and re-read them as inputs of Google Vision API is rather inefficient.

因此,我直接检查了发布请求的文档:

Hence, I went check the documentation of posting requests directly:

  • Using Python to send requests
  • Base64 Encoding
  • Optical character recognition (OCR),

和以下是导致失败的最小代码:

and here are minimum codes to make the failure:

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格式错误?

I went to my console and see there are indeed request errors for google.cloud.vision.v1.ImageAnnotator.BatchAnnotateImages, but I don't know what happened. Is it because the wrong format of sent data in requests.post?

推荐答案

错误"message": "Invalid JSON payload received. Unexpected token.\nrequests=image&reque\n^",表示您正在传递非json格式,该格式必须是json.因此,您应该将其转换为json并将其传递给请求,如下所示.

The error, "message": "Invalid JSON payload received. Unexpected token.\nrequests=image&reque\n^", states you are passing a non-json format which is required to be a json. So, you should convert it to json and pass it to the request, as shown below.

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图像.因此,请先执行此操作并发送请求

It will trigger typeError: Object of type 'bytes' is not JSON serializable at the line of json.dumps([sending_request]) because you are not decoding the b64encode image. So, do this first and send the request

content = base64.b64encode(image).decode('UTF-8')

这篇关于来自Google Vision API OCR的响应400,带有指定图像的base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆