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.

Post a comment

Categories