MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / highlightFaces

Function highlightFaces

vision/faceDetection.js:50–90  ·  view source on GitHub ↗
(inputFile, faces, outputFile, PImage)

Source from the content-addressed store, hash-verified

48 */
49// [START vision_face_detection_tutorial_process_response]
50async 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);
64
65 // Now draw boxes around all the faces
66 context.strokeStyle = 'rgba(0,255,0,0.8)';
67 context.lineWidth = '5';
68
69 faces.forEach(face => {
70 context.beginPath();
71 let origX = 0;
72 let origY = 0;
73 face.boundingPoly.vertices.forEach((bounds, i) => {
74 if (i === 0) {
75 origX = bounds.x;
76 origY = bounds.y;
77 context.moveTo(bounds.x, bounds.y);
78 } else {
79 context.lineTo(bounds.x, bounds.y);
80 }
81 });
82 context.lineTo(origX, origY);
83 context.stroke();
84 });
85
86 // Write the result to a file
87 console.log(`Writing to file ${outputFile}`);
88 const writeStream = fs.createWriteStream(outputFile);
89 await PImage.encodePNGToStream(img, writeStream);
90}
91// [END vision_face_detection_tutorial_process_response]
92
93// Run the example

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected