在树莓派中安装opencv库
- 参考教程:关于opencv的编译安装,可以参考Adrian Rosebrock的Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi。
Step #1: Expand filesystem
- $ sudo raspi-config
- 选中Advanced Options
- 选中Expand filesystem
- $ sudo reboot
- $ df -h #查看扩展后的空间大小,如果空间不够可执行下面指令
$ sudo apt-get purge wolfram-engine
$ sudo apt-get purge libreoffice*
$ sudo apt-get clean
$ sudo apt-get autoremove
Step #2: Install dependencies
#更新和升级包
$ sudo apt-get update && sudo apt-get upgrade
#安装Cmake
$ sudo apt-get install build-essential cmake pkg-config
#安装 image I/O packages
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
#安装 video I/O packages
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev
#安装GTK development library
$ sudo apt-get install libgtk2.0-dev libgtk-3-dev
$ sudo apt-get install libatlas-base-dev gfortran
#安装python 2.7和python 3头文件
$ sudo apt-get install python2.7-dev python3-dev
Step #3: Download the OpenCV source code
#从OpenCV repository grab archive
$ cd ~
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip
$ unzip opencv.zip
#扩展安装OpenCV
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
$ unzip opencv_contrib.zip
下载离线的OpenCV并传送到树莓派里
Step #4: Python 3
#安装pip
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
$ sudo python3 get-pip.py
#安装virtualenv和virtualenvwrapper
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip
#更新~/.profile文件
vim ~/.profile
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
#刷新更改
$ source ~/.profile
#创建python virtual environment
$ mkvirtualenv cv -p python3
#进入 CV virtual environment,成功进入会在最左边看见(cv)
$ workon cv
#安装 NumPy(这个耗时比较长,大概十来分钟,请耐心等待)
$ pip install numpy
更新.profile文件
进入cv
Step #5: Compile and Install OpenCV
#进入cv,setup build CMake
$ workon cv(已经在cv里面可忽略)
$ cd ~/opencv-3.3.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.3.0/modules
-D BUILD_EXAMPLES=ON ..
#检查CMake的输出,确保Python 3的Interpreter,Libraries,numpy 和packages path都有确定的path
#扩大交换空间(这个要注意不能一直使用,在完成编译和测试安装后要记得还原成默认值,否者会烧坏SD卡)
$ vim /etc/dphys-swapfile
CONF_SWAPSIZE=1024
#重启交换服务
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start
#Complie OpenCV(这个最为耗时,大概一个多小时)
$ make -j4
#安装 OpenCV3
$ sudo make install
$ sudo ldconfig
编译完成
Step #6: Finish installing OpenCV
#检查OpenCV + Python bindings
$ ls -l /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
#sym-link OpenCV bindings into the cv
$ cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so
Step #7: Testing your OpenCV 3 install
#验证安装成功
$ source ~/.profile
$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'3.3.0'
>>>
#如果有如上信息则可执行下面指令
$ rm -rf opencv-3.3.0 opencv_contrib-3.3.0
#最后不要忘记将交换空间改为默认值
CONF_SWAPSIZE=xxx
#重启交换服务
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start
验证opencv的安装
使用opencv和python控制树莓派的摄像头
- 参考教程:Accessing the Raspberry Pi Camera with OpenCV and Python**](https://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/)
source ~/.profile
workon cv
pip install "picamera[array]"
拍照测试
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
camera = PiCamera()
rawCapture = PiRGBArray(camera)
time.sleep(5) # 感光时间需要长一些
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
cv2.imshow("Image", image)
cv2.waitKey(0)
视频流测试
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
time.sleep(0.1)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)
if key == ord("q"):
break
利用树莓派的摄像头实现人脸识别
参考教材树莓派实现简单的人脸识别
文件目录
安装依赖库
pip install dlib face_recognition numpy
facerec_on_respberry测试结果
facerec_from_webcam_faster测试结果
结合微服务的进阶任务
使用微服务,部署opencv的docker容器(要能够支持arm),并在opencv的docker容器中跑通(3)的示例代码facerec_on_raspberry_pi.py
安装Docker
#下载安装脚本
curl -fsSL https://get.docker.com -o get-docker.sh
# 运行安装脚本(阿里云镜像)
sh get-docker.sh --mirror Aliyun
部署opencv的docker容器
- 拉取镜像 DockerHub镜像地址
-
创建并运行容器
-
进入容器后运行跑通facerec_on_raspberry
小结
问题和解决方法
-
numpy下载报超时错误
最后在尝试网上的换源大法失败后只能离线下载
-
在构建虚拟环境时没找到可执行脚本
-
AVFMT_RAWPICTURE未定义错误
添加#define内容并重新编译
-
缺少boostdesc_bgm.i
缺失文件下载链接:点我进入下载后将所需文件添加到相应目录
-
缺少hpp类错误
这个出错是由于cuda.hpp文件不存在,但其实这个文件实际存在于opencv-contrib目录下,可以在代码中将其路径修改为文件所在目录对应的绝对路径。sudo nano /home/pi/opencv-3.3.0/modules/stitching/src/precomp.hpp #include "opencv2/xfeatures2d/cuda.cpp" //改成如下 #include "/.../opencv_contrib-3.3.0/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.cpp"
成员名单和分工
学号 | 姓名 | 贡献 |
---|---|---|
031702525 | 周鑫煌 | 查阅资料 部分博客 |
031702316 | 翟鑫亮 | 查阅资料 错误总结 |
031702337 | 叶琦熠 | 实际操作 |
在线协作图片