Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
updated samples code to use async await
Browse files Browse the repository at this point in the history
  • Loading branch information
vijay-qlogic committed Oct 25, 2018
1 parent 9b3786b commit c34c822
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 640 deletions.
660 changes: 258 additions & 402 deletions samples/detect.js

Large diffs are not rendered by default.

239 changes: 108 additions & 131 deletions samples/detect.v1p1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

'use strict';

function detectFulltext(fileName) {
async function detectFulltext(fileName) {
// [START vision_detect_document]
// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision').v1p1beta1;
Expand All @@ -29,45 +29,43 @@ function detectFulltext(fileName) {
// const fileName = 'Local image file, e.g. /path/to/image.png';

// Performs label detection on the local file
client
.textDetection(fileName)
.then(results => {
const pages = results[0].fullTextAnnotation.pages;
pages.forEach(page => {
page.blocks.forEach(block => {
const blockWords = [];
block.paragraphs.forEach(paragraph => {
paragraph.words.forEach(word => blockWords.push(word));
console.log(`Paragraph confidence: ${paragraph.confidence}`);
});

let blockText = '';
const blockSymbols = [];
blockWords.forEach(word => {
word.symbols.forEach(symbol => blockSymbols.push(symbol));
let wordText = '';
word.symbols.forEach(symbol => {
wordText = wordText + symbol.text;
console.log(`Symbol text: ${symbol.text}`);
console.log(`Symbol confidence: ${symbol.confidence}`);
});
console.log(`Word text: ${wordText}`);
console.log(`Word confidence: ${word.confidence}`);
blockText = blockText + ` ${wordText}`;
});

console.log(`Block text: ${blockText}`);
console.log(`Block confidence: ${block.confidence}`);
const [
{
fullTextAnnotation: {pages},
},
] = await client.textDetection(fileName);

pages.forEach(page => {
page.blocks.forEach(block => {
const blockWords = [];
block.paragraphs.forEach(paragraph => {
paragraph.words.forEach(word => blockWords.push(word));
console.log(`Paragraph confidence: ${paragraph.confidence}`);
});

let blockText = '';
const blockSymbols = [];
blockWords.forEach(word => {
word.symbols.forEach(symbol => blockSymbols.push(symbol));
let wordText = '';
word.symbols.forEach(symbol => {
wordText = wordText + symbol.text;
console.log(`Symbol text: ${symbol.text}`);
console.log(`Symbol confidence: ${symbol.confidence}`);
});
console.log(`Word text: ${wordText}`);
console.log(`Word confidence: ${word.confidence}`);
blockText = blockText + ` ${wordText}`;
});
})
.catch(err => {
console.error('ERROR:', err);

console.log(`Block text: ${blockText}`);
console.log(`Block confidence: ${block.confidence}`);
});
});
// [END vision_detect_document]
}

function detectSafeSearch(fileName) {
async function detectSafeSearch(fileName) {
// [START vision_safe_search_detection]
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision').v1p1beta1;
Expand All @@ -81,25 +79,20 @@ function detectSafeSearch(fileName) {
// const fileName = 'Local image file, e.g. /path/to/image.png';

// Performs safe search detection on the local file
client
.safeSearchDetection(fileName)
.then(results => {
const detections = results[0].safeSearchAnnotation;

console.log('Safe search:');
console.log(`Adult: ${detections.adult}`);
console.log(`Medical: ${detections.medical}`);
console.log(`Spoof: ${detections.spoof}`);
console.log(`Violence: ${detections.violence}`);
console.log(`Racy: ${detections.racy}`);
})
.catch(err => {
console.error('ERROR:', err);
});
const [{safeSearchAnnotation: detections}] = await client.safeSearchDetection(
fileName
);

console.log('Safe search:');
console.log(`Adult: ${detections.adult}`);
console.log(`Medical: ${detections.medical}`);
console.log(`Spoof: ${detections.spoof}`);
console.log(`Violence: ${detections.violence}`);
console.log(`Racy: ${detections.racy}`);
// [END vision_safe_search_detection]
}

function detectWeb(fileName) {
async function detectWeb(fileName) {
// [START vision_web_detection]
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision').v1p1beta1;
Expand All @@ -113,87 +106,79 @@ function detectWeb(fileName) {
// const fileName = 'Local image file, e.g. /path/to/image.png';

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

if (webDetection.bestGuessLabels.length) {
webDetection.bestGuessLabels.forEach(label => {
console.log(`Best guess label: ${label.label}`);
});
}
// const [{webDetection}] = await client.webDetection(fileName);
const [{webDetection}] = await client.webDetection(fileName);

if (webDetection.pagesWithMatchingImages.length) {
const pages = webDetection.pagesWithMatchingImages;
console.log(`Pages with matching images found: ${pages.length}`);

pages.forEach(page => {
console.log(`Page url: ${page.url}`);

if (page.fullMatchingImages.length) {
const fullMatchingImages = page.fullMatchingImages;
console.log(`Full Matches found: ${fullMatchingImages.length}`);
fullMatchingImages.forEach(image => {
console.log(`Image url: ${image.url}`);
});
}

if (page.partialMatchingImages.length) {
const partialMatchingImages = page.partialMatchingImages;
console.log(
`Partial Matches found: ${partialMatchingImages.length}`
);
partialMatchingImages.forEach(image => {
console.log(`Image url: ${image.url}`);
});
}
});
}
if (webDetection.bestGuessLabels.length) {
webDetection.bestGuessLabels.forEach(label => {
console.log(`Best guess label: ${label.label}`);
});
}

if (webDetection.fullMatchingImages.length) {
console.log(
`Full matches found: ${webDetection.fullMatchingImages.length}`
);
webDetection.fullMatchingImages.forEach(image => {
console.log(` Image url: ${image.url}`);
});
}
if (webDetection.pagesWithMatchingImages.length) {
const pages = webDetection.pagesWithMatchingImages;
console.log(`Pages with matching images found: ${pages.length}`);

if (webDetection.partialMatchingImages.length) {
console.log(
`Partial matches found: ${webDetection.partialMatchingImages.length}`
);
webDetection.partialMatchingImages.forEach(image => {
console.log(` Image url: ${image.url}`);
});
}
pages.forEach(page => {
console.log(`Page url: ${page.url}`);

if (webDetection.webEntities.length) {
console.log(`Web entities found: ${webDetection.webEntities.length}`);
webDetection.webEntities.forEach(webEntity => {
console.log(` Score: ${webEntity.score}`);
console.log(` Description: ${webEntity.description}`);
if (page.fullMatchingImages.length) {
const fullMatchingImages = page.fullMatchingImages;
console.log(`Full Matches found: ${fullMatchingImages.length}`);
fullMatchingImages.forEach(image => {
console.log(`Image url: ${image.url}`);
});
}

if (webDetection.visuallySimilarImages.length) {
const visuallySimilarImages = webDetection.visuallySimilarImages;
console.log(
`Visually similar images found: ${visuallySimilarImages.length}`
);
visuallySimilarImages.forEach(image => {
console.log(` Image url: ${image.url}`);
if (page.partialMatchingImages.length) {
const partialMatchingImages = page.partialMatchingImages;
console.log(`Partial Matches found: ${partialMatchingImages.length}`);
partialMatchingImages.forEach(image => {
console.log(`Image url: ${image.url}`);
});
}
})
.catch(err => {
console.error('ERROR:', err);
});
}

if (webDetection.fullMatchingImages.length) {
console.log(
`Full matches found: ${webDetection.fullMatchingImages.length}`
);
webDetection.fullMatchingImages.forEach(image => {
console.log(` Image url: ${image.url}`);
});
}

if (webDetection.partialMatchingImages.length) {
console.log(
`Partial matches found: ${webDetection.partialMatchingImages.length}`
);
webDetection.partialMatchingImages.forEach(image => {
console.log(` Image url: ${image.url}`);
});
}

if (webDetection.webEntities.length) {
console.log(`Web entities found: ${webDetection.webEntities.length}`);
webDetection.webEntities.forEach(webEntity => {
console.log(` Score: ${webEntity.score}`);
console.log(` Description: ${webEntity.description}`);
});
}

if (webDetection.visuallySimilarImages.length) {
const visuallySimilarImages = webDetection.visuallySimilarImages;
console.log(
`Visually similar images found: ${visuallySimilarImages.length}`
);
visuallySimilarImages.forEach(image => {
console.log(` Image url: ${image.url}`);
});
}
// [END vision_web_detection]
}

function detectWebEntitiesIncludingGeoResults(fileName) {
async function detectWebEntitiesIncludingGeoResults(fileName) {
// [START vision_web_entities_include_geo_results]
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision').v1p1beta1;
Expand All @@ -220,19 +205,11 @@ function detectWebEntitiesIncludingGeoResults(fileName) {
};

// Performs safe search detection on the local file
client
.webDetection(request)
.then(results => {
const webDetection = results[0].webDetection;

webDetection.webEntities.forEach(entity => {
console.log(`Score: ${entity.score}`);
console.log(`Description: ${entity.description}`);
});
})
.catch(err => {
console.error('ERROR:', err);
});
const [{webDetection}] = await client.webDetection(request);
webDetection.webEntities.forEach(entity => {
console.log(`Score: ${entity.score}`);
console.log(`Description: ${entity.description}`);
});
// [END vision_web_entities_include_geo_results]
}

Expand Down
27 changes: 7 additions & 20 deletions samples/detect.v1p3beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

'use strict';

function detectHandwritingOCR(fileName) {
async function detectHandwritingOCR(fileName) {
// [START vision_handwritten_ocr_beta]
// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision').v1p3beta1;
Expand All @@ -39,19 +39,13 @@ function detectHandwritingOCR(fileName) {
languageHints: ['en-t-i0-handwrit'],
},
};
client
.documentTextDetection(request)
.then(results => {
const fullTextAnnotation = results[0].fullTextAnnotation;
console.log(`Full text: ${fullTextAnnotation.text}`);
})
.catch(err => {
console.error('ERROR:', err);
});

const [{fullTextAnnotation}] = await client.documentTextDetection(request);
console.log(`Full text: ${fullTextAnnotation.text}`);
// [END vision_handwritten_ocr_beta]
}

function detectHandwritingGCS(uri) {
async function detectHandwritingGCS(uri) {
// [START vision_handwritten_ocr_gcs_beta]
// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision').v1p3beta1;
Expand All @@ -74,15 +68,8 @@ function detectHandwritingGCS(uri) {
},
};

client
.documentTextDetection(request)
.then(results => {
const fullTextAnnotation = results[0].fullTextAnnotation;
console.log(`Full text: ${fullTextAnnotation.text}`);
})
.catch(err => {
console.error('ERROR:', err);
});
const [{fullTextAnnotation}] = await client.documentTextDetection(request);
console.log(`Full text: ${fullTextAnnotation.text}`);
// [END vision_handwritten_ocr_gcs_beta]
}

Expand Down
Loading

0 comments on commit c34c822

Please sign in to comment.