@@ -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 ( / \. j p g $ / ) ) {
55+ promise = PImage . decodeJPEGFromStream ( stream ) ;
56+ } else if ( inputFile . match ( / \. p n g $ / ) ) {
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]
9795async 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]
0 commit comments