我正在创建一个基于 Google 的 I/O '17 演示文稿的 Firebase 托管网络应用程序,其中 Google 的 AutoML Vision 可以拍摄云图,并根据其机器学习培训告诉您它是什么类型的云。我使用的代码只允许一次调用,我认为这是由于以下代码:
// Get only the first prediction response
let data = response[0]['payload'];
predictions[data[0].displayName] = data[0].classification.score;
从我从谷歌的文档中可以看出,[0] 对应于 annotateImageID。代码传递 0 的问题是 webapp 无法获得对多个图像的预测。
下面是调用结果推送到 webapp 的整个代码部分:
exports.callCustomModel = functions.storage.object().onFinalize(event => {
const file = gcsClient.bucket(event.bucket).file(event.name);
let destination = '/tmp/' + event.name.replace(/\s/g, '');
return file.download({destination: destination})
.then(() => {
if(sizeOf(destination).width > 600) {
console.log('scaling image down...');
return resizeImg(destination);
} else {
return destination;
}
})
.then(() => {
let bitmap = fs.readFileSync(destination);
let data = new Buffer(bitmap).toString('base64');
return callAutoMLAPI(data);
})
.then((response) => {
let predictions = {};
// Get only the first prediction response
let data = response[0]['payload'];
predictions[data[0].displayName] = data[0].classification.score;
if (Object.keys(predictions).length === 0) {
predictions = {"predictionErr": "No high confidence predictions found"};
}
return db.collection('images').doc(event.name).set(predictions);
})
.then(() => {
// Delete tmp image file to clean up resources
return new Promise((resolve, reject) => {
fs.unlinkSync(destination, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
})