Skip to content

15. Wildcard week

Time flies… and we are almost at the end of our great adventure. The Wildcard week is the last ‘technical’ week so… for that reason we could chose our own topic for this week’s assignment. Neil showed a variety of topics, including:

  • Machines that we haven’t used yet.
  • Food printing.
  • Robotics.
  • Machine learning.
  • Machine vision.
  • Composites.

Composites is the classic topic for this week, but I didn’t feel like working on that… Especially since all the problems I had during the Molding and Casting week. I didn’t want to repeat the experience so I continued looking for other topic. I didn’t have access to the other machines shown during the global lecture so they weren’t an option for me.

Finally, after checking the equipment needed for computer vision I decided to go with that topic. The Raspberry Pi 3 (RPi3) was listed between the devices commonly used for computer vision… I don’t have one, but I have a Raspberry Pi Zero W (RPiZW) gathering dust in my drawer. It is a bit less powerful… But I decided to give it a try.

Hardware

Boards comparison

Below are the differences between the RPi3 and the RPiZW boards.

Raspberry Pi 3 vs Raspberry Pi Zero W

There’s a big difference in the processors inside them. The RPi3 has a 64bit Quad-Core ARM processor, while the RPiZW has a 32bit Single-Core Processor. Let’s see the implications of this in my assignment.

Camera modules

Although both boards support the camera interface, an adapter is needed for the RPiZW since it includes a small form-factor CSI port. The most popular cameras for these boards are Pi V2 NoIR and the Pi V2 Daylight modules. The first one doesn’t have an IR filter so it is a good option for low light environments. The Pi V2 Daylight has the IR Filter so it is commonly used for outdoor photography. The Pi V2 NoIR was the only module I found at my local store, so I bought it.

Installing the Pi V2 NoIR module

It is worth mentioning that the adapter for the camera cable is not included in the module package and it is sold separately. I followed the steps shown on this video to install the camera module. This docs are also useful for getting started with Picamera.

Software

OS upgrade

I bought my RPiZW board in 2018 and I had never updated the OS since then. So, before installing computer vision software I decided to update the OS. I followed this tutorial to update the system:

Basically, these are the steps I followed:

  • Download and install the Raspberry Pi Imager.
  • Insert a microSD card in your computer, open Raspberry Pi Imager, select an OS and transfer it to the card.
  • Copy the ssh and wpa_supplicant.conf files to the card and modify the latter to config the Wi-Fi connection.
  • Insert the microSD into the RPi and connect the board to a power supply.
  • Use the ssh command in your terminal or the VNC Viewer software to start controlling the RPi board.

Updating the OS

After installing the new OS, I noticed that the board stops sending signals through the mini HDMI port to the monitor when the OS starts. I tried adjusting all the settings related to screens, but I didn’t manage to fix that yet. I can control the board using ssh connections and the VNC Viewer anyways.

Controlling the camera

In case you want to try the camera using the raspistill command you need to enable the Legacy Camera Support running the raspi-config command.

Enabling legacy camera support

Below is the first photo I’ve taken using the Pi V2 NoIR module, the colors look different because of the daylight.

First photo

OpenCV

OpenCV is one of the most popular tools for image processing and computer vision tasks. In this section I’ll describe the steps I followed to install it into the RPiZW board.

First Attempt

I read this article about installing a striped version of OpenCV in boards with limited amounts of memory. It is very complete and contains the steps for installing the library in many models of Raspberry boards, including the RPiZW… which is the one with more limitations.

Comparison between different boards

In my case, I had to:

  • Increase the swap partition size, because the RpiZW has only 512Mb of RAM and 1GB of memory is needed for building OpenCV.
  • Install dependencies
  • Clone the OpenCV project and build it.

Missing package

It took around 4 hours to build OpenCV from source, but sadly not all the packages I needed for my assignment were installed.

Second Attempt

Then I came across an article named “An Easy(ish) OpenCV setup for Raspberry PI Zero W”. I removed the python packages I installed during the first attempt and then executed the commands shown in the guide. The only difference is that I installed opencv-python version 4.5.3.56 instead of 3.4.6.27.

Luckily, the CascadeClassifier package was installed this time. So I could run the examples shown in the OpenCV repository. The screenshot below shows the results of running the face detection example in the RPiZW board (left) and in my laptop (right).

Executing OpenCV examples successfully

Performance

I ran some examples in the OpenCV in both my computer and the RPiZW board to compare the performance. The RpiZW board is much slower at processing… the following video shows the difference in performance when running a face detection algorithm:

Testing the face tracker
Running the mosse example in my computer

Face Tracker

After trying all the python examples in the OpenCV repo I decided to reuse the techniques shown in facedetect.py and mosse.py to build a face tracking system, using a RPiZW board connected to a servo and to a camera.

I wasn't in the lab so I had to improvise

The idea is to execute the facedetect process to detect faces using the camera. When a face is detected, the mosse process is used to track the face movements. When the face is near the limits of the image, the servo will be activated to move the camera pointing to the tracked face.

I increased the speed of the videos shown below because of the performance issues I shown in the last section.

Testing the face tracker
Testing the face tracker with a little help from my wife

Future tests

I plan to modify this code a bit to use it directly on TAZ (the project we build during the machine building week). The idea is to:

  1. Use my smartphone as a webcam and connect it wirelessly to my laptop using Droidcam.
  2. Put the smartphone in the TAZ’s camera support.
  3. Run the face tracker code in the laptop and sand gcode instructions to TAZ when the subject moves.

Taz

I’ll try to test this the next time I go to the fablab and I’ll update this section with the results.

Files


Last update: May 18, 2022