A blog about my life, development and projects

Running face-api.js or tfjs-node on a raspberry pi and node.js

Welcome to another blog post. What a year it has been. As usual I haven't gotten to as much blogging as I wanted to.
Today I want to share some of my trial and error and results in getting tfjs-node or Tensorflow.js running on a raspberry pi.

There isn't much documentation on this so I hope that this blog sheds some light on the topic. I really struggled firstly to get tensorflow to build, then secondly to actually run properly.

Although I managed to get it running, it's still very slow on the raspberry pi. My next challenge is to try and speed it up on the PI.

So I'm running the following:

  • Raspberry PI 4, 4GB ram - It's the latest one
  • Node JS 12.13.0 - will show you how to upgrade

By default the new Raspbian image comes with Node.js 10 installed. After some long battles I realized that tfjs-node requires a more recent version of node. Lets run through a few steps to clean up the OS, install some utilities and then finally install and build tensorflow.

Clean Up raspbian Image:

In order to speed things up and free some space on the SD card, lets remove some of the built in stuff that isn't used. Unless you planning on using your pi as a desktop, I don't think having office and wolfram is needed

  • $ sudo apt-get purge wolfram-engine -y
  • $ sudo apt-get purge libreoffice* -y
  • $ sudo apt-get clean
  • $ sudo apt-get autoremove -y
  • $ sudo reboot

Updating raspbian:

Run the following commands to ensure that your image is running the latest version of raspbian:

  • $ sudo apt-get update & sudo apt-get -y dist-upgrade
  • $ sudo reboot

Install Node.js v12:

  • $ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
  • $ sudo apt install nodejs

Check your version to make sure that the installation was successful. If it wasn't you can look at this blog post for more information: https://thisdavej.com/upgrading-to-more-recent-versions-of-node-js-on-the-raspberry-pi/

  • $ node -v
  • v12.13.0

Install Build Tools and additional components:

Next you need to install all the build tools and additional components needed in order to build tfjs-node from source as well as some image processing libraries. Run the following commands in sequence on your PI.

  • $ sudo npm install -g node-gyp
  • $ sudo npm install -g node-pre-gyp
  • $ sudo apt-get update
  • $ sudo apt-get install cmake
  • $ sudo apt-get install build-essential pkg-config
  • $ sudo apt-get install libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev
  • $ sudo reboot

Installing Tensorflow:

Now that you have all the necessary components to build tensorflow and run it, you can install it and the build should now complete.

  • $ npm install @tensorflow/tfjs-node

If everything went well you can now run tensorflow from node.js. If yous are still not running you are welcome to drop me a message and I will try to assist you. It can be a bit tricky as I don't think it was intended to run on a pi.

Face-Api.js

All the above installation was needed for me to be able to run face-api.js: https://github.com/justadudewhohacks/face-api.js on a Raspberry PI.

If you follow the face-api guide you can get it running on a pi, as there is clear instructions on what's needed to run it on a PI. I found that running it on node vs browser yealds better results. Inside the browser is more sluggish. So I opted for express node application that does the all the facial recognition work, and the browser is just used to connect to the webcam and grab the image.
One things though, install canvas seperately, for some reason when it's part of the npm install it doesn't build corectly.

  • $ npm install canvas

Hope this helps.

Comments

kent 2019/12/28 09:35:59

Thanks for the tutorial

My English is not very good
Excuse me for any offense

I have a project that requires a face-api.js to teach students.
so I need use @tensorflow/tfjs-node

I was follow this tutorial by step to step

but when I do $ npm install @tensorflow/tfjs-node

print:
pi@raspberrypi:~ $ npm install @tensorflow/tfjs-node

> @tensorflow/tfjs-node@1.5.1 install /home/pi/node_modules/@tensorflow/tfjs-node
> node scripts/install.js

CPU-linux-1.5.1.tar.gz
* Downloading libtensorflow
(node:938) UnhandledPromiseRejectionWarning: Error: Unsupported system: cpu-linux-arm
at getPlatformLibtensorflowUri (/home/pi/node_modules/@tensorflow/tfjs-node/scripts/install.js:95:11)
at downloadLibtensorflow (/home/pi/node_modules/@tensorflow/tfjs-node/scripts/install.js:129:7)
at async run (/home/pi/node_modules/@tensorflow/tfjs-node/scripts/install.js:190:5)
(node:938) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:938) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


who can I fix it ?

thanks

Mike 2020/01/03 22:35:17

Hello. I have some troubles with tf node binding. Here's the error log after rebuild:
CPU-linux-1.5.1.tar.gz
* Downloading libtensorflow
(node:30986) UnhandledPromiseRejectionWarning: Error: Unsupported system: cpu-linux-arm

ozitvogel 2020/01/08 13:15:20

Hi, thanks for your post. However is still get an issue in compiling from source as tries to download the arm version that is not available.
Any idea ?
CPU-linux-1.5.1.tar.gz
* Downloading libtensorflow
(node:16539) UnhandledPromiseRejectionWarning: Error: Unsupported system: cpu-linux-arm
at getPlatformLibtensorflowUri (/usr/local/lib/node_modules/homebridge-unifi-protect-camera-motion/node_modules/@tensorflow/tfjs-node/scripts/install.js:95:11)
at downloadLibtensorflow (/usr/local/lib/node_modules/homebridge-unifi-protect-camera-motion/node_modules/@tensorflow/tfjs-node/scripts/install.js:129:7)
at async run (/usr/local/lib/node_modules/homebridge-unifi-protect-camera-motion/node_modules/@tensorflow/tfjs-node/scripts/install.js:190:5)
(node:16539) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16539) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Dezi Van Vuuren 2020/01/13 15:59:51

Hi Guys

Sorry for the late reply, was a little bit on holiday over December.

Please try forcing it to the 1.2.11 version, I'm also having issues with the latest one. I still need to dig in deeper to see why.

Which raspberry are you using? 4 or 3 B+?

Then I have also noticed that if you use the tinyFaceDetector and faceLandmark68TinyNet, the speed on the raspberry improves dramatically.

Mike 2020/01/19 16:15:08

Hi Dezi. I was busy too.
I'm running 64-bit Ubuntu Mate on my Pi 3B.
I have switched the tfjs-node version to 1.2.11 and here's full log:
https://pastebin.com/T4bRx799

Dezi Van Vuuren 2020/01/20 07:02:34

Thanks for the log, I'll have to work through it. I haven't tried running it on Ubuntu mate before, only Rasbian. I get the feeling that some other dependency is needed.

What I can do is clone my Rasbian SD card to image for you, which is already working, that should get you going at least.

Post a comment

Categories