Skip to content

Commit 17e17d7

Browse files
fix(samples): use pureimage instead of canvas (#774)
1 parent 1c1e8e2 commit 17e17d7

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

vision/samples/faceDetection.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,20 @@ async function detectFaces(inputFile) {
4747
* Draws a polygon around the faces, then saves to outputFile.
4848
*/
4949
// [START vision_face_detection_tutorial_process_response]
50-
async function highlightFaces(inputFile, faces, outputFile, Canvas) {
51-
const {promisify} = require('util');
52-
const readFile = promisify(fs.readFile);
53-
const image = await readFile(inputFile);
54-
const Image = Canvas.Image;
55-
// Open the original image into a canvas
56-
const img = new Image();
57-
img.src = image;
58-
const canvas = new Canvas.Canvas(img.width, img.height);
59-
const context = canvas.getContext('2d');
60-
context.drawImage(img, 0, 0, img.width, img.height);
50+
async function highlightFaces(inputFile, faces, outputFile, PImage) {
51+
// Open the original image
52+
const stream = fs.createReadStream(inputFile);
53+
let promise;
54+
if (inputFile.match(/\.jpg$/)) {
55+
promise = PImage.decodeJPEGFromStream(stream);
56+
} else if (inputFile.match(/\.png$/)) {
57+
promise = PImage.decodePNGFromStream(stream);
58+
} else {
59+
throw new Error(`Unknown filename extension ${inputFile}`);
60+
}
61+
const img = await promise;
62+
const context = img.getContext('2d');
63+
context.drawImage(img, 0, 0, img.width, img.height, 0, 0);
6164

6265
// Now draw boxes around all the faces
6366
context.strokeStyle = 'rgba(0,255,0,0.8)';
@@ -71,8 +74,10 @@ async function highlightFaces(inputFile, faces, outputFile, Canvas) {
7174
if (i === 0) {
7275
origX = bounds.x;
7376
origY = bounds.y;
77+
context.moveTo(bounds.x, bounds.y);
78+
} else {
79+
context.lineTo(bounds.x, bounds.y);
7480
}
75-
context.lineTo(bounds.x, bounds.y);
7681
});
7782
context.lineTo(origX, origY);
7883
context.stroke();
@@ -81,25 +86,18 @@ async function highlightFaces(inputFile, faces, outputFile, Canvas) {
8186
// Write the result to a file
8287
console.log(`Writing to file ${outputFile}`);
8388
const writeStream = fs.createWriteStream(outputFile);
84-
const pngStream = canvas.pngStream();
85-
86-
await new Promise((resolve, reject) => {
87-
pngStream
88-
.on('data', chunk => writeStream.write(chunk))
89-
.on('error', reject)
90-
.on('end', resolve);
91-
});
89+
await PImage.encodePNGToStream(img, writeStream);
9290
}
9391
// [END vision_face_detection_tutorial_process_response]
9492

9593
// Run the example
9694
// [START vision_face_detection_tutorial_run_application]
9795
async function main(inputFile, outputFile) {
98-
const Canvas = require('canvas');
96+
const PImage = require('pureimage');
9997
outputFile = outputFile || 'out.png';
10098
const faces = await detectFaces(inputFile);
10199
console.log('Highlighting...');
102-
await highlightFaces(inputFile, faces, outputFile, Canvas);
100+
await highlightFaces(inputFile, faces, outputFile, PImage);
103101
console.log('Finished!');
104102
}
105103
// [END vision_face_detection_tutorial_run_application]

vision/samples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
},
1515
"dependencies": {
1616
"@google-cloud/vision": "^1.11.0",
17-
"canvas": "^2.0.0",
1817
"mathjs": "^6.0.0",
1918
"natural": "^1.0.0",
19+
"pureimage": "^0.2.1",
2020
"redis": "^3.0.0",
2121
"yargs": "^15.0.0"
2222
},

0 commit comments

Comments
 (0)