Git Product home page Git Product logo

google-vision-api-client's Introduction

Google Vision API Client

Vision API is an early trust preview api provide by Google Cloud. This is a node.js client for using google vision api.

Apply for Trust Preview

Please request from Google's request page: https://services.google.com/fb/forms/visionapialpha/

Install

npm install google-vision-api-client

Use

var vision = require('google-vision-api-client');
var requtil = vision.requtil;

//Prepare your service account from trust preview certificated project
var jsonfile = '/path-to-your-service-account.json';

//Initialize the api
vision.init(jsonfile);

//Build the request payloads
var d = requtil.createRequests().addRequest(
	requtil.createRequest('/path-to-your-image.jpg')
		.withFeature('FACE_DETECTION', 3)
		.withFeature('LABEL_DETECTION', 2)
		.build());

//Do query to the api server
vision.query(d, function(e, r, d){
	if(e) console.log('ERROR:', e);
  console.log(JSON.stringify(d));
});

Sample of Detect Face

Let's to try a sample to highlight the faces from the image you gived...

var vision = require('google-vision-api-client');
var requtil = vision.requtil;
var request = require('request');
var Canvas = require('canvas')
  , Image = Canvas.Image
  , fs = require('fs')
  , sizeOf = require('image-size');

var tmpfile = '/tmp/mytmpfile';

var canvas = new Canvas(200, 200)
  , ctx = canvas.getContext('2d')

var jsonfile = '/Users/peihsinsu/.gcpkeys/itri-smart-home/itri-smart-home-33f5a755a360.json';
vision.init(jsonfile);

var outfile = '/tmp/test.jpg';

function getColor(i) {
  var list = ['red', 'yellow', 'pink', 'green', 'blue'];
  return list[i%list.length];
}

exports.draw = draw;
function draw(imgfile, outfile, opts, cb){
	fs.readFile(imgfile, function(err, squid){
		if (err) throw err;
		img = new Image;
		img.src = squid;

    if (opts && opts.filter) {
      opts.filter(ctx);
    }
		ctx.drawImage(img , 0, 0, img.width , img.height );

	  ctx.globalAlpha = .5;

    var i = 0;
    function doit(vec) {
      var color = Math.random()*1000%255;
		  ctx.lineWidth = 4;
			ctx.beginPath();
			ctx.moveTo(vec[0].x, vec[0].y);
			ctx.lineTo(vec[1].x, vec[1].y);
			ctx.lineTo(vec[2].x, vec[2].y);
			ctx.lineTo(vec[3].x, vec[3].y);
			ctx.closePath();
		  ctx.strokeStyle = getColor(i);//'rgb(255,color,0)';
      i++;
			ctx.stroke();
      //ctx.fill();
    }

    opts.vec.forEach(function(v) {
      doit(v)
    });
		fs.writeFileSync(outfile, canvas.toBuffer());
    cb();
	});
}

function main(imgfile) {
	var d = requtil.createRequests().addRequest(
		requtil.createRequest(imgfile)
			.withFeature('FACE_DETECTION', 50)
			//.withFeature('LABEL_DETECTION', 20)
			.build());

	var imgSize = sizeOf(imgfile);
	console.log(imgSize.width, imgSize.height);

	vision.query(d, function(e, r, d){
		if(e) return console.log('ERROR:', e);
		console.log(JSON.stringify(d));

    if(!d.responses[0].faceAnnotations) return;

		//var v = d.responses[0].faceAnnotations[0].boundingPoly.vertices;
		var v = [];
		d.responses[0].faceAnnotations.forEach(function(o){
			v.push(o.boundingPoly.vertices);
		})
		console.log('-->', v);
		canvas.width = imgSize.width;
		canvas.height = imgSize.height;

		draw(imgfile, outfile, {
				vec: v,
				filter: function(ctx) {
					//ctx.strokeStyle = 'rgba(255,0,0,.5)';
				}
			},
			function(err) {
				if(err) console.log('ERROR:', err);
		});
	});
}

//Do the process
if(process.argv[2].indexOf('http') == 0) {
  var url = process.argv[2];
  var req = request.get(url);
  req.pipe(fs.createWriteStream(tmpfile));
  req.on('end', function(){
    main(tmpfile);
  });
} else {
  main(process.argv[2]);
}

Save the code to test.js, then try to excute:

Execute for web resource...

$ node test http://www.kevinparker.com.au/people/p7IGM_images/fullsize/shutterstock_48737587_fs.jpg

Execute for local resource

$ node test /Users/peihsinsu/Pictures/Simon/simon01.jpg

View the image that formated...

$ open /tmp/test.jpg

PS: The sample default output the file to /tmp/test.jpg

Notes

google-vision-api-client's People

Contributors

peihsinsu avatar

Stargazers

Wuxian Zhang avatar Frank Zheng avatar TKHuang avatar Mathias Wulff avatar Trevor Hartman avatar khacanh avatar savdeep singh avatar Sebastien Rousseau avatar Umar Hansa avatar Tegan Snyder avatar Yongsul Kim avatar PJ Ahlberg avatar Nikolai avatar

Watchers

 avatar

google-vision-api-client's Issues

example credentials

tell me about code/example inside itri-smart-home-33f5a755a360.json ?. thanks

Deprecate in favor of official Cloud Vision API clients

Thanks for making this client!

Now, gcloud-node and google-api-nodejs-client both officially support the Cloud Vision API. For the best developer experience, these clients are the recommended way to use the Cloud Vision API in Node.js.

Please consider deprecating google-vision-api-client and pointing to the official clients:

npm deprecate google-vision-api-client "Deprecated in favor of official Cloud Vision API clients: https://github.com/GoogleCloudPlatform/gcloud-node and https://github.com/google/google-api-nodejs-client"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.