https://github.com/sthysel/arm-arch-opencv
Build optimized OpenCV on Arch for arm on a Raspberry Pi
https://github.com/sthysel/arm-arch-opencv
arch arm opencv
Last synced: about 2 months ago
JSON representation
Build optimized OpenCV on Arch for arm on a Raspberry Pi
- Host: GitHub
- URL: https://github.com/sthysel/arm-arch-opencv
- Owner: sthysel
- Created: 2020-03-04T14:51:27.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-05T06:03:32.000Z (over 6 years ago)
- Last Synced: 2025-03-11T04:59:45.808Z (over 1 year ago)
- Topics: arch, arm, opencv
- Language: Shell
- Size: 71.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.org
Awesome Lists containing this project
README
#+title: OpenCV 4.2.0 On Arch Arm for the Raspberry Pi 4
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.
* Installing Arch on the Pi4
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 [[https://archlinuxarm.org][Arch Linux Arm]], specifically [[https://archlinuxarm.org/platforms/armv8/broadcom/raspberry-pi-4][Raspberry Pi 4]]
#+caption: Raspberry Pi 4
[[file:pics/image.png]]
** Install Arch on the Pi 4
Below are lifted from the [[https://archlinuxarm.org/platforms/armv8/broadcom/raspberry-pi-4][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.
*** Prep SD card and install Arch
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:
#+BEGIN_SRC
❯ 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
#+END_SRC
In this case the SD card is ~sdc~
1. Use fdisk to partition the SD card:
~fdisk /dev/sdX~
2. 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.
3. Create and mount the FAT file system:
#+BEGIN_SRC
mkfs.vfat /dev/sdX1
mkdir boot
mount /dev/sdX1 boot
#+END_SRC
4. Create and mount the ext4 file system:
#+BEGIN_SRC
mkfs.ext4 /dev/sdX2
mkdir root
mount /dev/sdX2 root
#+END_SRC
5. Download and extract the root file system (as root, not via sudo):
#+BEGIN_SRC
wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-4-latest.tar.gz
bsdtar -xpf ArchLinuxARM-rpi-4-latest.tar.gz -C root
sync
#+END_SRC
6. Move boot files to the first partition:
~mv root/boot/* boot~
7. Unmount the two partitions:
~umount boot root~
8. Insert the SD card into the Raspberry Pi, connect ethernet, and apply 5V power.
9. 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.
10. Initialize the pacman keyring and populate the Arch Linux ARM package signing keys:
#+BEGIN_SRC
pacman-key --init
pacman-key --populate archlinuxarm
#+END_SRC
* Prepare build environment
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.
** Install core system dependencies
~pacman -S base-devel git nvim wget unzip~
** Install yay
Using ~yay~ it does not matter if the dependencies come from core or aur.
*** clone yay from the aur
~git clone https://aur.archlinux.org/yay.git~ and build it ~makepkg -sir~
*** use yay to install the OpenCV dependencies
~yay -S pkg-config libjpeg libtiff libpng jasper ffmpeg libx264 cairo gdk-pixbuf pango gtk2 gtk-3lapack hdf5 ~
* Prepare Python virtualenv
I like to keep virtualens in ~~/.virtualenvs~ :
#+BEGIN_SRC
python -m venv ~/.virtualenvs/cv
source ~/.virtualenvs/cv/bin/activate
pip install "picamera[array]"
#+END_SRC
* Build OpenCV
** Get the source
#+BEGIN_SRC
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
#+END_SRC
** prepare the build
Set some OpenCV build flags
#+BEGIN_SRC
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 ..
#+END_SRC
#+BEGIN_SRC
chmod +x prepare.sh
./prepare.sh
make -j4
sudo make install
sudo ldconfig
#+END_SRC
#+BEGIN_SRC
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
#+END_SRC
* Verify
#+BEGIN_SRC
(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'
#+END_SRC
* Camera setup
** Enable camera
~/boot/config.txt~ should look like below
#+BEGIN_VERBATIM
initramfs initramfs-linux.img followkernel
gpu_mem=128 start_file=start_x.elf fixup_file=fixup_x.dat
start_x=1
#+END_VERBATIM
Display camera footage: ~/opt/vc/bin/raspivid --fullscreen --vflip --timeout 0~