Skip to content

Commit

Permalink
Adds vision 1.1 features (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss authored and Ace Nassri committed Mar 6, 2017
1 parent 53c2d0a commit 7caf3e5
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 2 deletions.
241 changes: 241 additions & 0 deletions vision/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,205 @@ function detectSafeSearchGCS (bucketName, fileName) {
// [END vision_safe_search_detection_gcs]
}

function detectCropHints (fileName) {
// [START vision_crop_hint_detection]

// Imports the Google Cloud client library
const Vision = require('@google-cloud/vision');

// Instantiates a client
const vision = Vision();

// The path to the local image file, e.g. "/path/to/image.png"
// const fileName = 'my-file.jpg';

// Find crop hints for the local file
vision.detectCrops(fileName)
.then((data) => {
const cropHints = data[0];

cropHints.forEach((hintBounds, hintIdx) => {
console.log(`Crop Hint ${hintIdx}:`);
hintBounds.forEach((bound, boundIdx) => {
console.log(` Bound ${boundIdx}: (${bound.x}, ${bound.y})`);
});
});
});
// [END vision_crop_hint_detection]
}

function detectCropHintsGCS (bucketName, fileName) {
// [START vision_crop_hint_detection_gcs]

// Imports the Google Cloud client libraries
const Storage = require('@google-cloud/storage');
const Vision = require('@google-cloud/vision');

// Instantiates clients
const storage = Storage();
const vision = Vision();

// The name of the bucket where the file resides, e.g. "my-bucket"
// const bucketName = 'my-bucket';

// The path to the file within the bucket, e.g. "path/to/image.png"
// const fileName = 'my-file.jpg';

// Find crop hints for the remote file
vision.detectCrops(storage.bucket(bucketName).file(fileName))
.then((data) => {
const cropHints = data[0];

cropHints.forEach((hintBounds, hintIdx) => {
console.log(`Crop Hint ${hintIdx}:`);
hintBounds.forEach((bound, boundIdx) => {
console.log(` Bound ${boundIdx}: (${bound.x}, ${bound.y})`);
});
});
});
// [END vision_crop_hint_detection_gcs]
}

function detectWeb (fileName) {
// [START vision_web_detection]

// Imports the Google Cloud client library
const Vision = require('@google-cloud/vision');

// Instantiates a client
const vision = Vision();

// The path to the local image file, e.g. "/path/to/image.png"
// const fileName = 'my-file.jpg';

// Detect similar images on the web to a local file
vision.detectSimilar(fileName)
.then((data) => {
const results = data[1].responses[0].webDetection;

if (results.fullMatchingImages.length > 0) {
console.log(`Full matches found: ${results.fullMatchingImages.length}`);
results.fullMatchingImages.forEach((image) => {
console.log(` URL: ${image.url}`);
console.log(` Score: ${image.score}`);
});
}

if (results.partialMatchingImages.length > 0) {
console.log(`Partial matches found: ${results.partialMatchingImages.length}`);
results.partialMatchingImages.forEach((image) => {
console.log(` URL: ${image.url}`);
console.log(` Score: ${image.score}`);
});
}

if (results.webEntities.length > 0) {
console.log(`Web entities found: ${results.webEntities.length}`);
results.webEntities.forEach((webEntity) => {
console.log(` Description: ${webEntity.description}`);
console.log(` Score: ${webEntity.score}`);
});
}
});
// [END vision_web_detection]
}

function detectWebGCS (bucketName, fileName) {
// [START vision_web_detection_gcs]

// Imports the Google Cloud client libraries
const Storage = require('@google-cloud/storage');
const Vision = require('@google-cloud/vision');

// Instantiates clients
const storage = Storage();
const vision = Vision();

// The name of the bucket where the file resides, e.g. "my-bucket"
// const bucketName = 'my-bucket';

// The path to the file within the bucket, e.g. "path/to/image.png"
// const fileName = 'my-file.jpg';

// Detect similar images on the web to a remote file
vision.detectSimilar(storage.bucket(bucketName).file(fileName))
.then((data) => {
const results = data[1].responses[0].webDetection;

if (results.fullMatchingImages.length > 0) {
console.log(`Full matches found: ${results.fullMatchingImages.length}`);
results.fullMatchingImages.forEach((image) => {
console.log(` URL: ${image.url}`);
console.log(` Score: ${image.score}`);
});
}

if (results.partialMatchingImages.length > 0) {
console.log(`Partial matches found: ${results.partialMatchingImages.length}`);
results.partialMatchingImages.forEach((image) => {
console.log(` URL: ${image.url}`);
console.log(` Score: ${image.score}`);
});
}

if (results.webEntities.length > 0) {
console.log(`Web entities found: ${results.webEntities.length}`);
results.webEntities.forEach((webEntity) => {
console.log(` Description: ${webEntity.description}`);
console.log(` Score: ${webEntity.score}`);
});
}
});
// [END vision_web_detection_gcs]
}

function detectFulltext (fileName) {
// [START vision_fulltext_detection]

// Imports the Google Cloud client library
const Vision = require('@google-cloud/vision');

// Instantiates a client
const vision = Vision();

// The path to the local image file, e.g. "/path/to/image.png"
// const fileName = 'my-file.jpg';

// // Read a local image as a text document
vision.readDocument(fileName)
.then((data) => {
const results = data[1].responses[0].fullTextAnnotation;
console.log(results.text);
});
// [END vision_fulltext_detection]
}

function detectFulltextGCS (bucketName, fileName) {
// [START vision_fulltext_detection_gcs]

// Imports the Google Cloud client libraries
const Storage = require('@google-cloud/storage');
const Vision = require('@google-cloud/vision');

// Instantiates clients
const storage = Storage();
const vision = Vision();

// The name of the bucket where the file resides, e.g. "my-bucket"
// const bucketName = 'my-bucket';

// The path to the file within the bucket, e.g. "path/to/image.png"
// const fileName = 'my-file.jpg';

// Read a remote image as a text document
vision.readDocument(storage.bucket(bucketName).file(fileName))
.then((data) => {
const results = data[1].responses[0].fullTextAnnotation;
console.log(results.text);
});
// [END vision_fulltext_detection_gcs]
}

require(`yargs`)
.demand(1)
.command(
Expand Down Expand Up @@ -460,6 +659,42 @@ require(`yargs`)
{},
(opts) => detectSafeSearchGCS(opts.bucket, opts.fileName)
)
.command(
`crops <fileName>`,
`Detects crop hints in a local image file.`,
{},
(opts) => detectCropHints(opts.fileName)
)
.command(
`crops-gcs <bucket> <fileName>`,
`Detects crop hints in an image in Google Cloud Storage.`,
{},
(opts) => detectCropHintsGCS(opts.bucket, opts.fileName)
)
.command(
`web <fileName>`,
`Finds similar photos on the web for a local image file.`,
{},
(opts) => detectWeb(opts.fileName)
)
.command(
`web-gcs <bucket> <fileName>`,
`Finds similar photos on the web for an image in Google Cloud Storage.`,
{},
(opts) => detectWebGCS(opts.bucket, opts.fileName)
)
.command(
`fulltext <fileName>`,
`Extracts full text from a local image file.`,
{},
(opts) => detectFulltext(opts.fileName)
)
.command(
`fulltext-gcs <bucket> <fileName>`,
`Extracts full text from an image in Google Cloud Storage.`,
{},
(opts) => detectFulltextGCS(opts.bucket, opts.fileName)
)
.example(`node $0 faces ./resources/face_no_surprise.jpg`)
.example(`node $0 faces-gcs my-bucket your-image.jpg`)
.example(`node $0 labels ./resources/wakeupcat.jpg`)
Expand All @@ -474,6 +709,12 @@ require(`yargs`)
.example(`node $0 properties-gcs my-bucket your-image.jpg`)
.example(`node $0 safe-search ./resources/wakeupcat.jpg`)
.example(`node $0 safe-search-gcs my-bucket your-image.jpg`)
.example(`node $0 crops ./resources/wakeupcat.jpg`)
.example(`node $0 crops-gcs my-bucket your-image.jpg`)
.example(`node $0 web ./resources/wakeupcat.jpg`)
.example(`node $0 web-gcs my-bucket your-image.jpg`)
.example(`node $0 fulltext ./resources/wakeupcat.jpg`)
.example(`node $0 fulltext-gcs my-bucket your-image.jpg`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/vision/docs`)
Expand Down
2 changes: 1 addition & 1 deletion vision/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"@google-cloud/storage": "0.7.0",
"@google-cloud/vision": "0.8.0",
"@google-cloud/vision": "0.9.0",
"async": "2.1.4",
"natural": "0.4.0",
"redis": "2.6.5",
Expand Down
43 changes: 42 additions & 1 deletion vision/system-test/detect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const files = [
`landmark.jpg`,
`logos.png`,
`text.jpg`,
`wakeupcat.jpg`
`wakeupcat.jpg`,
`faulkner.jpg`
].map((name) => {
return {
name,
Expand Down Expand Up @@ -130,3 +131,43 @@ test(`should detect safe-search in a remote file`, async (t) => {
const output = await runAsync(`${cmd} safe-search-gcs ${bucketName} ${files[4].name}`, cwd);
t.true(output.includes(`Medical:`));
});

test(`should detect crop hints in a local file`, async (t) => {
const output = await runAsync(`${cmd} crops ${files[2].localPath}`, cwd);
t.true(output.includes(`Crop Hint 0:`));
t.true(output.includes(`Bound 2: (280, 43)`));
});

test(`should detect crop hints in a remote file`, async (t) => {
const output = await runAsync(`${cmd} crops-gcs ${bucketName} ${files[2].name}`, cwd);
t.true(output.includes(`Crop Hint 0:`));
t.true(output.includes(`Bound 2: (280, 43)`));
});

test(`should detect similar web images in a local file`, async (t) => {
const output = await runAsync(`${cmd} web ${files[5].localPath}`, cwd);
t.true(output.includes('Full matches found: 5'));
t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/'));
t.true(output.includes('Partial matches found: 5'));
t.true(output.includes('Web entities found: 5'));
t.true(output.includes('Description: Google Cloud Platform'));
});

test(`should detect similar web images in a remote file`, async (t) => {
const output = await runAsync(`${cmd} web-gcs ${bucketName} ${files[5].name}`, cwd);
t.true(output.includes('Full matches found: 5'));
t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/'));
t.true(output.includes('Partial matches found: 5'));
t.true(output.includes('Web entities found: 5'));
t.true(output.includes('Description: Google Cloud Platform'));
});

test(`should read a document from a local file`, async (t) => {
const output = await runAsync(`${cmd} fulltext ${files[2].localPath}`, cwd);
t.true(output.includes('Google Cloud Platform'));
});

test(`should read a document from a remote file`, async (t) => {
const output = await runAsync(`${cmd} fulltext-gcs ${bucketName} ${files[2].name}`, cwd);
t.true(output.includes('Google Cloud Platform'));
});

0 comments on commit 7caf3e5

Please sign in to comment.