树莓派安装OpenCV和Tensorflow


# config.txt 新增如下行(烧录后,启动前修改)
dtparam=audio=on
max_usb_current=1
hdmi_group=2
hdmi_mode=1
hdmi_mode=87
hdmi_cvt 1024 600 60 6 0 0 0
start_x=1
gpu_mem=128

# 摄像头, SSH, 扩展使用整个SD卡等, 中英文locale
sudo raspi-config 
sudo vi /etc/default/locale

LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
LC_ALL=C

# 替换Raspbian源为国内源
sudo vi /etc/apt/sources.list

#清华
deb https://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
#阿里云
deb http://mirrors.aliyun.com/raspbian/raspbian/ stretch main contrib non-free rpi 


#替换树莓派基金会的源
sudo vi /etc/apt/sources.list.d/raspi.list
#清华
deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui

# pip国内源
mkdir ~/.pip
vi ~/.pip/pio.conf

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple


# 设置vim
sudo apt-get install vim
git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime
sh ~/.vim_runtime/install_awesome_vimrc.sh

# 增大swap空间
opencv编译会消耗大量内存,树莓派内存太小,所以只有增大swap来完成
$vi  /etc/dphys-swapfile
# 找到CONF_SWAPSIZE=100改为1024
CONF_SWAPSIZE=1024
# 重启交换服务
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start


# 更新pip
python -m pip install -U pip
此时会出现错误: ImportError: cannot import name 'main'
sudo vi /usr/bin/pip3, 修改为如下行:
from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

pip3 --version
显示如下信息为正常
pip 19.0.3 

# 更新
sudo apt update
sudo apt upgrade

# 摄像头硬件
sudo vi /etc/modules

snd-bcm2835
bcm2835-v4l2

# opencv 依赖的工具
sudo apt install build-essential cmake git pkg-config


# 视频I/O包
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

# install numpy, scipy, matplotlib
sudo -E pip3 install -U numpy
pip3 install scipy  matplotlib


# gtk2.0和优化函数包
sudo apt install libgtk2.0-dev libgtk-3-dev
sudo apt install libatlas-base-dev gfortran
sudo apt install libatlas3-base


# 内置python2.7升级为python3
sudo apt install python3
sudo apt remove python
sudo rm /usr/bin/python
sudo rm -rf /usr/lib/python2.7
sudo ln -s /usr/bin/python3.5 /usr/bin/python

# opencv 依赖的lib
sudo apt install libjpeg8-dev 
sudo apt install libtiff5-dev 
sudo apt install libjasper-dev 
sudo apt install libpng12-dev

sudo apt install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev 
sudo apt install libgtkglext1-dev libv4l-0 libv4l-dev v4l-utils

# opencv 源码

官网:https://github.com/opencv/opencv/releases
wget -O opencv-3.4.3.zip https://github.com/Itseez/opencv/archive/3.4.3.zip
unzip opencv-3.4.3.zip
wget -O opencv_contrib-3.4.3.zip https://github.com/Itseez/opencv_contrib/archive/3.4.3.zip
unzip opencv_contrib-3.4.3.zip
#解压缩
unzip opencv-3.4.3.zip
unzip opencv_contrib-3.4.3.zip


.cache文件
下载 https://dl-download.csdn.net/down11/20171226/ac86990ac9f78c4c347bbeeb2ee1f21d.zip?response-content-disposition=attachment%3Bfilename%3D%22opencv3.4.0%E7%9A%84.cache%E6%96%87%E4%BB%B6%E5%A4%B9.zip%22&OSSAccessKeyId=9q6nvzoJGowBj4q1&Expires=1551978556&Signature=HQsk5Fpo7JwhDcmsbaLktaVqeAE%3D&user=weixin_39116068&sourceid=10173967&sourcescore=6&isvip=1&weixin_39116068&10173967
解压缩覆盖: /home/pi/Downloads/opencv-3.4.3/.cache 目录


源码方式参考教程
# https://github.com/iotJumpway/RPI-Examples/blob/master/_DOCS/2-Installing-OpenCV.md
cd opencv-3.4.0
mkdir build
cd build
modules_path=~/Downloads/opencv_contrib-3.4.3/modules
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D BUILD_opencv_python3=YES \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=${modules_path}\
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D INSTALL_C_EXAMPLES=OFF \
      -D BUILD_EXAMPLES=OFF ..

make -j4 && sudo make install
运行 make install之后,您的OpenCV + Python绑定应安装在/usr/local/lib/python3.5/site-packages
cd /usr/local/lib/python3.5/site-packages/
sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so

sudo vi /etc/ld.so.conf.d/opencv.conf
文件是空的, 添加如下行:
/usr/local/lib          # enter this in opencv.conf, NOT at the command line
更新动态库
sudo ldconfig
sudo vi /etc/bash.bashrc
添加如下行在bash.bashrc尾部
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig       
export PKG_CONFIG_PATH

# 安装tensorflow
sudo apt install libatlas-base-dev
sudo pip3 install tensorflow


# 测试tensorflow

import tensorflow as tf
tf.enable_eager_execution()
hello = tf.constant('Hello, TensorFlow!')
print(hello)

# 测试代码
# -*- coding: utf-8 -*-
__author__ = "kyoRan"

import cv2

# 打开摄像头
cap = cv2.VideoCapture(0)                                        
print("VideoCapture is opened?", cap.isOpened())

while(True):
    # 读取摄像头图像
    ret, frame = cap.read()                                      
    # 图像中心点位置
    center = (frame.shape[1]//2, frame.shape[0]//2)              

    # 转灰度
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    # 画圆
    cv2.circle(gray, center=center, radius=100, color=(0,0,255))
    # 显示图片
    cv2.imshow("frame", gray)                                    

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()            # 释放摄像头
cv2.destroyAllWindows()  # 关闭所有窗口
 

你可能感兴趣的:(树莓派,ROS,智能车,SLAM)