Skip to content

Commit 4e070aa

Browse files
authored
Vision snippets. (#294)
1 parent ef7d36f commit 4e070aa

File tree

13 files changed

+878
-19
lines changed

13 files changed

+878
-19
lines changed

vision/samples/detect.js

Lines changed: 436 additions & 0 deletions
Large diffs are not rendered by default.

vision/samples/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
"license": "Apache Version 2.0",
66
"author": "Google Inc.",
77
"scripts": {
8-
"test": "cd ..; npm run st -- --verbose vision/system-test/*.test.js"
8+
"test": "cd ..; npm run st -- --verbose vision/system-test/detect.test.js"
99
},
1010
"dependencies": {
11+
"@google-cloud/storage": "0.6.0",
1112
"@google-cloud/vision": "0.7.0",
1213
"async": "2.1.4",
1314
"natural": "0.4.0",
14-
"redis": "2.6.3"
15+
"redis": "2.6.3",
16+
"yargs": "6.6.0"
1517
},
1618
"optionalDependencies": {
1719
"canvas": "1.6.2"
91.1 KB
Loading
158 KB
Loading

vision/samples/resources/logos.png

8.06 KB
Loading

vision/samples/resources/text.jpg

122 KB
Loading

vision/samples/resources/water.jpg

162 KB
Loading
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
require(`../../system-test/_setup`);
19+
20+
const path = require(`path`);
21+
const storage = require(`@google-cloud/storage`)();
22+
const uuid = require(`uuid`);
23+
24+
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
25+
const cmd = `node detect.js`;
26+
const cwd = path.join(__dirname, `..`);
27+
const files = [
28+
`face_no_surprise.jpg`,
29+
`landmark.jpg`,
30+
`logos.png`,
31+
`text.jpg`,
32+
`wakeupcat.jpg`
33+
].map((name) => {
34+
return {
35+
name,
36+
localPath: path.resolve(path.join(__dirname, `../resources/${name}`))
37+
};
38+
});
39+
40+
test.before(async () => {
41+
const [bucket] = await storage.createBucket(bucketName);
42+
await Promise.all(files.map((file) => bucket.upload(file.localPath)));
43+
});
44+
45+
test.after.always(async () => {
46+
const bucket = storage.bucket(bucketName);
47+
await bucket.deleteFiles({ force: true });
48+
await bucket.deleteFiles({ force: true }); // Try a second time...
49+
await bucket.delete();
50+
});
51+
52+
test(`should detect faces in a local file`, async (t) => {
53+
const output = await runAsync(`${cmd} faces ${files[0].localPath}`, cwd);
54+
t.true(output.includes(`Faces:`));
55+
t.true(output.includes(`Face #1:`));
56+
});
57+
58+
test(`should detect faces in a remote file`, async (t) => {
59+
const output = await runAsync(`${cmd} faces-gcs ${bucketName} ${files[0].name}`, cwd);
60+
t.true(output.includes(`Faces:`));
61+
t.true(output.includes(`Face #1:`));
62+
});
63+
64+
test(`should detect labels in a local file`, async (t) => {
65+
const output = await runAsync(`${cmd} labels ${files[4].localPath}`, cwd);
66+
t.true(output.includes(`Labels:`));
67+
t.true(output.includes(`cat`));
68+
});
69+
70+
test(`should detect labels in a remote file`, async (t) => {
71+
const output = await runAsync(`${cmd} labels-gcs ${bucketName} ${files[4].name}`, cwd);
72+
t.true(output.includes(`Labels:`));
73+
t.true(output.includes(`cat`));
74+
});
75+
76+
test(`should detect landmarks in a local file`, async (t) => {
77+
const output = await runAsync(`${cmd} landmarks ${files[1].localPath}`, cwd);
78+
t.true(output.includes(`Landmarks:`));
79+
t.true(output.includes(`Palace of Fine Arts`));
80+
});
81+
82+
test(`should detect landmarks in a remote file`, async (t) => {
83+
const output = await runAsync(`${cmd} landmarks-gcs ${bucketName} ${files[1].name}`, cwd);
84+
t.true(output.includes(`Landmarks:`));
85+
t.true(output.includes(`Palace of Fine Arts`));
86+
});
87+
88+
test(`should detect text in a local file`, async (t) => {
89+
const output = await runAsync(`${cmd} text ${files[3].localPath}`, cwd);
90+
t.true(output.includes(`Text:`));
91+
t.true(output.includes(`System Software Update`));
92+
});
93+
94+
test(`should detect text in a remote file`, async (t) => {
95+
const output = await runAsync(`${cmd} text-gcs ${bucketName} ${files[3].name}`, cwd);
96+
t.true(output.includes(`Text:`));
97+
t.true(output.includes(`System Software Update`));
98+
});
99+
100+
test(`should detect logos in a local file`, async (t) => {
101+
const output = await runAsync(`${cmd} logos ${files[2].localPath}`, cwd);
102+
t.true(output.includes(`Logos:`));
103+
t.true(output.includes(`Google`));
104+
});
105+
106+
test(`should detect logos in a remote file`, async (t) => {
107+
const output = await runAsync(`${cmd} logos-gcs ${bucketName} ${files[2].name}`, cwd);
108+
t.true(output.includes(`Logos:`));
109+
t.true(output.includes(`Google`));
110+
});
111+
112+
test(`should detect properties in a local file`, async (t) => {
113+
const output = await runAsync(`${cmd} properties ${files[1].localPath}`, cwd);
114+
t.true(output.includes(`Colors:`));
115+
t.true(output.split(`\n`).length > 4, `Multiple colors were detected.`);
116+
});
117+
118+
test(`should detect properties in a remote file`, async (t) => {
119+
const output = await runAsync(`${cmd} properties-gcs ${bucketName} ${files[1].name}`, cwd);
120+
t.true(output.includes(`Colors:`));
121+
t.true(output.split(`\n`).length > 4, `Multiple colors were detected.`);
122+
});
123+
124+
test(`should detect safe-search in a local file`, async (t) => {
125+
const output = await runAsync(`${cmd} safe-search ${files[4].localPath}`, cwd);
126+
t.true(output.includes(`Medical:`));
127+
});
128+
129+
test(`should detect safe-search in a remote file`, async (t) => {
130+
const output = await runAsync(`${cmd} safe-search-gcs ${bucketName} ${files[4].name}`, cwd);
131+
t.true(output.includes(`Medical:`));
132+
});

vision/samples/system-test/faceDetection.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const inputFile = path.join(__dirname, `../resources`, `face.png`);
5252
const outputFile = path.join(__dirname, `../../vision`, `out.png`);
5353

5454
test.before(stubConsole);
55-
test.after(restoreConsole);
55+
test.after.always(restoreConsole);
5656

5757
test.cb(`should detect faces`, (t) => {
5858
faceDetectionExample.main(inputFile, outputFile, MockCanvas, (err, faces) => {

vision/samples/system-test/labelDetection.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const labelDetectionSample = require(`../labelDetection`);
2323
const inputFile = path.join(__dirname, `../resources`, `cat.jpg`);
2424

2525
test.before(stubConsole);
26-
test.after(restoreConsole);
26+
test.after.always(restoreConsole);
2727

2828
test.cb(`should detect labels`, (t) => {
2929
labelDetectionSample.main(inputFile, (err, labels) => {

0 commit comments

Comments
 (0)