Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python

Overview

--Stay Home I popped a raspberry pi 4 and a High Quality Camera to play during the week, but the camera didn't meet the shipping deadline before the holidays and I received only the raspberry pi 4. ――I thought that I would probably use it when the camera came, so I installed OpenCV. I couldn't find the article about using raspberry pi 4 and Ubuntu 20.04 and OpenCV and python, so I will leave it.

OpenCV installation

I built it from source code.

$ wget https://github.com/opencv/opencv/archive/3.4.3.zip -O opencv-3.4.3.zip
$ wget https://github.com/opencv/opencv_contrib/archive/3.4.3.zip -O opencv_contrib-3.4.3.zip
$ unzip opencv-3.4.3.zip
$ unzip opencv_contrib-3.4.3.zip
$ cd opencv-3.4.3
$ mkdir build
$ cd build
$ ./cmake.sh
$ make -j4
$ sudo make install
$ sudo ldconfig
$ cd /path/to/site-packages
$ sudo ln -s /usr/local/lib/python3.6/site-packages/cv2.cpython-36m-aarch64-linux-gnu.so cv2.so

Since cmake has many options and I tried again many times, I saved it as a file as follows.

cmake.sh


#!/bin/sh
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF \
-D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF \
-D BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF \
-D BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF \
-D BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF \
-D BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF \
-D BUILD_DOCS=OFF -D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF \
-D BUILD_opencv_python3=ON -D FORCE_VTK=ON \
-D WITH_TBB=ON -D WITH_V4L=ON \
-D WITH_OPENGL=ON -D WITH_CUBLAS=ON \
-D BUILD_opencv_python3=ON \
-D PYTHON3_EXECUTABLE=`pyenv local 3.6.8; pyenv which python` \
-D PYTHON3_INCLUDE_DIR=`pyenv local 3.6.8; python -c 'from distutils.sysconfig import get_python_inc; print(get_python_inc())'` \
-D PYTHON3_NUMPY_INCLUDE_DIRS=`pyenv local 3.6.8; python -c 'import numpy; print(numpy.get_include())'` \
-D PYTHON3_LIBRARIES=`find $PYENV_ROOT/versions/3.6.8/lib -name 'libpython*.so'` \
-D WITH_FFMPEG=ON \
..

I'm new to OpenCV, and I didn't go smoothly because of the special environment. I stumbled on the following points.

--There is no libjasper-dev I got an error and was worried about what to do. However, I skipped it because it was written on an English bulletin board somewhere that it wasn't necessary and I didn't have to overdo it. It seems to be a JPEG related library. The build passed even if I didn't put it on the bulletin board. --After building with cmake, I was worried that I couldn't do ʻimport cv2. I had to get it to be a Makefile for python3 with the -D PYTHON3_hogehoge` option. I didn't see the result of cmake properly. ..

matplotlib I put matplotlib for image display. I got an error that the backend is non-GUI, so I put PyQt5 as follows. (I did it through trial and error, so I feel that there are not enough commands.)

$ sudo apt install -y qt5-qmake qt5-default
$ wget https://sourceforge.net/projects/pyqt/files/sip/sip-4.19.12/sip-4.19.12.tar.gz
$ wget https://sourceforge.net/projects/pyqt/files/PyQt5/PyQt-5.10.1/PyQt5_gpl-5.10.1.tar.gz
$ tar -xvf sip-4.19.12.tar.gz
$ tar -xvf PyQt5_gpl-5.10.1.tar.gz
$ cd sip-4.19.12/
$ python configure.py --sip-module=PyQt5.sip
$ make -j4
$ sudo make install
$ cd ../PyQt5_gpl-5.10.1/
$ python configure.py --qmake /usr/bin/qmake --sip-incdir ~/deeplabcut/sip-4.19.12/siplib
$ make
$ sudo make install
$ pip install pyqt5

Confirmed operation with python

Can Lena's face be detected? I checked the operation of OpenCV based on the theme.

cvtest.py


import cv2
import matplotlib.pyplot as plt

cascade_path = '/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml'

def face_detection(img_src):
    img_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
    cascade = cv2.CascadeClassifier(cascade_path)
    facerect = cascade.detectMultiScale(img_gray)
    if len(facerect) == 0:
        print('No face detected')
        return None

    rect = facerect[0]
    cv2.rectangle(
        img_src,
        tuple(rect[0:2]), tuple(rect[0:2] + rect[2:4]),
        (255, 255, 255),
        thickness=2)
    return img_src

if __name__ == "__main__":
    lena_path = './lena.png'
    lena_src = cv2.imread(lena_path)
    lena_face_detected = face_detection(lena_src)
    if lena_face_detected is None:
        img_show = lena_src
    else:
        img_show = cv2.cvtColor(lena_face_detected, cv2.COLOR_BGR2RGB)
    plt.imshow(img_show)
    plt.show()

The following is the execution result. You can detect it safely. Wow

cvtest.png

When the camera arrives, I would like to play with it.

that's all.

Recommended Posts

Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Use vl53l0x with Raspberry Pi (python)
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Working with GPS on Raspberry Pi 3 Python
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Play with your Ubuntu desktop on your Raspberry Pi 4
Connect to MySQL with Python on Raspberry Pi
Don't work Python with OpenCV on AMD Ryzen CPU on WSL2 Ubuntu 18.04 And 20.04
Use matplotlib on Ubuntu 12 & Python
Install OpenCV on Ubuntu + python
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Record temperature and humidity with systemd on Raspberry Pi
Try debugging Python on Raspberry Pi with Visual Studio.
Detect mask wearing status with OpenCV and Raspberry Pi
Install pyenv on Raspberry Pi and version control Python
Display USB camera video with Python OpenCV with Raspberry Pi
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Shining life with Python and OpenCV
Neural network with OpenCV 3 and Python 3
MQTT on Raspberry Pi and Mac
OpenCV installation procedure on Raspberry Pi
Use OpenCV with Python 3 in Window
OpenCV3 & Python3 environment construction on Ubuntu
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Create a web surveillance camera with Raspberry Pi and OpenCV
Python beginner opens and closes interlocking camera with Raspberry Pi
Create an LCD (16x2) game with Raspberry Pi and Python
Pet monitoring with Rekognition and Raspberry pi
Control the motor with a motor driver using python on Raspberry Pi 3!
Detect "brightness" using python on Raspberry Pi 3!
Python virtual environment and packages on Ubuntu
Adafruit Python BluefruitLE works on Raspberry Pi.
A memo with Python2.7 and Python3 on CentOS
Use PIL and Pillow with Cygwin Python
Set swap space on Ubuntu on Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
Use the Grove sensor on the Raspberry Pi
Created Ubuntu, Python, OpenCV environment on Docker
Working with sensors on Mathematica on Raspberry Pi
Use PIR motion sensor with raspberry Pi
Detect temperature using python on Raspberry Pi 3!
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Until you can use opencv with python
Use Python and MeCab with Azure Functions
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Try tweeting arXiv's RSS feed on twitter from Raspberry Pi with python
Discord bot with python raspberry pi zero with [Notes]
Build python environment with pyenv on EC2 (ubuntu)
Detect slide switches using python on Raspberry Pi 3!
How to use Raspberry Pi pie camera Python
Detect magnet switches using python on Raspberry Pi 3!
Enjoy electronic work with GPIO on Raspberry Pi
MQTT RC car with Arduino and Raspberry Pi
Power on / off your PC with raspberry pi
The easiest way to use OpenCV with python
Use Majoca Iris elongated LCD with Raspberry Pi
Use Grove-Temperature & Humidity Sensor (DHT11) on Raspberry Pi
Get CPU information of Raspberry Pi with Python
Make DHT11 available on Raspberry Pi + python (memo)