Help us understand the problem. What is going on with this article?

Raspbian Busterにて最新のOpenCVインストール

概要

最新のOpenCV(v4.1.2)をRaspbian Busterでソースコンパイルしてインストールした内容のメモです。

環境確認

LinuxバージョンとPythonバージョン確認

$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:    10
Codename:   buster

$ python3 --version
Python 3.7.3

必要パッケージインストール

# パッケージアップデート
$ sudo apt-get update && sudo apt-get upgrade

# ビルドツールインストール
$ sudo apt install -y build-essential cmake pkg-config

# イメージ関連パッケージインストール
$ sudo apt install -y libjpeg-dev libtiff5-dev libjasper-dev libpng-dev

# ビデオ関連パッケージインストール
$ sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev

# OpenCV関連パッケージインストール
$ sudo apt install -y libfontconfig1-dev libcairo2-dev libgdk-pixbuf2.0-dev libpango1.0-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran

# Python関連パッケージインストール
$ sudo apt install -y python3-dev

Swapサイズ変更

$ sudo vi /etc/dphys-swapfile
...
CONF_SWAPSIZE=100 --> 2048
...

$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start

OpenCVインストール

約2~3時間かかるので、tmuxを使いましょう。

ソースダウンロード

$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.2.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.2.zip
$ unzip opencv.zip
$ unzip opencv_contrib.zip
$ mv opencv-4.1.2 opencv
$ mv opencv_contrib-4.1.2 opencv_contrib

ソースコンパイル

$ cd ~/opencv
$ mkdir -p build && cd build
$ 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 ..

# 2~3時間所要
$ make -j4

$ sudo make install
$ sudo ldconfig

OpenCV確認

Swapサイズ変更

$ sudo vi /etc/dphys-swapfile
...
CONF_SWAPSIZE=2048 --> 100
...

$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start

OpenCVリンク修正

$ cd /usr/local/lib/python3.7/dist-packages/cv2/python-3.7
$ sudo mv cv2.cpython-37m-arm-linux-gnueabihf.so cv2.so
$ cd ~/.local/lib/python3.7/site-packages/
$ ln -s /usr/local/lib/python3.7/dist-packages/cv2/python-3.7/cv2.so cv2.so

バージョン確認

$ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.2'
>>>
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした