我试图获取使用Firebase云功能中的Google Vision API的示例,但它失败了。
我使用Github上提供的未经修改的样本:https://github.com/firebase/functions-samples/tree/master/moderate-images
修改
这是我的源文件:
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for t`he specific language governing permissions and
* limitations under the License.
*/
'use strict';
const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')();
const vision = require('@google-cloud/vision')();
const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');
/**
* When an image is uploaded we check if it is flagged as Adult or Violence by the Cloud Vision
* API and if it is we blur it using ImageMagick.
*/
exports.blurOffensiveImages = functions.storage.object().onChange(event => {
const object = event.data;
const file = gcs.bucket(object.bucket).file(object.name);
// Exit if this is a move or deletion event.
if (object.resourceState === 'not_exists') {
return console.log('This is a deletion event.');
}
// Check the image content using the Cloud Vision API.
return vision.detectSafeSearch(file).then(data => {
const safeSearch = data[0];
console.log('SafeSearch results on image', safeSearch);
if (safeSearch.adult || safeSearch.violence) {
return blurImage(object.name, object.bucket, object.metadata);
}
});
});
/**
* Blurs the given image located in the given bucket using ImageMagick.
*/
function blurImage(filePath, bucketName, metadata) {
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const bucket = gcs.bucket(bucketName);
// Create the temp directory where the storage file will be downloaded.
return mkdirp(tempLocalDir).then(() => {
console.log('Temporary directory has been created', tempLocalDir);
// Download file from bucket.
return bucket.file(filePath).download({destination: tempLocalFile});
}).then(() => {
console.log('The file has been downloaded to', tempLocalFile);
// Blur the image using ImageMagick.
return spawn('convert', [tempLocalFile, '-channel', 'RGBA', '-blur', '0x8', tempLocalFile]);
}).then(() => {
console.log('Blurred image created at', tempLocalFile);
// Uploading the Blurred image.
return bucket.upload(tempLocalFile, {
destination: filePath,
metadata: {metadata: metadata} // Keeping custom metadata.
});
}).then(() => {
console.log('Blurred image uploaded to Storage at', filePath);
fs.unlinkSync(tempLocalFile);
console.log('Deleted local file', filePath);
});
}

我已完成以下步骤:
npm install
firebase deploy
然后我收到了这个错误:
i deploying functions
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
Error: Error occurred while parsing your function triggers.
TypeError: require(...) is not a function
at Object.<anonymous> (C:\Users\xxxxxx\FirebaseTest\functions\index.js:21:47)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at C:\Users\Tobias\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:18:11
at Object.<anonymous> (C:\Users\xxxxxx\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:38:3)
所以他抱怨这条线:
const vision = require('@google-cloud/vision')();
我的package.json看起来像这样:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase experimental:functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"@google-cloud/storage": "^1.5.2",
"@google-cloud/vision": "^0.14.0",
"child-process-promise": "^2.2.1",
"firebase-admin": "^5.8.1",
"firebase-functions": "^0.8.1",
"mkdirp": "^0.5.1",
"mkdirp-promise": "^5.0.1"
},
"private": true
}
很高兴知道:其他尝试,例如在其他功能中试用云存储触发器,与我的Firebase项目相得益彰。只有Vision API给我带来了很多麻烦。
有人可以给我一个暗示我的设置出了什么问题吗?
谢谢!
答案 0 :(得分:0)
我在FCF中也遇到过一些令人头疼的云端视觉问题。我的工作解决方案如下:
const vision = require('@google-cloud/vision');
const visionClient = new vision.ImageAnnotatorClient();
然后你应该能够做一些很酷的事情:
visionClient.labelDetection(someImageUri)
答案 1 :(得分:0)
问题是您使用的是较新版本的视觉API,而不是示例代码使用的版本。示例代码提供了这些依赖项:
"@google-cloud/storage": "^0.4.0",
"@google-cloud/vision": "^0.5.0",
"child-process-promise": "^2.2.0",
"firebase-admin": "^4.1.1",
"firebase-functions": "^0.5.1",
"mkdirp": "^0.5.1",
"mkdirp-promise": "^4.0.0"
你正在使用更新的东西:
"@google-cloud/storage": "^1.5.2",
"@google-cloud/vision": "^0.14.0",
"child-process-promise": "^2.2.1",
"firebase-admin": "^5.8.1",
"firebase-functions": "^0.8.1",
"mkdirp": "^0.5.1",
"mkdirp-promise": "^5.0.1"
所以,你有两个选择。回溯依赖版本以匹配示例代码使用的内容,或更改样本以适合您要使用的版本。