一、 在树莓派中安装opencv库
(1)安装依赖
# 更新和升级任何现有的软件包
sudo apt-get update && sudo apt-get upgrade
# 安装开发工具CMake,帮助我们配置OpenCV构建过程
sudo apt-get install build-essential cmake pkg-config
# 图像I/O包,允许我们从磁盘加载各种图像文件格式。这种文件格式的例子包括JPEG,PNG,TIFF等
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
# 视频I/O包。这些库允许我们从磁盘读取各种视频文件格式,并直接处理视频流
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
# OpenCV库附带一个名为highgui的子模块 ,用于在我们的屏幕上显示图像并构建基本的GUI。为了编译 highgui模块,我们需要安装GTK开发库
sudo apt-get install libgtk2.0-dev libgtk-3-dev
# OpenCV中的许多操作(即矩阵操作)可以通过安装一些额外的依赖关系进一步优化
sudo apt-get install libatlas-base-dev gfortran
# 安装Python 2.7和Python 3头文件,以便我们可以用Python绑定来编译OpenCV
sudo apt-get install python2.7-dev python3-dev
(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
(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 ,添加如下,
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
- 并使之生效
source ~/.profile
- 使用Python3安装虚拟机
mkvirtualenv cv -p python3
提醒:后续所有操作均在虚拟机中
- 安装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 ..
- 增大交换空间 CONF_SWAPSIZE=1024
sudo nano /etc/dphys-swapfile #虚拟机中sudo才可以修改
- 重启swap服务并开始编译
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
make -j4
(6)安装opencv
sudo make install
sudo ldconfig
- 检查安装位置
ls -l /usr/local/lib/python3.7/site-packages
ls -l /usr/local/lib/python3.7/dist-packages
cd ~/.virtualenvs/cv/lib/python3.7/site-packages/
ln -s /usr/local/lib/python3.7/site-packages/cv2 cv2
(7)验证安装
source ~/.profile
workon cv
python
import cv2
cv2.__version__
提示:退出python虚拟机命令deactivate
二、 使用opencv和python控制树莓派的摄像头
(1)picamera模块安装
- 开启虚拟机
source ~/.profile
workon cv
再次提醒:后续所有操作均在虚拟机中
- 安装picamera
pip install "picamera[array]"
(2)在Python代码中导入OpenCV控制摄像头
- Python代码
# 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)
三、 利用树莓派的摄像头实现人脸识别
(1)安装所需库
pip install dlib
pip install face_recognition
(2)准备好需要用到的图片和代码文件
- facerec_on_raspberry_pi.py代码
# 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))
- facerec_from_webcam_faster.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.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
# Create arrays of known face encodings and their names
known_face_encodings = [
obama_face_encoding,
biden_face_encoding
]
known_face_names = [
"Barack Obama",
"Joe Biden"
]
# 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()
(3)人脸识别
四、 结合微服务的进阶任务
(1)安装Docker
- 下载安装脚本
curl -fsSL https://get.docker.com -o get-docker.sh
- 执行安装脚本(使用阿里云镜像)
sh get-docker.sh --mirror Aliyun
- 将当前用户加入docker用户组
sudo usermod -aG docker $USER
- 查看docker版本,验证是否安装成功
docker --version
(2).配置docker的镜像加速
- 写加速器地址
sudo nano /etc/docker/daemon.json
- 重启docker
service docker restart
(3)定制opencv镜像
- 拉取镜像
sudo docker pull sixsq/opencv-python
- 创建并运行容器
sudo docker run -it sixsq/opencv-python /bin/bash
- pip安装 "picamera[array]" dlib face_recognition
pip install "picamera[array]"
pip install dlib
pip install face_recognition
(face_recognition可离线安装)
sudo docker cp face_recognition_models-0.3.0-py2.py3-none-any.whl e8e0d5cfb823:/home
sudo docker cp face_recognition-1.3.0-py2.py3-none-any.whl e8e0d5cfb823:/home
- commit镜像
sudo docker commit priceless_chatelet gkd
(4)自定义镜像
- 创建文件
FROM gkd
MAINTAINER GROUP08
RUN mkdir /myapp
WORKDIR /myapp
COPY myapp .
- 生成镜像
sudo docker build -t myopencv .
- 运行代码
sudo docker run -it --device=/dev/vchiq --device=/dev/video0 --name face myopencv
python3 facerec_on_raspberry_pi.py
(5)opencv的docker容器中运行facerec_from_webcam_faster.py
- 在Windows系统中安装Xming
- 检查树莓派的ssh配置中的X11是否开启
cat /etc/ssh/sshd_config
-
启动putty
-
查看DISPLAY环境变量值
printenv
- 编写run.sh
#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
myopencv7
python3 facerec_from_webcam_faster.py
#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 facerecgui001
myopencv7
python3 facerec_from_webcam_faster.py
- 启动
sh run.sh
五、 以小组为单位,发表一篇博客,记录遇到的问题和解决方法,提供小组成员名单、分工、各自贡献以及在线协作的图片
1.困难
(1)安装依赖时报错,提示要安装的xxx依赖于xxx,但是xxx不会被安装。
解决:重新烧录一个干净的系统
(2)系统重装导致ssh密匙重复
解决:删掉密匙
ssh-keygen -R 192.168.43.167
(3)因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系
解决:用aptitude install下载,可以在下载软件包的同时选择下载相关依赖
sudo apt-get install aptitude
sudo aptitude install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo aptitude install libxvidcore-dev libx264-dev
sudo aptitude install libgtk2.0-dev libgtk-3-dev
sudo aptitude install libatlas-base-dev gfortran
sudo aptitude install python2.7-dev python3-dev
(4)编译OpenCV出错
cd /home/pi/opencv_contrib-4.1.2/modules/xfeatures2d/src
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i
(5)raw.githubusercontent.com (raw.githubusercontent.com)|0.0.0.0|:443... 失败:拒绝连接
sudo nano /etc/hosts
151.101.76.133 raw.githubusercontent.com
(6)cv2库用不了
解决:依赖没下全,少libgtk2.0-dev,重新下载,再重新编译。
(7)libgtk2.0-dev下载不了
libgtk2.0-dev : 依赖: libgtk2.0-0 (= 2.24.32-3) 但是 2.24.32-3+rpt1 已安装
libcairo2-dev : 依赖: libcairo2 (= 1.16.0-4) 但是 1.16.0-4+rpt1 已安装
依赖: libcairo-gobject2 (= 1.16.0-4) 但是 1.16.0-4+rpt1 已安装
libpng-dev : 冲突: libpng12-dev 但是 1.2.54-6 已安装
libpng12-dev : 冲突: libpng-dev 但是 1.6.36-6 将被安装
libpixman-1-dev : 依赖: libpixman-1-0 (= 0.36.0-1) 但是 0.36.0-1+rpt1 已安装
解决:降级再下载
(8)face_recognition下载太慢
2.分工
学号 | 名称 | 任务 |
---|---|---|
031702606 | 余琳玲 | 查找资料、解决问题 |
111700306 | 陈佳雯 | 查找资料、解决问题 |
031702616 | 林涛 | 实际操作、编写博客 |
3.在线协作
- 耗时:16h