--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.
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
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
When the camera arrives, I would like to play with it.
that's all.
Recommended Posts