-
Notifications
You must be signed in to change notification settings - Fork 2k
Upgrades vision to partial-GAPIC #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9219a48
dcd348b
5d718f5
4f8f7fc
8eecc72
62c0383
c00ba7a
a766be3
de3b44b
c308488
c150b77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,21 +33,25 @@ var fs = require('fs'); | |
| */ | ||
| function detectFaces (inputFile, callback) { | ||
| // Make a call to the Vision API to detect the faces | ||
| vision.detectFaces(inputFile, function (err, faces) { | ||
| if (err) { | ||
| return callback(err); | ||
| } | ||
| var numFaces = faces.length; | ||
| console.log('Found ' + numFaces + (numFaces === 1 ? ' face' : ' faces')); | ||
| callback(null, faces); | ||
| }); | ||
| const request = { source: { filename: inputFile } }; | ||
| vision.faceDetection(request) | ||
| .then((results) => { | ||
| const faces = results[0].faceAnnotations; | ||
| var numFaces = faces.length; | ||
| console.log('Found ' + numFaces + (numFaces === 1 ? ' face' : ' faces')); | ||
| callback(null, faces); | ||
| }) | ||
| .catch((err) => { | ||
| console.error('ERROR:', err); | ||
| callback(err); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Draws a polygon around the faces, then saves to outputFile. | ||
| */ | ||
| function highlightFaces (inputFile, faces, outputFile, Canvas, callback) { | ||
| fs.readFile(inputFile, function (err, image) { | ||
| fs.readFile(inputFile, (err, image) => { | ||
| if (err) { | ||
| return callback(err); | ||
| } | ||
|
|
@@ -64,12 +68,18 @@ function highlightFaces (inputFile, faces, outputFile, Canvas, callback) { | |
| context.strokeStyle = 'rgba(0,255,0,0.8)'; | ||
| context.lineWidth = '5'; | ||
|
|
||
| faces.forEach(function (face) { | ||
| faces.forEach((face) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use an arrow function here if you're not going to convert the whole file to arrow functions.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: I converted the whole file to arrow functions. 😛 |
||
| context.beginPath(); | ||
| face.bounds.face.forEach(function (bounds) { | ||
| let origX = 0; | ||
| let origY = 0; | ||
| face.boundingPoly.vertices.forEach((bounds, i) => { | ||
| if (i === 0) { | ||
| origX = bounds.x; | ||
| origY = bounds.y; | ||
| } | ||
| context.lineTo(bounds.x, bounds.y); | ||
| }); | ||
| context.lineTo(face.bounds.face[0].x, face.bounds.face[0].y); | ||
| context.lineTo(origX, origY); | ||
| context.stroke(); | ||
| }); | ||
|
|
||
|
|
@@ -78,7 +88,7 @@ function highlightFaces (inputFile, faces, outputFile, Canvas, callback) { | |
| var writeStream = fs.createWriteStream(outputFile); | ||
| var pngStream = canvas.pngStream(); | ||
|
|
||
| pngStream.on('data', function (chunk) { | ||
| pngStream.on('data', (chunk) => { | ||
| writeStream.write(chunk); | ||
| }); | ||
| pngStream.on('error', console.log); | ||
|
|
@@ -89,13 +99,13 @@ function highlightFaces (inputFile, faces, outputFile, Canvas, callback) { | |
| // Run the example | ||
| function main (inputFile, outputFile, Canvas, callback) { | ||
| outputFile = outputFile || 'out.png'; | ||
| detectFaces(inputFile, function (err, faces) { | ||
| detectFaces(inputFile, (err, faces) => { | ||
| if (err) { | ||
| return callback(err); | ||
| } | ||
|
|
||
| console.log('Highlighting...'); | ||
| highlightFaces(inputFile, faces, outputFile, Canvas, function (err) { | ||
| highlightFaces(inputFile, faces, outputFile, Canvas, (err) => { | ||
| if (err) { | ||
| return callback(err); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add
callback(err);belowconsole.error