(1) 在樹莓派中安裝opencv庫
參考教程:關於opencv的編譯安裝,可以參考 Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi
python2的opencv上節課就配置完畢

接下來配置python3的opencv
-
安裝numpy(樹莓派python3自帶)
sudo pip3 install numpy -
擴大TF卡的空間(上次實驗做過)
sudo raspi-config

-
開始為OpenCV安裝做准備,配置所需的庫(一次一行)
sudo apt-get install build-essential git cmake pkg-config -y sudo apt-get install libjpeg8-dev -y sudo apt-get install libtiff5-dev -y sudo apt-get install libjasper-dev -y sudo apt-get install libpng12-dev -y sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y sudo apt-get install libgtk2.0-dev -y sudo apt-get install libatlas-base-dev gfortran -y下載Opencv3.3.0
wget https://github.com/Itseez/opencv/archive/3.3.0.zip wget https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip解壓后在opencv-3.3.0文件夾里創建build文件夾,然后在命令行里
面cd到此文件夾
cd opencv-3.4.0 mkdir build cd build -
開始cmake
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=/home/pi/opencv/opencv_contrib-3.3.0/modules \ -D BUILD_EXAMPLES=ON \ -D WITH_LIBV4L=ON \ -D PYTHON3_EXECUTABLE=/usr/bin/python3.7 \ -D PYTHON_INCLUDE_DIR=/usr/include/python3.7 \ -D PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.7m.so \ -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \ ..注意OPENCV_EXTRA_MODULES_PATH后跟的路徑為自己解壓后的路徑,注意python版本。 -
開始編譯
make && sudo make install -
出錯

裝了這么久失敗了,一怒之下直接pip3 install opencv-python
成功!
(2) 使用opencv和python控制樹莓派的攝像頭
參考教程:還是可以參考Accessing the Raspberry Pi Camera with OpenCV and Python
跑通教程的示例代碼(有可能要調整里面的參數)
-
下面這個是第6次實驗中我已經完成過了的
通過攝像頭實時拍攝查看視頻
#opencvtest.py import cv2 import datetime cap = cv2.VideoCapture(0) print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #cap.set(3, 3000) #cap.set(4, 3000) #print(cap.get(3)) #print(cap.get(4)) while(cap.isOpened()): ret, frame = cap.read() if ret == True: font = cv2.FONT_HERSHEY_SIMPLEX text = 'Width: '+ str(cap.get(3)) + ' Height:' + str(cap.get(4)) datet = str(datetime.datetime.now()) frame = cv2.putText(frame, text, (10, 50), font, 1, (255, 255, 255), 2, cv2.LINE_AA) frame = cv2.putText(frame, datet, (10, 100), font, 1, (255, 255, 255), 2, cv2.LINE_AA) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() cv2.destroyAllWindows()

(3) 利用樹莓派的攝像頭實現人臉識別
- 參考資料------樹莓派實現簡單的人臉識別
- 安裝face_recognition庫的命令。
pip install dlib
pip install face_recognition
- 安裝dlib的速度還好,但是安裝face_recognition一直超時,因為國內的源沒有對應的.whl文件,所以我選擇了直接從網上下載進行安裝,文件如下。

- 用xftp向樹莓派傳遞這個文件,因為之前我們已經做了內網穿透,外網也可以連入樹莓派。

- 安裝下載好的文件。
python -m pip install face_recognition_models-0.3.0-py2.py3-none-any.whl
- 之后再次執行pip install face_recognition,就可以很快完成安裝了。

- 如圖,表明已經安裝成功。

- 接下來進行兩個測試,第一個facerec_on_raspberry_pi.py,該程序會檢測人臉,如果檢測到人臉會在終端顯示出來,如果檢測到奧巴馬的人臉還會顯示奧巴馬的名字。這里用的是python3,因為2.7會報錯,需要重新安裝一些庫。

- 第二個facerec_from_webcam_faster.py,該程序會顯示攝像頭實時拍攝的畫面,如果檢測到人臉會有一個方框顯示,該程序能特別的識別出奧巴馬和拜登的人臉。


(4)結合微服務的進階任務
參考資料
①docker的安裝與配置
-
安裝docker
直接使用sudo curl -sSL https://get.docker.com | sh安裝docker即可 -
設置docker
# Add pi to Docker group
sudo usermod pi -aG docker
# config cgroup for Docker
echo Adding " cgroup_enable=cpuset cgroup_enable=memory" to /boot/cmdline.txt
sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt
# if you encounter problems, try changing cgroup_memory=1 to cgroup_enable=memory.
orig="$(head -n1 /boot/cmdline.txt) cgroup_enable=cpuset cgroup_memory=1"
echo $orig | sudo tee /boot/cmdline.txt
sudo reboot
②facerec_on_raspberry_pi
- 下載示例鏡像並開啟容器
docker run -it \
--name face_recognition \
--device /dev/vchiq \
registry.cn-hangzhou.aliyuncs.com/denverdino/face_recognition \
bash
-
代碼說明
- 其包含了face_recognition 的完整開發環境和示例應用
- 將攝像頭設備
/dev/vchiq掛載到容器內部,這樣就可以讓容器中的應用來拍攝照片和視頻。 - 可以利用 docker cp 命令,向容器中拷貝文件
-
應用示例代碼facerec_from_raspberry_faster.py
在上一步中我們已經進入了容器中,現在我們進入代碼存放目錄
cd face_recognition/examples/
可以看到示例代碼:
# This is a demo of running face recognition on a Raspberry Pi.
# This program will print out the names of anyone it recognizes to the console.
# To run this, you need a Raspberry Pi 2 (or greater) with face_recognition and
# the picamera[array] module installed.
# You can follow this installation instructions to get your RPi set up:
# https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
import face_recognition
import picamera
import numpy as np
# Get a reference to the Raspberry Pi camera.
# If this fails, make sure you have a camera connected to the RPi and that you
# enabled your camera in raspi-config and rebooted first.
camera = picamera.PiCamera()
camera.resolution = (320, 240)
output = np.empty((240, 320, 3), dtype=np.uint8)
# Load a sample picture and learn how to recognize it.
print("Loading known face image(s)")
obama_image = face_recognition.load_image_file("obama_small.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
# Initialize some variables
face_locations = []
face_encodings = []
while True:
print("Capturing image.")
# Grab a single frame of video from the RPi camera as a numpy array
camera.capture(output, format="rgb")
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(output)
print("Found {} faces in image.".format(len(face_locations)))
face_encodings = face_recognition.face_encodings(output, face_locations)
# Loop over each face found in the frame to see if it's someone we know.
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
name = "<Unknown Person>"
if match[0]:
name = "Barack Obama"
print("I see someone named {}!".format(name))
- 運行
python3 facerec_on_raspberry_pi.py
結果:

③facerec_from_webcam_faster
- 應用示例代碼facerec_from_webcam_faster.py
令人很窒息的是,face_recognition並沒有安裝opencv,而安裝opencv實在是太麻煩了,so...只能去都出docker hub上看看有沒有現成的,但是沒有真正滿足需求的,最后找到了一個有opencv的鏡像
- 權限分配
# 允許所有用戶訪問顯示接口,在樹莓派擁有者端操作比較方便
xhost +
- 創建容器
docker run -it --rm \ # 可交互,用完即刪
--device=/dev/video0 \ # 攝像頭
-e DISPLAY=unix$DISPLAY \ # 修改環境變量DISPLAY
-v /tmp/.X11-unix:/tmp/.X11-unix \ # 共享本地unix端口
-v /home/pi/Desktop/face-recognition:/face-recognition \ # 掛載目錄
demosense/raspberrypi3-opencv \
bash
- 安裝依賴
pip install picamera dlib face_recognition numpy
apt install x11-xserver-utils
- 測試代碼`python facerec_from_webcam_faster.py`

**上一步的facerec_from_raspberry_faster.py用例也可以實現**

(4)小組協作
| 學號 | 姓名 | 任務 |
|---|---|---|
| 081700209 | 林郁昊 | docker環境的搭建與實現 |
| 111700312 | 胡浩楠 | 樹莓派下的人臉識別 |
| 031701129 | 黃宇航 | 樹莓派的環境搭建 |
說明:所列為主要負責任務,其實各個部分大家都有一定的參與度,遇到問題一起查資料解決
協作主要以qq群溝通為主,因為大家都能夠通過內網穿透操作樹莓派
協作照片:

遇到的問題
- 在下載安裝opencv、face_recognition依賴時總是出錯
解決方案:使用xshell和xftp工具,先在windows上下載好文件,在遠端上傳到樹莓派上(還好之前把內網穿透弄好了,使得三人都能遠端登錄樹莓派
- 在運行facerec_on_raspberry_pi.py程序時,會報錯。

解決方案:因為之前用的是python2.7,有些函數沒有,換成python3就可以正常運行。為此又把face_recognition重新在py3上安裝一遍。
