How to use the @google-cloud/vision.ImageAnnotatorClient function in @google-cloud/vision

To help you get started, we’ve selected a few @google-cloud/vision examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github googleapis / nodejs-vision / samples / faceDetection.js View on Github external
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START vision_face_detection_tutorial_imports]
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
const vision = require('@google-cloud/vision');
// [END vision_face_detection_tutorial_imports]
// [START vision_face_detection_tutorial_client]
// Creates a client
const client = new vision.ImageAnnotatorClient();

const fs = require('fs');
// [END vision_face_detection_tutorial_client]

/**
 * Uses the Vision API to detect faces in the given file.
 */
// [START vision_face_detection_tutorial_send_request]
async function detectFaces(inputFile) {
  // Make a call to the Vision API to detect the faces
  const request = {image: {source: {filename: inputFile}}};
  const results = await client.faceDetection(request);
  const faces = results[0].faceAnnotations;
  const numFaces = faces.length;
  console.log(`Found ${numFaces} face${numFaces === 1 ? '' : 's'}.`);
  return faces;
github GoogleCloudPlatform / serverless-photosharing-workshop / functions / image-analysis / nodejs / index.js View on Github external
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const vision = require('@google-cloud/vision');
const Storage = require('@google-cloud/storage');
const Firestore = require('@google-cloud/firestore');

const client = new vision.ImageAnnotatorClient();

exports.vision_analysis = async (event, context) => {
    console.log(`Event: ${JSON.stringify(event)}`);

    const filename = event.name;
    const filebucket = event.bucket;

    console.log(`New picture uploaded ${filename} in ${filebucket}`);

    const request = {
        image: { source: { imageUri: `gs://${filebucket}/${filename}` } },
        features: [
            { type: 'LABEL_DETECTION' },
            { type: 'IMAGE_PROPERTIES' },
            { type: 'SAFE_SEARCH_DETECTION' }
        ]
github googleapis / nodejs-vision / samples / productSearch / similarProducts.js View on Github external
async function getSimilarProductsFile(
  projectId,
  location,
  productSetId,
  productCategory,
  filePath,
  filter
) {
  // Imports the Google Cloud client library
  const vision = require('@google-cloud/vision');
  const fs = require('fs');
  // Creates a client
  const productSearchClient = new vision.ProductSearchClient();
  const imageAnnotatorClient = new vision.ImageAnnotatorClient();

  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const projectId = 'nodejs-docs-samples';
  // const location = 'us-west1';
  // const productSetId = 'indexed_product_set_id_for_testing';
  // const productCategory = 'apparel';
  // const filePath = './resources/shoes_1.jpg';
  // const filter = '';
  const productSetPath = productSearchClient.productSetPath(
    projectId,
    location,
    productSetId
  );
  const content = fs.readFileSync(filePath, 'base64');
github GoogleCloudPlatform / cloud-functions-gmail-nodejs / lib / helpers.js View on Github external
* Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

const cheerio = require('cheerio');
const google = require('googleapis');
const gmail = google.gmail('v1');
const oauth = require('./oauth');
const pify = require('pify');
const Vision = require('@google-cloud/vision');
const visionClient = new Vision.ImageAnnotatorClient();

/**
 * Get base64-encoded image attachments in a GMail message
 * @param message The GMail message to extract images from
 * @returns A promise containing a list of base64-encoded images
 */
const _getImageAttachments = (message) => {
  // Get attachment data
  const attachmentIds = message.payload.parts
    .filter(x => x.mimeType && x.mimeType.includes('image'))
    .map(x => x.body.attachmentId);

  // Return base64-encoded images
  return Promise.all(attachmentIds.map(attachmentId => {
    return pify(gmail.users.messages.attachments.get)({
      auth: oauth.client,
github googleapis / nodejs-vision / samples / async-batch-annotate-images.js View on Github external
async function main(inputImageUri, outputUri) {
  // [START vision_async_batch_annotate_images_beta]

  // Imports the Google Cloud client libraries
  const {ImageAnnotatorClient} = require('@google-cloud/vision').v1p4beta1;

  // Creates a client
  const client = new ImageAnnotatorClient();

  /**
   * TODO(developer): Uncomment the following lines before running the sample.
   */
  // GCS path where the image resides
  // const inputImageUri = 'gs://my-bucket/my_image.jpg';
  // GCS path where to store the output json
  // const outputUri = 'gs://mybucket/out/'

  const features = [
    {type: 'DOCUMENT_LABEL_DETECTION'},
    {type: 'DOCUMENT_TEXT_DETECTION'},
    {type: 'DOCUMENT_IMAGE_DETECTION'},
  ];

  const outputConfig = {
github GoogleCloudPlatform / node-red-contrib-google-cloud / vision.js View on Github external
node.error(err);
            }
        } // Input

        // We must have EITHER credentials or a keyFilename.  If neither are supplied, that
        // is an error.  If both are supplied, then credentials will be used.
        if (credentials) {
            imageAnnotatorClient = new vision.ImageAnnotatorClient({
                "credentials": credentials
            });
        } else if (keyFilename) {
            imageAnnotatorClient = new vision.ImageAnnotatorClient({
                "keyFilename": keyFilename
            });
        } else {
            imageAnnotatorClient = new vision.ImageAnnotatorClient({});
        }

        node.on("input", Input); // Register the handler to be invoked when a new message is to be processed.

    } // VisionNode
github googleapis / nodejs-vision / samples / batch-annotate-files.js View on Github external
async function main(fileName) {
  // [START vision_batch_annotate_files_beta]
  // Imports the Google Cloud client libraries
  const {ImageAnnotatorClient} = require('@google-cloud/vision').v1p4beta1;
  const fs = require('fs');
  const {promisify} = require('util');
  const readFileAsync = promisify(fs.readFile);

  // Creates a client
  const client = new ImageAnnotatorClient();

  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const fileName = `/path/to/localDocument.pdf`;

  const inputConfig = {
    // Other supported mime_types: image/tiff' or 'image/gif'
    mimeType: 'application/pdf',
    content: await readFileAsync(fileName),
  };
  const features = [{type: 'DOCUMENT_TEXT_DETECTION'}];
  const request = {
    requests: [
      {
        inputConfig,
github axa-group / Parsr / server / src / input / google-vision / GoogleVisionExtractor.ts View on Github external
private async execute(inputFile: string) {
		const client = new vision.ImageAnnotatorClient();
		const result: GoogleVisionResponse = await client.documentTextDetection(inputFile);

		if (result[0].error) {
			const e = result[0].error;
			let details = '';

			if (e.details.length > 0) {
				details = ` Details: \n${e.details.join('\n')}`;
			}
			throw new Error(`Google Vision Error #${e.code}: ${e.message}${details}`);
		}

		const pages: Page[] = [];
		let pageNumber = 1;
		let order = 1;