Giter Site home page Giter Site logo

arm-arch-opencv's Introduction

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 Arch Linux Arm, specifically Raspberry Pi 4

pics/image.png

Install Arch on the 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.

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:

❯ 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

  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:
    mkfs.vfat /dev/sdX1
    mkdir boot
    mount /dev/sdX1 boot
        
  4. Create and mount the ext4 file system:
    mkfs.ext4 /dev/sdX2
    mkdir root
    mount /dev/sdX2 root
        
  5. 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
        
  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:
    pacman-key --init
    pacman-key --populate archlinuxarm
        

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~ :

python -m venv ~/.virtualenvs/cv
source ~/.virtualenvs/cv/bin/activate
pip install "picamera[array]"

Build OpenCV

Get the source

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

prepare the build

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

Verify

(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'

Camera setup

Enable camera

/boot/config.txt should look like below

Display camera footage: /opt/vc/bin/raspivid --fullscreen --vflip --timeout 0

arm-arch-opencv's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.