Install OpenCV 3.2.0 on Raspberry Pi Zero W in 15 Minutes

OpenCV, or Open Source Computer Vision Library, is an open source computer vision and machine learning software. It works on Raspberry Pi computers, and can process photos captured by the Raspberry Pi Camera Module.

OpenCV has two supported versions: 2.4.x and 3.x. New features are being added to 3.x branch, while 2.4.x only receives bug fixes and efficiency fixes. It is recommended that new developments should use OpenCV 3.x, to take advantage of new features. However, the official operating system for the Raspberry Pi, Raspbian Stretch, comes with OpenCV 2.4.9. While I am not yet familiar with OpenCV algorithms, one thing notably missing from OpenCV 2.4.9 is a Python 3 binding.

I wanted to have OpenCV 3 running in Raspbian Stretch on a Raspberry Pi Zero W. Unable to find existing packages for Pi Zero and Stretch, I had no choice but to compile my own OpenCV 3. I decided to do it the proper way: build a backported Debian package. This method is superior to installing from the source code, because the packages can easily be deployed to other Raspberry Pi Zero W computers. I followed the Simple Backport Creation instruction, and spent a week building the packages. Now I'm sharing my compiled packages, so that you can use them if you dare.

What You Need

The Quest of Building OpenCV 3.2.0 Debian Packages on Raspberry Pi Zero W

The newest members in my toy collection are a Raspberry Pi Zero W and a NoIR Camera Module, purchased in Dec 2017. Recently, I witnessed an impressive Contour Detection demo at MoCoMakers meetup. I read their source code, and it has a dependency on cv2 Python package. Therefore, the first step to get it working on my RPi Zero would be installing OpenCV that provides cv2 package.

While Raspbian Stretch offers a python-opencv package, it is version 2.4.9 released in 2014, and it only works with Python 2 but not Python 3. Since I'm starting from scratch, I wanted to develop on newer platforms: OpenCV 3.x and Python 3.

Many online tutorials suggest compiling OpenCV from source code. There are also a few sites offering pre-compiled tarballs, but these are either compiled for the Raspberry Pi 3, or built for Raspbian Jessie; neither would be compatible with my Raspberry Pi Zero W running Raspbian Stretch. Therefore, I started my quest to build OpenCV 3 for Pi Zero.

Debian Package > Source Code

When I first learned Linux, the standard process of installing software is wget, tar xzvf, ./configure, make, make install. Today, this is no longer recommended because software installed this way is difficult to remove and could cause conflicts. Instead, it is recommended to install everything from Debian packages.

PO Box 3943

PO Box, or post-office box, is a uniquely addressable lockable box located on the premises of a post office station. There is a wall of PO Boxes in the University of Arizona's Student Union Memorial Center, where it costs $25 to rent one for a semester. I found them interesting, although I didn't use one because I could receive mail in my apartment, and the Tucson heat did a good job to keep the grounds dry so I didn't have to worry about packages.

When I first came to Maryland, I rented a room in Orchard Place neighborhood. It rains often, and the house does not have a porch. Seeing the post office right outside the neighborhood, I thought it would be the perfect chance to try a PO Box.

Opening My PO Box

Rental fee for a small PO Box at Diamond Farms post office is $54 for six months, or $30 for three month. I would live at Orchard Place for two or three months, so the 3-month option seems more reasonable. However, it is cheaper to choose the 6-month option, because the post office offers a ½ refund if I close the PO Box within three months, so the final price is only $27.

PO Boxes at this location include "premium service": every PO Box comes with a street address, and can accept deliveries from non-postal carriers. This is an important factor for me, because most items I buy online are not shipped via USPS, and having a street address allows me to receive them at the PO Box.

NFD on Windows 10 WSL

The NDN Forwarding Daemon (NFD) connects every Ubuntu and Mac OS machine to the Named Data Networking (NDN) testbed network. While it's awesome to get your NFD connected from a Linux server or a Macbook, 82.56% of the desktop users running Windows are out of luck. Compiling NFD for Windows is possible, but the amount of patches needed is astonishing.

Then the good news came: Microsoft announced Windows Subsystem for Linux (WSL), which lets developers run Linux environments directly on Windows, unmodified, without the overhead of a virtual machine. Ubuntu is the first Linux distribution supported by WSL. This means, we can now run NFD natively on Windows!

How to Install NFD on WSL

This section outlines how to install NDN Forwarding Daemon (NFD) on Windows Subsystem for Linux (WSL). As of this writing, I have Windows 10 version 1709 (Fall Creators Update), and the latest NFD release is version 0.6.1.

The steps to install NFD on Windows are:

How to Flash C.H.I.P Offline

Next Thing Co (NTC), the company that made the $9 C.H.I.P computer, is not doing well: orders are not shipping on time, and support is not answering emails. While the little CHIPs would still work even if NTC no longer exists, same cannot be said for firmware flashing: the recommended method for flashing firmware is through Chrome App developed by NTC, which in turn downloads from NTC's file servers. If Next Thing Co evaporates and their file servers go offline, the Chrome App would not be able to download the firmware, and therefore I cannot re-flash my CHIPs if I ever mess up their operating system.

To keep my C.H.I.Ps working for as long as possible, I have to plan for the inevitable and find out how to flash a CHIP without relying on the Internet, or at least, without relying on the file server. NTC has published some scripts, known as CHIP-tools, to work with CHIP computers. The script is a command line tool to flash the device. While the content of this script is complicated, to make it work offline, I just need to figure out what it needs from NTC file servers, and download these files in advance.

A brief read and tests indicate that has the following workflow:

  1. Verify all necessary programs are available.
  2. Select a flavor according to command line arguments. It is one of server, gui, pocketchip, and buildroot.
  3. Download a latest file that contains the latest firmware version number.
  4. Download SPL, sunxi, and U-Boot images.
  5. Connect to CHIP in FEL mode, and determine whether the CHIP has a Hynix or Toshiba NAND chip.
  6. Download CHIP operating system image that suitable for the NAND chip.
  7. Connect to CHIP in Fastboot mode, and send the operating system image to CHIP.

Files for each flavor is hosted in a different directory on the file server. For server flavor, the files are:

Orchard Place (2)

I moved into Mr Argoti's house on the afternoon of Oct 29, 2017, and became a resident of Orchard Place neighborhood.

The Tiny Bedroom

My assigned bedroom is incredibly small. There are five pieces of furniture: a BED that later turns out to be a sofa, a four-drawer DRESSER, a big glass DESK, a nice office CHAIR, and a small wooden end TABLE. My room also includes a small FRIDGE and a MICROWave. The following diagram shows their placement.

Orchard Place (1)

I moved to Gaithersburg Maryland to start a research job at National Institute of Standards and Technology (NIST). As I exited Washington Union Station on Oct 26, 2017 after enjoying an epic 4-day Amtrak train journey, I felt lost immediately: this is no longer Tucson, the toasty city that I know and love, but a completely different place. Nevertheless, Cortana's voice guided me along Interstate 270, and my rental car arrived at Extended Stay America Gaithersburg South, where I was greeted by James and received a spacious and comfortable room.

Before I first came to Tucson, I signed up for a student housing apartment online. Unlike six years ago, I did not select an apartment before traveling to Maryland. Gaithersburg is not a college town, and I am no longer a student, so that I couldn't get a fairly straightforward "student housing" option. Instead, I am facing a much larger and complex apartment rental market. I want to see the apartments myself before committing to sign a lease.

Oct 26 is a Thursday, so there was only one weekday remaining before the weekend when most leasing offices would be closed. I browsed online apartment listings, and selected four apartment complexes to visit on Friday. I still wanted to avoid owning a vehicle, so location was the most important factor in my consideration. I also preferred a furnished apartment, to avoid the hassle of buying and selling furniture.

Spring Ridge apartments model room

Enter WiFi Credentials on ESP32 with One Button

It is considered bad practice to hard-code WiFi credentials into Arduino sketches. Typical recommendations include the WiFiManager library for ESP8266, and SmartConfig for ESP32. Both require the user to have a smartphone to send command to the ESP chip, which otherwise does not have any User Interface (UI).

My ESP32 board is the Heltec WiFi_Kit_32 that comes with a 0.96 inch OLED display and a little PRG button connected to pin #0. Can I use these minimal UI to enter WiFi credentials without involving a smartphone?

Heltec WiFi_Kit_32 OLED board with one button

Design with Minimal UI

I took inspiration from Prof Stephen Hawking's computer:

Happy New Year 2018 on ESP32 OLED

I've been playing with ESP8266 for one and a half years now. Recently, I'm upgrading to its bigger brother, the ESP32. I purchased two development boards, one of them is the Heltec WiFi_Kit_32 that comes with a 0.96 inch OLED display.

When 2018 arrives, what's a better way to say Happy New Year than on the ESP32? Therefore, I crawled out of the bed at 4AM, turned on the computer, and started Arduino IDE. Half an hour later, I tweeted this picture:

"Happy New Year 2018" on Heltec WiFi\_Kit\_32 OLED display

The code is simple yet effective. It was modified from u8g2 library full_buffer/HelloWorld example, with the constructor line found on Robot Zero One.