參考教程:關於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對的,多動手有好處!