question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Face detection don't work on AWS Lambda due to node-canvas not working on node 8.10

See original GitHub issue

Hi there!

I am trying to get this up and running on AWS Lambda but are having trouble as it seem the ‘canvas’ package is broken on node 8.10. I am trying to get some face detection going.

https://github.com/Automattic/node-canvas/issues/1252#issuecomment-437598572

It looks like its kind of a pain to try and run any other version of node on AWS Lambda, so I started trying to use it without the canvas package. As you mention in the README and I also noticed that the faceapi.locateFaces() function also takes a Tensor4D class as input. I have never used Tensorflow and I am a little confused as how to turn a ArrayBuffer from axios into a correctly shaped Tensor4D object.

I am fetching a jpeg image using axios.

I found the tf.tensor4d function but not sure what shape and dtype it should be.

Do you have any idea?

My code so far:

const { data: imageBuffer } = await axios.get(url, {
	responseType: 'arraybuffer'
})
const imageTensor = tf.tensor4d(imageBuffer, [?, ?, ? ,?])
const faces = await detector.locateFaces(imageTensor)

Error messages look similar to this one:

Error: Based on the provided shape, [1,2,3,4], and dtype float32, the tensor should have 24 values but has 68695

Any help is greatly appreciated!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:24 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bobmoffcommented, Nov 21, 2018

Thanks!

Tried the get-pixels npm package and it works!

get-pixels return a 4 channel array all the time, even for jpegs 🤷 , so had to remove the alpha channel.

const pixels = await new Promise<Pixels>(resolve => {
	getPixels(url, (err, pixels: Pixels) => {
		if (err) {
			throw err
		}
		console.log('pixels:', pixels)
		resolve(pixels)
	})
})

// remove alpha channel
const RGBValues = []
pixels.data.forEach((px, i) => {
	if ((i + 1) % 4 != 0) {
		RGBValues.push(px)
	}
})
const imageTensor = tf.tensor3d(RGBValues, [pixels.shape[1], pixels.shape[0], 3]) as any
const faces = await detector.locateFaces(imageTensor)

Now to the task of getting it up on Lambda without breaking their code size limit, wish me luck!

0reactions
nasr18commented, Sep 9, 2021

@bobmoff I have successfully deployed in Lambda using containers. But I’m facing one issue. I’m just doing faceapi.detectSingleFace().withFaceLandmarks().withFaceDescriptor() in lambda to detect single face and returning that output as a response via API Gateway. I can’t able to return without doing JSON.stringify(result) and If I do stringify the result, that result not working in final comparison. I dont know what I’m doing wrong.

update: I managed to fix it. And its working fine now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS Lambda function errors in Node.js
This page describes how to view Lambda function invocation errors for the Node.js runtime using the Lambda console and the AWS CLI.
Read more >
Getting node-canvas to run on AWS Lambda - Jamie Tanna
Some common issues that occur when using node-canvas on AWS Lambda, and how to solve them.
Read more >
Couchbase throws an error when Nodejs 8.10 AWS Lambda ...
I am using Docker Engine 18.0.09.1 with the Serverless CLI to package and deploy AWS Lambda CRUD functions. The deployment succeeds in ...
Read more >
AWS Facial Recognition with Rekognition and Node - YouTube
Let's build a tool that allows us to search for people using only their faces !We'll build a node app that let's us...
Read more >
How to test and develop AWS lambda functions locally with ...
In this video we build a nodejs lambda function while running and testing it locally.The lambda function that gets arguments from query ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found