在 Firebase Cloud Functions 中使用 Google Vision API 时出现类型错误
TypeError when using Google Vision API in Firebase Cloud Functions
我正在尝试获取一个示例,以便在 Firebase Cloud Functions 中使用 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);
});
}
我已经完成了以下步骤:
- 创建了一个有效的 Firebase 项目
- 激活愿景 API 并为项目开具账单
- 在我的 PC 上本地初始化了 Firebase 函数
- 使用
npm install
安装了所需的 npm 模块
- 尝试使用
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 给我带来那么多麻烦。
有人可以提示我我的设置出了什么问题吗?
谢谢!
我在 FCF 中也经历过 Cloud Vision 的一些头痛。我的工作解决方案如下所示:
const vision = require('@google-cloud/vision');
const visionClient = new vision.ImageAnnotatorClient();
那么你应该可以做一些很酷的事情,比如:
visionClient.labelDetection(someImageUri)
问题是您使用的视觉版本 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"
所以,你有两个选择。降低依赖项版本以匹配示例代码所使用的版本,或更改示例以适合您要使用的版本。
我正在尝试获取一个示例,以便在 Firebase Cloud Functions 中使用 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);
});
}
我已经完成了以下步骤:
- 创建了一个有效的 Firebase 项目
- 激活愿景 API 并为项目开具账单
- 在我的 PC 上本地初始化了 Firebase 函数
- 使用
npm install
安装了所需的 npm 模块
- 尝试使用
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 给我带来那么多麻烦。
有人可以提示我我的设置出了什么问题吗?
谢谢!
我在 FCF 中也经历过 Cloud Vision 的一些头痛。我的工作解决方案如下所示:
const vision = require('@google-cloud/vision');
const visionClient = new vision.ImageAnnotatorClient();
那么你应该可以做一些很酷的事情,比如:
visionClient.labelDetection(someImageUri)
问题是您使用的视觉版本 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"
所以,你有两个选择。降低依赖项版本以匹配示例代码所使用的版本,或更改示例以适合您要使用的版本。