Raspberry Pi Zero W with camera

Raspberry Pi Zero – updating the gate opener with water meter OCR

Introduction

My Raspberry Pi 1B+ has been opening garadge and letting me know of the conditions for work nearly 4 years. Recently I’ve decided to replace this 2014 small board computer (SBC) with newer version. I’ve also looked at better (the principle Keep it Small and Simple – KISS) solutions that became available – i.e. ESP Easy on ESP8266 (controlling the relay and reading the contact switch is not too much for this platform, even with addition of environmental sensors), but after some thinking – the option of leaving the Raspberry Pi in place won because of one additional perk that I wanted to handle – a classic water meter. Yes, such an old analogue water meter. Fun!

Raspberry Pi Zero Front
Raspberry Pi Zero Front

Raspberry Pi Zero

Since 2014, the number of Raspberry Pi versions has grown quite significantly – which one should I choose? I decided on the underrated Raspberry Pi Zero, although the question of choosing between Zero and Zero W remained. So let’s see:

Raspberry Pi Zero :

  • CPU: Broadcom BCM2835, identical to the Raspberry Pi B+
  • Clock: 1GHz, previously 700MHz
  • RAM: 512MB
  • Mass storage: microSD, formerly SD
  • USB: 1 microUSB port, previously 4 USB-A ports
  • HDMI: miniHDMI port (special cable or adapter may be required)
  • Camera connector, slightly narrower, requires a different cable type than before.
  • GPIO – standard pins are not soldered to the board
Raspberru Pi Zero W with camera
Raspberry Pi Zero W with camera

Raspberry Pi Zero W :

  • additionally WiFi and Bluetooth

Let’s prepare the upgrage!

We will need:

Hardware:

  • Rasbpberry Pi Zero (or other version of Raspberry Pi)
  • Camera for Raspberry Pi with the appropriate cable
  • OPTION: HDMI extension cable for Raspberry Pi camera – if we can’t place Raspberry over the counter

Software:

  • ImageMagick – image manipulation program that will allow us to manipulate the recorded counter photo by the camera so that recognition is easier and more reliable
  • Tesseract OCR – text recognition program: https://github.com/tesseract-ocr/tesseract

The final chocise was the Raspberry Pi Zero, and any version (also the W) works with the current version of Raspbian – Stretch today and (writting this in early 2020) Buster. As in previous entry first you need to install the operating system using a microSD card. Please do remember that after installation one needs to an empty file called “ssh” (without extension, only the word ssh) in the root directory of the first partition on the freshly installed card (the partition is called boot) – so that when you start Raspberry Pi Zero – the SSH daemon (sshd) is also launched – for remote access. The actual need for the “ssh” file presence is the safety measure against running the Raspberry Pi with the default password “rasbperry”. First thing that you do after starting should be changing the default password, and turning on ssh permanently!

So finally, I’m using Raspberry Pi Zero here (of course you can also W, if you do not have access to the ethernet cable in the garage), so in the case of the Zero version a USB hub with Ethernet (and a cute copper radiator!):

Raspberruy Pi Zero with Hub/Ethernet
Raspberry Pi Zero with Hub/Ethernet

If we do not have an adapter or cable, we can solder pins to the CVSB output (AV on the TV) – the hot signal is marked with an arrow on the left:

RPi Zero - adding AV/CVSB output
RPi Zero – adding AV/CVSB output

Another issue is soldering pins for GPIO – we need to connect the Raspberry Pi Zero with sensors and reed switches – so the header is a “must have”. Soldering is a process that requires quite a lot of practice – from time to time it is worth training and checking with how many pins we are satisfied …

Raspberruy Pi Zero Header
Raspberry Pi Zero Header

Personally – here I’m happy with 75% 😉

Raspberruy Pi Zero Solder Side
Raspberry Pi Zero Solder Side

OK, now it it’s time to move everything from the previous entry about opening the garage, and add a new item – Raspberry Pi camera – for Raspberry Pi Zero. Then – connect the power supply (just as before – power via GPIO pins)

In my case – I was unable to phisically move my Raspberry Pi Zero – so I used a camera extension cable – using solutions using the HDMI cable . Let’s check out how the setup looks, and this time I changed the heat sink to the aluminum version. As before, we have a gate opening relay, a closing sensor (reed switch) and, additionally – 12V LED light that switches on the second of the relays. The only change from the previous entry is the replacement for Raspberry Pi Zero from older version:

New Raspberry Pi Zero in Garadge
New Raspberry Pi Zero in Garadge

The camera using an extension cord looks like this:

Begining of HDMI RPi camera extension
Begining of HDMI RPi camera extension

Whereas its other end with the camera is positioned above the meter:

End of HDMI RPi camera extension
End of HDMI RPi camera extension

Counter reading recognition

To recognize how much water we have used, you need to take a picture and recognize the numbers – replace them with a form that you can use in the programs – Domoticz, Home Assistant etc.

First of all – turn on the camera:

Selecting camera in raspi-config
Selecting camera in raspi-config

Turning on camera in raspi-config
Turning on camera in raspi-config
Turning on camera in raspi-config
Turning on camera in raspi-config

Now let’s restart Raspberry and proceed to the software installation:

Let’s take a picture:

We will receive something similar to:

Now you need to cut and then convert the image to black and white (monochrome). This will allow a better recognition. We will use Image Magick for this, like this but do not enter the whole command yet:

The steps are as follows in the pictures, -crop is the area where the meter is and the 54% parameter should be chosen so that the numbers are clear as in the picture below:

Raw meter picture, cropped
Raw meter picture, cropped (water-meter-raw.jpg)
Black and white picture for recogntion, not mask yet
Black and white picture for recogntion, not mask yet (intermediate picture)

We can now specify exactly where the numbers appear and perform the so-called a mask. Subtracting this mask from the image – we will remove the unwanted artifacts around digits. How? Exactly the opposite – erase only the digits – leaving only the undesirable artifacts in the next image:

Mask for the meter
Mask for the meter (created maska-pic-cropped-bw.jpg)

Now we can run the full command:

and get

Water meter after mask and inversion
Water meter after mask and inversion

Let’s fire up Tesseract OCR:

To help it even more let’s run the tesseract with only the possible characters (digits): 0123456789, and if everything was processed correctly, we should receive:

00522

That’s all!