目錄
(1) 在樹莓派中安裝opencv庫
(1)安裝依賴
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
偶爾有幾個卡殼的就加上--fix-missing再執行幾次
(2)下載OpenCV源碼
cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/4.1.2.zip
unzip opencv.zip
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/4.1.2.zip
unzip opencv_contrib.zip
#最好掛tz加快速度
(3)安裝pip
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo python3 get-pip.py
(4)安裝Python虛擬機
sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip
- 配置~/.profile ,添加如下,並使用
source ~/.profile
生效
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_ENV_BIN_DIR=bin
- 使用Python3安裝虛擬機
mkvirtualenv cv -p python3
- 進入虛擬機
source ~/.profile && workon cv
可以看到前面有cv虛擬機的標識
- 安裝numpy
pip install numpy
(5)編譯OpenCV
cd ~/opencv-4.1.2/
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-4.1.2/modules \
-D BUILD_EXAMPLES=ON ..
要確保路徑齊全
-
增大交換區內存到1024
-
重啟swap服務並開始編譯
sudo /etc/init.d/dphys-swapfile stop && sudo /etc/init.d/dphys-swapfile start make -j4
-
1小時+后看到編譯完成
(6)安裝opencv
sudo make install
sudo ldconfig
-
檢查安裝位置
(7)驗證安裝
-
退出python虛擬機命令
deactivate
(2) 使用opencv和python控制樹莓派的攝像頭
-
安裝picamera(虛擬機環境下)
source ~/.profile workon cv pip install "picamera[array]"
-
使用python和opencv控制攝像頭
# 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(1) # 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)
(3) 利用樹莓派的攝像頭實現人臉識別
參考1
-
安裝所需庫
pip install dlib && pip install face_recognition && pip install numpy #前面安裝過就不用了
-
同目錄下放置一張用於識別test.jpg
(1)基於picamera的人臉識別
isLeiJun.py
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("test.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 = "雷軍"
print("I see someone named {}!".format(name))
(2)基於opencv的人臉識別
recognition_opcv.py
import face_recognition
import cv2
import numpy as np
# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
# 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
# 2. Only detect faces in every other frame of video.
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)
# Load a sample picture and learn how to recognize it.
leijun_image = face_recognition.load_image_file("leijun.jpg")
leijun_face_encoding = face_recognition.face_encodings(leijun_image)[0]
# Load a second sample picture and learn how to recognize it.
liuzuohu_image = face_recognition.load_image_file("liuzuohu.jpg")
liuzuohu_face_encoding = face_recognition.face_encodings(liuzuohu_image)[0]
# Create arrays of known face encodings and their names
known_face_encodings = [
leijun_face_encoding,
liuzuohu_face_encoding
]
known_face_names = [
"LeiJun",
"LiuZuoHu"
]
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = small_frame[:, :, ::-1]
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# # If a match was found in known_face_encodings, just use the first one.
# if True in matches:
# first_match_index = matches.index(True)
# name = known_face_names[first_match_index]
# Or instead, use the known face with the smallest distance to the new face
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
process_this_frame = not process_this_frame
# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
(4) 結合微服務的進階任務
(1)使用微服務,部署opencv的docker容器(要能夠支持arm),並在opencv的docker容器中跑通(3)的示例代碼facerec_on_raspberry_pi.py
-
准備操作
#退出python虛擬機 deactivate #腳本安裝docker sudo curl -sSL https://get.docker.com | sh #填加用戶到docker組 sudo usermod -aG docker pi #重新登陸以用戶組生效 exit && ssh pi@raspiberry #驗證docker版本 docker --version
-
創建對應目錄
-
拉取arm可用的docker鏡像
docker pull sixsq/opencv-python
-
進入容器並安裝所需庫
docker run -it [imageid] /bin/bash pip install "picamera[array]" dlib face_recognition
-
comiit鏡像
docker commit [containerid] my-opencv
-
自定義鏡像
-
Dockerfile
FROM my-opencv MAINTAINER GROUP13 RUN mkdir /myapp WORKDIR /myapp ENTRYPOINT ["python3"]
-
生成鏡像
docker build -t my-opencv-test .
-
運行腳本
docker run -it --rm --name my-running-py -v ${PWD}/workdir:/myapp --device=/dev/vchiq --device=/dev/video0 my-opencv-test isLeiJun.py
-
(2)選做:在opencv的docker容器中跑通步驟(3)的示例代碼facerec_from_webcam_faster.py
采用的是在Windows上通過ssh連接樹莓派並傳送X11數據 參考1 參考2
-
環境准備
-
windows端安裝XMing
-
檢測ssh配置文件中X11是否開啟
cat /etc/ssh/sshd_config
-
支持X11轉發的ssh客戶端
由於安裝XMing的時候創建了鏈接到putty,所以此處打個勾就行
-
查看DISPLAY環境變量值
printenv
-
-
編輯啟動腳本 run.sh
xhost + #允許來自任何主機的連接 docker run -it \ --rm \ -v ${PWD}/workdir:/myapp \ --net=host \ -v $HOME/.Xauthority:/root/.Xauthority \ -e DISPLAY=:10.0 \ #此處填寫上面查看到的變量值 -e QT_X11_NO_MITSHM=1 \ --device=/dev/vchiq \ --device=/dev/video0 \ --name my-running-py \ my-opencv-test \ recognition.py
-
執行腳本
sh run.sh
效果同上,不再錄制視頻了
(5) 以小組為單位,發表一篇博客,記錄遇到的問題和解決方法,提供小組成員名單以及在線協作的圖片
(1)踩過的坑
- 原本使用windows terminal ssh連接樹莓派時找不到DISPLAY環境變量,即使使用了 ssh -X pi@raspi
- 后面換了putty,打勾一下X11轉發即可
- 期間各種下載和安裝依賴實在慢
- 直接在路由器端轉發國外的流量到代理,也不用一直換源了
- 一開始插入視頻標簽無法顯示
(2)小組成員名單
- 031702626楊世傑
- 031702625楊藍宇
- 171709012沈鴻驍
(3)在線協作圖片
- 采用的是群內分享屏幕,三位同學一起查找資料和解決困難,由楊世傑進行主要操作