RK3399平台 Debian9搭建TensorFlow,OpenCV

RK3399平台 Debian9搭建TensorFlow,OpenCV

一、平台说明

  1. 硬件平台:瑞芯微RK3399
  2. 系统版本:Debian9
  3. 软件版本:TensorFlow 1.14.0 Python3.7 OpenCV3.2.0

二、详细步骤

  1. 更新系统

    apt update && apt upgrade 
    
  2. 安装必要的环境

    apt install build-essential libpython3.7-dev wget python3.7-pip python3.7-dev python3.7
    

    编译OpenCV所依赖的库

    apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
    

    可选择安装的库

    apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libdc1394-22-dev
    

    运行时要安装的库

    apt install libgtk2.0-dev pkg-config
    

    没有情况,就一起安装上,避免后续还要安装。

  3. 下载tensorflow

    wget https://github.com/lhelontra/tensorflow-on-arm/releases/download/v2.0.0/tensorflow-1.14.0-cp37-none-linux_aarch64.whl
    
  4. 安装tensorflow

    python3.7 -m pip install tensorflow-1.14.0-cp37-none-linux_aarch64.whl
    

    报“error: invalid command ‘bdist_wheel’”的错误,升级pip,再次安装即可

    python3.7 -m pip install --upgrade pip
    pip3.7 install --upgrade setuptools
    python3.7 -m pip install tensorflow-1.14.0-cp37-none-linux_aarch64.whl
    

    等待安装完成,时间有点长,最后打印的。

    Successfully built numpy grpcio
    Installing collected packages: numpy, astor, grpcio, gast, werkzeug, markdown, tensorboard, tensorflow(numpy, setuptools, gast, astor, wheel, grpcio, termcolor, protobuf, absl-py, markdown, werkzeug, tensorboard, tensorflow)
    Successfully installed astor-0.8.0 gast-0.2.2 grpcio-1.23.0 markdown-3.1.1 numpy-1.17.1 tensorboard-1.14.0 tensorflow-1.14.0 werkzeug-0.15.6
    
  5. 测试

    root@linaro-alip:~# python3 demo.py
    
    root@linaro-alip:~# python3
    >>> import tensorflow as tf
    >>> hello = tf.constant('Hello world!')
    >>> sess = tf.Session()
    >>> print(sess.run(hello))
    >>> b'Hello world!'
    >>> exit()
    

    demo.py代码如下

    import tensorflow as tf
    hello = tf.constant('Hello world!')
    sess = tf.Session()
    print(sess.run(hello))
    
  6. 下载OpenCV源码

    git clone https://github.com/opencv/opencv.git
    cd opencv/
    git checkout 3.2.0
    

    要使用的额外的模块

    git clone https://github.com/opencv/opencv_contrib.git
    cd opencv_contrib/
    git checkout 3.2.0
    

    注意:
    如果到网页下载,可能目录名不一样。cmake配置时要注意目录名就可以了。

  7. 编译OpenCV

    cd opencv/
    #配置环境变量
    export PY_NAME=$(python -c 'from sys import version_info as v; print("python%d.%d" % v[:2])')
    export PY_NUMPY_DIR=$(python -c 'import os.path, numpy.core; print(os.path.dirname(numpy.core.__file__))')
    
    cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DPYTHON2_EXECUTABLE=$(which python3) -DPYTHON_INCLUDE_DIR=/usr/include/$PY_NAME -DPYTHON_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/$PY_NAME -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/lib$PY_NAME.so -DPYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/$PY_NAME/dist-packages/numpy/core/include/ -DBUILD_DOCS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
    #编译安装
    make 
    make install
    
  8. 添加环境变量

    /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
    ldconfig
    ONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
    #查看有没有添加成功
    export PKG_CONFIG_PATH
    
  9. 测试

    修改canny.py的权限,然后执行./canny.py
    

    canny.py代码如下:

    #!/usr/bin/env python
    import cv2
    import numpy as np
    def main():
        img = cv2.imread('/root/1.jpeg', 0) #修改本地图片的路径
        edges = cv2.Canny(img, 100, 200)
        cv2.imshow('Original & Edge', np.vstack((img, edges)))
        cv2.waitKey()
        cv2.destroyAllWindows()
    if __name__ == '__main__':
        main()
    
    

注意:
上面过程安装,都要root权限。因为在ARM平台运行,默认就是root登录。

你可能感兴趣的:(嵌入式开发相关工具,tensorflow,opencv,python)