앎/raspberry pi

[Raspberry Pi] OpenCV 설치 Case2

후니아부지 2018. 3. 10. 18:02

2018.03.10 00:28:24


http://webnautes.tistory.com/916

https://www.pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/

http://kkokkal.tistory.com/1304

https://raspberrypi.stackexchange.com/questions/69169/how-to-install-opencv-on-raspberry-pi-3-in-raspbian-jessie


https://qiita.com/nanbuwks/items/422eb405ceef84826ab4  <-- 이거 기준으로 확인중




raspbian-stretch 에 설치




$ sudo apt-get update

$ sudo apt-get upgrade




$ sudo apt-get install build-essential git cmake pkg-config 


정지 영상 파일 입출력

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev


비디오 파일 입출력/스트리밍

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev

$ sudo apt-get install libv4l-dev v4l-utils

$ sudo apt-get install libxvidcore-dev libx264-dev libxine2-dev

$ sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev


OpenCV의 highgui 모듈에서 GTK를 사용하도록

$ sudo apt-get install libgtk2.0-dev


Opencv 최적화를 위한 라이브러리를 설치한다.

$ sudo apt-get install libatlas-base-dev gfortran libeigen3-dev


python3 설치

stretch에는 이미 최신 버전이 들어가 있다. (lite에는 없다)

$ sudo apt-get install python3-dev python3-pip


Opencv에서 매트릭스 연산을 위해 Numpy를 설치한다.

역시 최신으로 들어가 있다. (역시나 lite에는 없다)

$ sudo apt-get install python-numpy python3-numpy

$ sudo pip3 install numpy


opencv, opencv_contrib 다운로드

현재 최신 버전은 3.4.0


$ cd ~

$ git clone https://github.com/Itseez/opencv.git

$ cd opencv

$ git checkout 3.4.0

$ cd ..

$ git clone https://github.com/Itseez/opencv_contrib.git

$ cd opencv_contrib

$ git checkout 3.4.0

$ cd ..


빌드를 위해 디렉토리를 만든다.

$ cd opencv

$ mkdir build

$ cd build


cmake 구성


[stretch]

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \

        -D CMAKE_INSTALL_PREFIX=/usr/local \

        -D INSTALL_PYTHON_EXAMPLES=ON \

        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \

        -D BUILD_EXAMPLES=ON ../



[stretch_lite]

// lite는 뭔가 많이 빠져 있어서 그런지 cmake에서 에러가 난다...

// 위에 필요 라이브러리를 모두 설치하고 (빼먹으면 안된다!!!!)

//

// cmake 구성에서 -D BUILD_opencv_python2=OFF 하지 않을거면

// pyhton 2.7도 설치해야한다!!!! 

// $ sudo apt-get install python2.7-dev python-dev python-numpy

//

// full 컴파일 완료되었던 build(무려 5시간 걸려 만들었던거....)를 넣어주고

// 다시 cmake 해라.




중간에 다운로드 때문에 시간이 약간 걸린다.



-- General configuration for OpenCV 3.4.0 =====================================

--   Version control:               3.4.0

-- 

--   Extra modules:

--     Location (extra):            /home/pi/opencv_contrib/modules

--     Version control (extra):     3.4.0

-- 

--   Platform:

--     Timestamp:                   2018-03-10T03:14:53Z

--     Host:                        Linux 4.9.59-v7+ armv7l

--     CMake:                       3.7.2

--     CMake generator:             Unix Makefiles

--     CMake build tool:            /usr/bin/make

--     Configuration:               RELEASE

-- 

--   CPU/HW features:

--     Baseline:

--       requested:                 DETECT

--       disabled:                  VFPV3 NEON

-- 

--   C/C++:

--     Built as dynamic libs?:      YES

--     C++11:                       YES

--     C++ Compiler:                /usr/bin/c++  (ver 6.3.0)

--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG

--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG

--     C Compiler:                  /usr/bin/cc

--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG

--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG

--     Linker flags (Release):

--     Linker flags (Debug):

--     ccache:                      NO

--     Precompiled headers:         YES

--     Extra dependencies:          dl m pthread rt

--     3rdparty dependencies:

-- 

--   OpenCV modules:

--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dpm face features2d flann freetype fuzzy highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto

--     Disabled:                    js world

--     Disabled by dependency:      -

--     Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv dnn_modern hdf java matlab ovis sfm viz

--     Applications:                tests perf_tests examples apps

--     Documentation:               NO

--     Non-free algorithms:         NO

-- 

--   GUI: 

--     GTK+:                        YES (ver 2.24.31)

--       GThread :                  YES (ver 2.50.3)

--       GtkGlExt:                  NO

--     VTK support:                 NO

-- 

--   Media I/O: 

--     ZLib:                        /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.8)

--     JPEG:                        /usr/lib/arm-linux-gnueabihf/libjpeg.so (ver )

--     WEBP:                        build (ver encoder: 0x020e)

--     PNG:                         /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.6.28)

--     TIFF:                        /usr/lib/arm-linux-gnueabihf/libtiff.so (ver 42 / 4.0.8)

--     JPEG 2000:                   /usr/lib/arm-linux-gnueabihf/libjasper.so (ver 1.900.1)

--     OpenEXR:                     build (ver 1.7.1)

-- 

--   Video I/O:

--     DC1394:                      NO

--     FFMPEG:                      YES

--       avcodec:                   YES (ver 57.64.101)

--       avformat:                  YES (ver 57.56.101)

--       avutil:                    YES (ver 55.34.101)

--       swscale:                   YES (ver 4.2.100)

--       avresample:                NO

--     GStreamer:                   

--       base:                      YES (ver 1.10.4)

--       video:                     YES (ver 1.10.4)

--       app:                       YES (ver 1.10.4)

--       riff:                      YES (ver 1.10.4)

--       pbutils:                   YES (ver 1.10.4)

--     libv4l/libv4l2:              NO

--     v4l/v4l2:                    linux/videodev2.h

--     gPhoto2:                     NO

-- 

--   Parallel framework:            pthreads

-- 

--   Trace:                         YES (built-in)

-- 

--   Other third-party libraries:

--     Lapack:                      NO

--     Eigen:                       YES (ver 3.3.2)

--     Custom HAL:                  YES (carotene (ver 0.0.1))

-- 

--   NVIDIA CUDA:                   NO

-- 

--   OpenCL:                        YES (no extra features)

--     Include path:                /home/pi/opencv/3rdparty/include/opencl/1.2

--     Link libraries:              Dynamic load

-- 

--   Python 2:

--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.13)

--     Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython2.7.so (ver 2.7.13)

--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.12.1)

--     packages path:               lib/python2.7/dist-packages

-- 

--   Python 3:

--     Interpreter:                 /usr/bin/python3 (ver 3.5.3)

--     Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython3.5m.so (ver 3.5.3)

--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.12.1)

--     packages path:               lib/python3.5/dist-packages

-- 

--   Python (for build):            /usr/bin/python2.7

-- 

--   Java:

--     ant:                         NO

--     JNI:                         NO

--     Java wrappers:               NO

--     Java tests:                  NO

-- 

--   Matlab:                        NO

-- 

--   Install to:                    /usr/local

-- -----------------------------------------------------------------

-- 

-- Configuring done

-- Generating done

-- Build files have been written to: /home/pi/opencv/build



이제 빌드

// j옵션은 병열로 처리하도록 한다. 그냥 j옵션 쓰지않는다. 좀 더 늦어지겠지만 중간에 뻗지는 않는다.




이는 stretch-lite에서 시험해봤다.


// 이게 메모리 어쩌고 확장하고 하면 된다던데 나중에....

// https://www.pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/

// 위 링크에 Configure your swap space size before compiling 참고


라즈베리파이의  코어 네개 모두 사용하여 컴파일하려면 -j4를 쓰면되나

컴파일 하는 도중 메모리 문제 때문에 행이 걸리는 현상이 발생한다.


이를 피하기 위해 swap 크기를 변경해준다.


/etc/dphys-swapfile 을 열어 CONF_SWAPSIZE 를 1024로 변경해준다.


$ sudo vi /etc/dphys-swapfile


# set size to absolute value, leaving empty (default) then uses computed value

#   you most likely don't want this, unless you have an special disk situation

CONF_SWAPSIZE=100


디폴트로 100 MB로 되어 있을 것이다.

이를 주석처리하고 1024 MB 로 변경한다.


#CONF_SWAPSIZE=100

CONF_SWAPSIZE=1024


새로운 swap 을 활성화하기 위해 swap 서비스를 리스타트한다.


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

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


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 

컴파일 후 반드시 원래대로 돌려야한다.

안그러면.... SD카드 오래 못쓴다.....

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 




// 12:21 시작

$ make      


15:12 32% 

17:28 62% 

20:37 100% ...


풀로하니까 더럽게 오래 걸리네 5시간?!?!?!?!


이제 설치...


$ sudo make install

$ sudo ldconfig



설치 확인

$ python3

> import cv2

> cv2.__version__