参考教程:关于opencv的编译安装,能够参考Adrian Rosebrock的Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi。html
$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt-get install build-essential 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 libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev
$ sudo apt-get install libgtk2.0-dev libgtk-3-dev
$ sudo apt-get install libatlas-base-dev gfortran
$ sudo apt-get install python2.7-dev python3-dev
$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip $ unzip opencv.zip
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py $ sudo python3 get-pip.py
超时了。。。python
而后从新弄就成功了。。。linux
$ sudo pip install virtualenv virtualenvwrapper $ sudo rm -rf ~/.cache/pip
$ echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.profile $ echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.profile $ echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.profile $ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.profile $ source〜/ .profile
$ mkvirtualenv cv -p python2 $ source ~/.profile $ workon cv
又又又超时了。。。git
换了个时间就成功了github
$ 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 ..
# 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 CONF_SWAPSIZE=1024
$ sudo /etc/init.d/dphys-swapfile stop $ sudo /etc/init.d/dphys-swapfile start
$ make -j4 $ sudo make install $ sudo ldconfig
$ ls -l /usr/local/lib/python3.7/site-packages/
$ cd /usr/local/lib/python3.7/site-packages/ $ sudo mv cv2.cpython-37m-arm-linux-gnueabihf.so cv2.so #注意这里的python-37m
$ cd ~/.virtualenvs/cv/lib/python3.7/site-packages/ $ ln -s /usr/local/lib/python3.7/site-packages/opencv-3.3.0.so cv2.so
$ source ~/.profile $ workon cv $ python >>> import cv2 >>> cv2.__version__ '3.3.0' >>>
从截图中能够看到,OpenCV 3已成功安装在个人Raspberry Pi 3 + Python 3.7环境中web
# 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 # CONF_SWAPSIZE=1024
$ sudo /etc/init.d/dphys-swapfile中止 $ sudo /etc/init.d/dphys-swapfile开始
参考教程:仍是能够参考Adrian Rosebrock的Accessing the Raspberry Pi Camera with OpenCV and Python
跑通教程的示例代码(有可能要调整里面的参数)docker
示例代码1:test_image.pybootstrap
# 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(3) //0.1改为3 # 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)
示例代码2:test_vedio.pywindows
# 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(3) # 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库face_recognition,这当中有不少示例代码
参考教程:树莓派实现简单的人脸识别
要求:跑通[face_recognition]的示例代码facerec_on_raspberry_pi.py以及facerec_from_webcam_faster.py网络
ls /dev/video*
pip install dlib pip install face_recognition
使用微服务,部署opencv的docker容器(要可以支持arm),并在opencv的docker容器中跑通(3)的示例代码facerec_on_raspberry_pi.py
选作:在opencv的docker容器中跑通步骤(3)的示例代码facerec_from_webcam_faster.py
#脚本安装docker curl -fsSL https://get.docker.com -o get-docker.sh #下载安装脚本 sh get-docker.sh --mirror Aliyun #执行脚本 sudo usermod -aG docker $USER #填加用户组,这在树莓派reboot后docker指令就无需sudo特权了
docker --version
这里配置信息device即挂载本机摄像头设备
docker run -it --device=/dev/vchiq --device=/dev/video0 --name myopencv opencv2 python3 facerec_on_raspberry_pi.py
注:X11forwarding 显示yes便可
$ cat /etc/ssh/sshd_config
注:这个查看是在用Putty打开的窗口上查看到的,若直接在树莓派里的终端看,看到就是"DISPLAY=localhost:10.0"
printenv
#sudo apt-get install x11-xserver-utils xhost + docker run -it \ --net=host \ -v $HOME/.Xauthority:/root/.Xauthority \ -e DISPLAY=:10.0 \ -e QT_X11_NO_MITSHM=1 \ --device=/dev/vchiq \ --device=/dev/video0 \ --name facerecgui \ myopencv \ python3 facerec_from_webcam_faster.py
sh run.sh
能够看到在windows的Xvideo能够正确识别人脸。
可见速度之慢
解决方法:
-刚开始不太了解pip的时候就无脑挂机干等,可是解决不了timeout报错,肝到半夜发现凌晨4点的洛杉矶···网速真快
-后来按群里大佬的谈话发现,手动复制链接去下载whl文件而后放到python3工做目录下的site-package文件便可
-虽然一开始有想到这个方法,但不知道该文件包放哪才能让pip找到而无奈劝退...
该文件的指定路径其实没有这个文件,但其实在xfeature2d这个东西在opencv_contrib是能够找到的
解决:
-有大佬是修改相应路径
-而咱们选择暴力复制一个xfeature2d到路径下便可
解决:
-百度了一下才发现应该是须要添加头文件
-home/pi/opencv-3.3.0/modules/videoio/src/cap_ffmpeg_impl.hpp下添加
#define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22) #define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER #define AVFMT_RAWPICTURE 0x0020
-这里可能会缺不少文件,不止截图中的一个
-缘由彷佛是由于在使用cmake建立build任务的那里,会出现你所须要的boostdesc部分文件由于网络没法链接而无法如今
解决:
-网上可找到相应部分的资源,下载解压而后放进相应路径下便可
解决:
-使人头痛的解决问题,编译问题很长也且容易被跳过(反正可能就是菜看不出来)
-问了群上大佬才知道须要进入相关文件去修改字符类型(大佬仍是强的,觉得是群里某大佬的回答)
编译问题
学号 | 姓名 | 分工 |
---|---|---|
031702220 | 黄恒杰 | 实机操做,博客审定,查阅资料 |
031702223 | 郑志强 | 博客编写,查阅资料,寻找解决方法 |
031702239 | 林国钦 | 博客编写,查阅资料,寻找解决方法 |
主要经过qq视频,群聊进行沟通和资料分享
此次实验对于opencv的安装编译真是吐了...
成功安装opencv后,可能由于动到电源或是开过久,树莓派断连,而后连不上?看指示灯觉得烧坏了仍是啥...
心态差点炸了...重作咯
参照以前的试卷答案,彷佛挺多人选择直接pip?
可是直接pip下的opencv彷佛是在本地python上运行的而非cv下?这彷佛失去了python虚拟环境的意义
老师说可跑便可?那python2下的直接installopencv不是更为简单?这里的教程安装py2下的opencv不要太舒服 但慢慢作到后面发现,学docker直接拉取opencv相关镜像额......那安装那个东西......emm对的,多动手有好处!