Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions functions/imagemagick/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const storage = require('@google-cloud/storage')();
const vision = require('@google-cloud/vision')();
const vision = require('@google-cloud/vision').v1p1beta1;

const client = new vision.ImageAnnotatorClient();
// [END functions_imagemagick_setup]

// [START functions_imagemagick_analyze]
Expand All @@ -38,18 +40,22 @@ exports.blurOffensiveImages = (event) => {
}

const file = storage.bucket(object.bucket).file(object.name);
const filePath = `gs://${object.bucket}/${object.name}`

console.log(`Analyzing ${file.name}.`);

return vision.detectSafeSearch(file)
return client.safeSearchDetection(filePath)
.catch((err) => {
console.error(`Failed to analyze ${file.name}.`, err);
return Promise.reject(err);
})
.then(([safeSearch]) => {
if (safeSearch.adult || safeSearch.violence) {
.then(([result]) => {
const detections = result.safeSearchAnnotation

if (detections.adult === 'VERY_LIKELY' ||
detections.violence === 'VERY_LIKELY') {
console.log(`The image ${file.name} has been detected as inappropriate.`);
return blurImage(file, safeSearch);
return blurImage(file);
} else {
console.log(`The image ${file.name} has been detected as OK.`);
}
Expand Down
2 changes: 1 addition & 1 deletion functions/imagemagick/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"dependencies": {
"@google-cloud/storage": "1.4.0",
"@google-cloud/vision": "0.12.0"
"@google-cloud/vision": "0.15.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "2.1.0",
Expand Down