尝试将 base_64 编码图像传递给 google 身份验证
Trying to pass a base_64 encoded image to google authentication
所以我正在做一个小项目(一个 restful 服务,所以 json 格式,代码中没有提到),其中代码接受 base_64 图像数据并解码它来自图像,我可以将它转换回图像,但我无法在图像上使用 google vision(googel ocr) 来提取文本。唯一不起作用的部分是以下代码块:
from flask import Flask,request,jsonify
import os,io,re,glob,base64
from google.cloud import vision
from google.cloud.vision import types
from PIL import Image
app = Flask(__name__)
os.environ['GOOGLE_APPLICATION_CREDENTIALS']=r'date_scanner.json'
@app.route('/jason_example',methods=['POST'])
def jason_example():
req_data=request.get_json()
base_64_image_content=req_data['imgcnt']
#the issue starts from here
image = base64.b64decode(base_64_image_content)
image=Image.open(io.BytesIO(image))
image=vision.types.Image(content=content)
response=client.text_detection(image=image)
texts=response.text_annotations`
enter code here
不需要使用 Image.open
,我认为这是一个 PIL
方法。您应该能够使用 base64.decodebytes
将其直接解码为字节字符串,如 this answer、
中所述
代码应如下所示:
# the issue starts from here
image_bytes = base64.decodebytes(base_64_image_content)
image = vision.types.Image(content=image_bytes)
response=client.text_detection(image=image)
texts=response.text_annotations
所以我正在做一个小项目(一个 restful 服务,所以 json 格式,代码中没有提到),其中代码接受 base_64 图像数据并解码它来自图像,我可以将它转换回图像,但我无法在图像上使用 google vision(googel ocr) 来提取文本。唯一不起作用的部分是以下代码块:
from flask import Flask,request,jsonify
import os,io,re,glob,base64
from google.cloud import vision
from google.cloud.vision import types
from PIL import Image
app = Flask(__name__)
os.environ['GOOGLE_APPLICATION_CREDENTIALS']=r'date_scanner.json'
@app.route('/jason_example',methods=['POST'])
def jason_example():
req_data=request.get_json()
base_64_image_content=req_data['imgcnt']
#the issue starts from here
image = base64.b64decode(base_64_image_content)
image=Image.open(io.BytesIO(image))
image=vision.types.Image(content=content)
response=client.text_detection(image=image)
texts=response.text_annotations`
enter code here
不需要使用 Image.open
,我认为这是一个 PIL
方法。您应该能够使用 base64.decodebytes
将其直接解码为字节字符串,如 this answer、
代码应如下所示:
# the issue starts from here
image_bytes = base64.decodebytes(base_64_image_content)
image = vision.types.Image(content=image_bytes)
response=client.text_detection(image=image)
texts=response.text_annotations