如何在谷歌云视觉 API 中直接发送 base 64 编码的图像字符串?

问题描述

我使用以下代码,它看起来像 client.text_detection 必须接收图像。相反,我只想发送图像的 base64 编码文本字符串。这样做的最佳方法是什么?

def detect_text(path): """Detects text in the file.""" client = vision.ImageAnnotatorClient()

    with io.open(path,'rb') as image_file:
        content = image_file.read()
    
    image = vision.Image(content=content)
    response = client.text_detection(image=image)
    texts = response.text_annotations
    print('Texts:')
    
    for text in texts:
        print('\n"{}"'.format(text.description))
    
        vertices = (['({},{})'.format(vertex.x,vertex.y)
                    for vertex in text.bounding_poly.vertices])
    
        print('bounds: {}'.format(','.join(vertices)))
    
    if response.error.message:
        raise Exception(
            '{}\nFor more info on error messages,check: '
            'https://cloud.google.com/apis/design/errors'.format(
                response.error.message))

解决方法

uploadImage() async {
const url = 'input web url here';
const header = {
  "Content-Type": "application/json","Api-Key": "input key here"
};`enter code here`
if(uploadimage == null) return;
String base64Image = base64Encode(uploadimage.readAsBytesSync());

var body = jsonEncode({
  'images': [base64Image],});

final response = await http.post(url,headers: header,body: body);


print(response.statusCode);