简体   繁体   中英

TypeError when using Google Vision API in Firebase Cloud Functions

I'm trying to get a sample for the use of the Google Vision API within Firebase Cloud Functions to work but it fails.

I'm using the unmodified sample provided on Github: https://github.com/firebase/functions-samples/tree/master/moderate-images

EDIT:

Here is my source file:

 /** * 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); }); } 

I've done the following steps:

  • Created a working Firebase project
  • Activated the Vision API and the billing for the project
  • Initialized the Firebase Functions localy on my PC
  • Installed needed npm modules with npm install
  • Tried to deploy with firebase deploy

Then i got this error:

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)

So he is complaining about this line:

const vision = require('@google-cloud/vision')();

My package.json looks like this:

{
  "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
}

Nice to know: Other attempts, for example to try out Cloud Storage triggers in other functions, are working pretty well with my Firebase project. Only the Vision API gives me that much trouble.

Can someone please give me a hint what went wrong with my setup?

Thank you!

I also experienced some headaches with Cloud Vision in FCF. My working solution looks like this:

const vision = require('@google-cloud/vision');
const visionClient =  new vision.ImageAnnotatorClient();

Then you should be able to do cool stuff like:

visionClient.labelDetection(someImageUri)

The problem is that you're using newer versions of the vision API than the sample code uses. The sample code provides these dependencies:

"@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"

You're using newer stuff:

"@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"

So, you have two options. Back down the dependency versions to match what's being used by the sample code, or change the sample to suit the versions you want to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM