|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +// [START auth] |
| 17 | +// You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT |
| 18 | +// environment variables to run this sample. See: |
| 19 | +// https://github.com/GoogleCloudPlatform/gcloud-node/blob/master/docs/authentication.md |
| 20 | +var projectId = process.env.GCLOUD_PROJECT; |
| 21 | + |
| 22 | +// Initialize gcloud |
| 23 | +var gcloud = require('gcloud')({ |
| 24 | + projectId: projectId |
| 25 | +}); |
| 26 | + |
| 27 | +// Get a reference to the vision component |
| 28 | +var vision = gcloud.vision(); |
| 29 | +// [END auth] |
| 30 | + |
| 31 | +var fs = require('fs'); |
| 32 | +var Canvas = require('canvas'); |
| 33 | +var Image = Canvas.Image; |
| 34 | + |
| 35 | +/** |
| 36 | + * Uses the Vision API to detect faces in the given file. |
| 37 | + */ |
| 38 | +function detectFaces(inputFile, callback) { |
| 39 | + // Make a call to the Vision API to detect the faces |
| 40 | + vision.detectFaces(inputFile, function (err, faces) { |
| 41 | + if (err) { |
| 42 | + return callback(err); |
| 43 | + } |
| 44 | + var numFaces = faces.length; |
| 45 | + console.log('Found ' + numFaces + (numFaces === 1 ? ' face' : ' faces')); |
| 46 | + callback(null, faces); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Draws a polygon around the faces, then saves to outputFile. |
| 52 | + */ |
| 53 | +function highlightFaces(inputFile, faces, outputFile, callback) { |
| 54 | + fs.readFile(inputFile, function (err, image) { |
| 55 | + if (err) { |
| 56 | + return callback(err); |
| 57 | + } |
| 58 | + |
| 59 | + // Open the original image into a canvas |
| 60 | + var img = new Image(); |
| 61 | + img.src = image; |
| 62 | + var canvas = new Canvas(img.width, img.height); |
| 63 | + var context = canvas.getContext('2d'); |
| 64 | + context.drawImage(img, 0, 0, img.width, img.height); |
| 65 | + |
| 66 | + // Now draw boxes around all the faces |
| 67 | + context.strokeStyle = 'rgba(0,255,0,0.8)'; |
| 68 | + context.lineWidth = '5'; |
| 69 | + |
| 70 | + faces.forEach(function (face) { |
| 71 | + context.beginPath(); |
| 72 | + face.bounds.face.forEach(function (bounds) { |
| 73 | + context.lineTo(bounds.x, bounds.y); |
| 74 | + }); |
| 75 | + context.lineTo(face.bounds.face[0].x, face.bounds.face[0].y); |
| 76 | + context.stroke(); |
| 77 | + }); |
| 78 | + |
| 79 | + // Write the result to a file |
| 80 | + console.log('Writing to file ' + outputFile); |
| 81 | + var writeStream = fs.createWriteStream(outputFile); |
| 82 | + var pngStream = canvas.pngStream(); |
| 83 | + |
| 84 | + pngStream.on('data', function (chunk) { |
| 85 | + writeStream.write(chunk); |
| 86 | + }); |
| 87 | + pngStream.on('error', console.log); |
| 88 | + pngStream.on('end', callback); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +// Run the example |
| 93 | +function main(inputFile, outputFile, callback) { |
| 94 | + outputFile = outputFile || 'out.png'; |
| 95 | + detectFaces(inputFile, function (err, faces) { |
| 96 | + if (err) { |
| 97 | + return callback(err); |
| 98 | + } |
| 99 | + |
| 100 | + console.log('Highlighting...'); |
| 101 | + highlightFaces(inputFile, faces, outputFile, function (err) { |
| 102 | + if (err) { |
| 103 | + return callback(err); |
| 104 | + } |
| 105 | + console.log('Finished!'); |
| 106 | + callback(null, faces); |
| 107 | + }); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +exports.main = main; |
| 112 | + |
| 113 | +if (module === require.main) { |
| 114 | + if (process.argv.length < 3) { |
| 115 | + console.log('Usage: node faceDetection <inputFile> [outputFile]'); |
| 116 | + process.exit(1); |
| 117 | + } |
| 118 | + exports.main(process.argv[2], process.argv[3], console.log); |
| 119 | +} |
0 commit comments