我将通过xhr请求从画布获取的base64代码发送到Firebase Functions,并尝试在Cloud Vision上使用它,但出现此错误:
Error: No image present.
at _coerceRequest (/user_code/node_modules/@google-cloud/vision/src/helpers.js:69:21)
at ImageAnnotatorClient.<anonymous> (/user_code/node_modules/@google-cloud/vision/src/helpers.js:224:12)
at ImageAnnotatorClient.wrapper [as annotateImage] (/user_code/node_modules/@google-cloud/vision/node_modules/@google-cloud/promisify/build/src/index.js:44:35)
at ImageAnnotatorClient.<anonymous> (/user_code/node_modules/@google-cloud/vision/src/helpers.js:141:17)
at PromiseCtor (/user_code/node_modules/@google-cloud/vision/node_modules/@google-cloud/promisify/build/src/index.js:71:28)
at ImageAnnotatorClient.wrapper [as labelDetection] (/user_code/node_modules/@google-cloud/vision/node_modules/@google-cloud/promisify/build/src/index.js:56:16)
at exports.hello.functions.https.onRequest (/user_code/index.js:23:8)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /var/tmp/worker/worker.js:783:7
at /var/tmp/worker/worker.js:766:11
这是将画布图像转换为base64并将其发送的方法:
const base64Img = canvas.toDataURL();
const data = base64Img.replace(/^data:image\/(png|jpg);base64,/, "");
const blob = new Blob([data], {type: 'text/plain'});
let xhr = new XMLHttpRequest();
xhr.open("POST", "hello", true);
xhr.send(blob);
这就是我试图将其发送到Cloud Vision on Cloud Functions的方式:
const functions = require('firebase-functions');
const vision = require('@google-cloud/vision');
exports.hello = functions.https.onRequest((req, res) => {
let imageBuffer = Buffer.from(req.body).toString('base64');
const request = {
"requests":[
{
"image":{
"content": imageBuffer
},
"features":[
{
"type":"LABEL_DETECTION",
}
]
}
]
}
const client = new vision.ImageAnnotatorClient();
client
.labelDetection(request)
.then(results => {
res.send(results);
})
.catch(err => {
console.error('ERROR:', err);
res.send(err);
});
});