Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update functions/imagemagick sample #580

Merged
merged 5 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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