Notes describing how to build a Pi4 optimized OpenCV 4.2.0+ on Arch for Arm on a Raspberry Pi. Most all of the below can and may be scripted, there may be helper scripts or playbooks later on.
By the way I use Arch Arm Linux on my Raspberry Pi’s.
It is:
- Tight
- Arch
- Not Raspian
- Performant
- In line with all my other machine in the lab and house
- meme rich and zoomer approved
Be sure to use the correct image from Arch Linux Arm, specifically Raspberry Pi 4
Below are lifted from the armv8/broadcom page on the Arch Arm wiki
The Raspberry Pi 4 has higher power requirements than the Raspberry Pi 3. A power supply rated at 3A is the official recommendation. Using an insufficient power supply will result in random, inexplicable errors and file system corruption.
Early variants of the Raspberry Pi 4’s USB Type-C power port is not compliant with the USB Type-C specification, and it will not be able to be powered by supplies that are compliant.
Replace sdX in the following instructions with the device name for the SD card as it appears on your computer.
Use lsblk
to verify which labels belong to the target SD card:
❯ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 156.7M 1 loop /var/lib/snapd/snap/gnome-3-28-1804/110 loop1 7:1 0 44.9M 1 loop /var/lib/snapd/snap/gtk-common-themes/1440 loop2 7:2 0 54.7M 1 loop /var/lib/snapd/snap/core18/1650 loop3 7:3 0 97.4M 1 loop /var/lib/snapd/snap/signal-desktop/277 loop4 7:4 0 22.4M 1 loop /var/lib/snapd/snap/snapd/5754 sda 8:0 0 931.5G 0 disk ├─sda1 8:1 0 512M 0 part /efi ├─sda2 8:2 0 32G 0 part [SWAP] └─sda3 8:3 0 899G 0 part / sdb 8:16 0 2.7T 0 disk └─sdb1 8:17 0 2.7T 0 part sdc 8:32 1 29.7G 0 disk ├─sdc1 8:33 1 100M 0 part /run/media/thys/7CE9-4243 └─sdc2 8:34 1 29.6G 0 part /run/media/thys/f814e33d-4b31-4bf4-8dd8-dbff7fe0f570
In this case the SD card is sdc
- Use fdisk to partition the SD card:
fdisk /dev/sdX
- At the fdisk prompt, delete old partitions and create a new one:
- Type o. This will clear out any partitions on the drive.
- Type p to list partitions. There should be no partitions left.
- Type n, then p for primary, 1 for the first partition on the drive,
- press ENTER to accept the default first sector, then type +100M for the last sector.
- Type t, then c to set the first partition to type W95 FAT32 (LBA).
- Type n, then p for primary, 2 for the second partition on the drive, and then press ENTER twice to accept the default first and last sector.
- Write the partition table and exit by typing w.
- Create and mount the FAT file system:
mkfs.vfat /dev/sdX1 mkdir boot mount /dev/sdX1 boot
- Create and mount the ext4 file system:
mkfs.ext4 /dev/sdX2 mkdir root mount /dev/sdX2 root
- Download and extract the root file system (as root, not via sudo):
wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-4-latest.tar.gz bsdtar -xpf ArchLinuxARM-rpi-4-latest.tar.gz -C root sync
- Move boot files to the first partition:
mv root/boot/* boot
- Unmount the two partitions:
umount boot root
- Insert the SD card into the Raspberry Pi, connect ethernet, and apply 5V power.
- Use the serial console or SSH to the IP address given to the board by your router.
- Login as the default user alarm with the password alarm.
- The default root password is root.
- Initialize the pacman keyring and populate the Arch Linux ARM package signing keys:
pacman-key --init pacman-key --populate archlinuxarm
Install all the dependencies for the OpenCV build. I use yay
to install of
official and the aur. This is of course not a hard requirement, use any aur
helper to install aur hosted dependencies or just install it manually.
pacman -S base-devel git nvim wget unzip
Using yay
it does not matter if the dependencies come from core or aur.
git clone https://aur.archlinux.org/yay.git
and build it makepkg -sir
~yay -S pkg-config libjpeg libtiff libpng jasper ffmpeg libx264 cairo gdk-pixbuf pango gtk2 gtk-3lapack hdf5 ~
I like to keep virtualens in ~~/.virtualenvs~ :
python -m venv ~/.virtualenvs/cv source ~/.virtualenvs/cv/bin/activate pip install "picamera[array]"
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip unzip opencv.zip mv opencv-4.2.0 opencv wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip unzip opencv_contrib.zip mv opencv_contrib-4.2.0 opencv_contrib
Set some OpenCV build flags
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \ -D BUILD_TESTS=OFF \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D CMAKE_SHARED_LINKER_FLAGS=-latomic \ -D BUILD_EXAMPLES=OFF ..
chmod +x prepare.sh ./prepare.sh make -j4 sudo make install sudo ldconfig
cd /usr/local/lib/python3.8/site-packages/cv2/python-3.8/ sudo mv cv2.cpython-38-arm-linux-gnueabihf.so cv2.so cd ~/.virtualenvs/cv/lib/python3.8/site-packages/ ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.so
(cv) [thys@pateye-1 site-packages]$ python Python 3.8.1 (default, Jan 25 2020, 17:00:07) [GCC 9.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '4.2.0'
/boot/config.txt
should look like below
Display camera footage: /opt/vc/bin/raspivid --fullscreen --vflip --timeout 0