Skip to content

Commit

Permalink
Update functions/imagemagick sample (#580)
Browse files Browse the repository at this point in the history
* Update index.js

* Update package.json

* Lint

* Update index.test.js
  • Loading branch information
chenyumic authored Apr 4, 2018
1 parent 3cac032 commit 7233e6b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
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
30 changes: 22 additions & 8 deletions functions/imagemagick/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ const proxyquire = require(`proxyquire`).noCallThru();
const sinon = require(`sinon`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
const vision = require('@google-cloud/vision').v1p1beta1;

const bucketName = `my-bucket`;
const filename = `image.jpg`;

var VisionStub = sinon.stub(vision, 'ImageAnnotatorClient');
VisionStub.returns({
safeSearchDetection: sinon.stub().returns(Promise.resolve([{
safeSearchAnnotation: {
adult: 'VERY_LIKELY',
violence: 'VERY_LIKELY'
}
}]))
});

function getSample () {
const file = {
getMetadata: sinon.stub().returns(Promise.resolve([{}])),
Expand All @@ -39,11 +50,7 @@ function getSample () {
const storageMock = {
bucket: sinon.stub().returns(bucket)
};
const visionMock = {
detectSafeSearch: sinon.stub().returns(Promise.resolve([{ violence: true }]))
};
const StorageMock = sinon.stub().returns(storageMock);
const VisionMock = sinon.stub().returns(visionMock);
const childProcessMock = {
exec: sinon.stub().yields()
};
Expand All @@ -53,7 +60,6 @@ function getSample () {

return {
program: proxyquire(`../`, {
'@google-cloud/vision': VisionMock,
'@google-cloud/storage': StorageMock,
'child_process': childProcessMock,
'fs': fsMock
Expand All @@ -63,8 +69,7 @@ function getSample () {
childProcess: childProcessMock,
storage: storageMock,
bucket,
file,
vision: visionMock
file
}
};
}
Expand Down Expand Up @@ -96,8 +101,17 @@ test.serial(`blurOffensiveImages blurs images`, async (t) => {
});

test.serial(`blurOffensiveImages ignores safe images`, async (t) => {
VisionStub.restore();
VisionStub = sinon.stub(vision, 'ImageAnnotatorClient');
VisionStub.returns({
safeSearchDetection: sinon.stub().returns(Promise.resolve([{
safeSearchAnnotation: {
adult: 'VERY_UNLIKELY',
violence: 'VERY_UNLIKELY'
}
}]))
});
const sample = getSample();
sample.mocks.vision.detectSafeSearch = sinon.stub().returns(Promise.resolve([{}]));
await sample.program.blurOffensiveImages({ data: { bucket: bucketName, name: filename } });
t.is(console.log.callCount, 2);
t.deepEqual(console.log.getCall(0).args, [`Analyzing ${sample.mocks.file.name}.`]);
Expand Down

0 comments on commit 7233e6b

Please sign in to comment.