2017年3月12日 星期日

Raspberry Pi: Install OpenCV 3 with Python2,Python3

since: 2017/03/12
since: 2017/03/31
reference:
1. Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3 - PyImageSearch
2. Accessing the Raspberry Pi Camera with OpenCV and Python - PyImageSearch

A. 前置準備
    1. Raspberry Pi 3
    2. 16 GB microSD Card (編譯 OpenCV 大約會佔用 3G 的容量)
    3. Camera Module
    4. Enable Camera Interface
         > $ sudo raspi-config




    5. (optional) delete the Wolfram engine to free up some space
        $ sudo apt-get purge wolfram-engine

    6. check Python version
        $ python -V
           Python 2.7.9

        $ python3 -V
           Python 3.4.2

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

B. 安裝相關套件
    1. update and upgrade any existing packages
        $ sudo apt-get update
        $ sudo apt-get upgrade

    2. install some developer tools
        $ sudo apt-get install build-essential cmake pkg-config

    3. install some image I/O packages
        $ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev


    4. install some video I/O packages
        $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
        $ sudo apt-get install libxvidcore-dev libx264-dev

    5. install the GTK development for highgui
        $ sudo apt-get install libgtk2.0-dev

    6. installing a few extra dependencies for optimized
        $ sudo apt-get install libatlas-base-dev gfortran

    7. install both the Python 2.7 and Python 3 header files
        $ sudo apt-get install python2.7-dev python3-dev

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

C. 下載 OpenCV source code
     1. opencv
         $ cd ~
         $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.2.0.zip
         $ chmod 755 opencv.zip
         $ unzip opencv.zip

     2. opencv_contrib (full install of OpenCV)
         $ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.2.0.zip
         $ chmod 755 opencv_contrib.zip
         $ unzip opencv_contrib.zip

Note: Make sure your opencv  and opencv_contrib  versions are the same
          (in this case, 3.2.0)


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

D. Install Python package manager
     $ wget https://bootstrap.pypa.io/get-pip.py
     $ chmod 755 get-pip.py
     $ sudo python get-pip.py
     $ sudo python3 get-pip.py

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

E. Installing NumPy
    $ pip install numpy (it may take a bit of time)

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

F. Compile and Install OpenCV
    $ cd ~/opencv-3.2.0
    $ mkdir build
    $ cd build
    $ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.2.0/modules -D BUILD_EXAMPLES=ON ..

備註: 選擇性參數: -D INSTALL_C_EXMAPLES=ON

   => Ensuring that Python 2.7 and Python 3 will be used

   > compile OpenCV
     // The Raspberry Pi 3 has four cores, thus we supply a value of 4  to allow OpenCV
     // to compile faster. However, due to race conditions, there are times when
     // make  errors out when using multiple cores.
     // $ make -j4

     $ make // about 7 hours
     $ sudo make install
     $ sudo ldconfig

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

G. 檢查與測試安裝結果
     1. 檢查
         $ ls -al /usr/local/lib/python2.7/dist-packages/cv2.so
         $ ls -al /usr/local/lib/python3.4/dist-packages/cv2.cpython-34m.so
         //$ cd /usr/local/lib/python3.4/dist-packages/
         //$ sudo cp cv2.cpython-34m.so cv2.so    

     2. 測試 
// python2
$ cd
$ python
>>> import cv2
>>> cv2.__version__
'3.2.0'
>>> exit()

// python3
$ cd
$ python3
>>> import cv2
>>> cv2.__version__
'3.2.0'
>>> exit()

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

H. 程式測試   
     1. Test out the camera module
         $ raspistill -o output.jpg

-----------------------------------------------------------------------------------------------
     2. Installing picamera
         // when using Python bindings, OpenCV represents images as NumPy arrays

         $ pip install "picamera[array]"

-----------------------------------------------------------------------------------------------
     3. Grabbing a single image (需要先啟動 X Window: $ startx)
         // test_image.py


# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)

# allow the camera to warmup
time.sleep(0.1)

# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array

# display the image on screen and wait for a keypress
cv2.imshow("Image", image)
cv2.waitKey(0)


$ python test_image.py


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

     4. access the video stream (需要先啟動 X Window: $ startx)
         // test_video.py

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))

# allow the camera to warmup
time.sleep(0.1)

# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    # grab the raw NumPy array representing the image, then initialize the timestamp
    # and occupied/unoccupied text

    image = frame.array

    # show the frame
    cv2.imshow("Frame", image)
    key = cv2.waitKey(1) & 0xFF

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

 
$ python test_video.py


沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。