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

(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

(5)配置~/.profile,添加内容:

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
  • 使之生效
source ~/.profile

(6)使用Python3 安装虚拟机

mkvirtualenv cv -p python3

(7)进入虚拟机

source ~/.profile
workon cv
  • 可以看到前面有cv虚拟机的标识

(8)编译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 ..

  • 编译之前配置交换空间大小

在开始编译过程之前,应 增加交换空间的大小。这使OpenCV可以使用 Raspberry PI的所有四个内核进行编译,而不会由于内存问题而挂起编译。
把交换空间交换空间增大到 CONF_SWAPSIZE=1024

  • 重启swap服务
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

  • 开始编译
make -j4

编译过程中遇到许多问题也极其耗时,这里遇到的问题写在后面

(9)安装OpenCV

sudo make install
sudo ldconfig

  • 检查OpenCV的安装位置
ls -l /usr/local/lib/python3.7/site-packages/
cd ~/.virtualenvs/cv/lib/python3.7/site-packages/
ln -s /usr/local/lib/python3.7/site-packages/cv2 cv2

(10)验证安装

source ~/.profile 
workon cv
python
import cv2
cv2.__version__

2、 使用opencv和python控制树莓派的摄像头

(1)安装picreame

source ~/.profile 
workon cv 
pip install "picamera[array]"

(2)使用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)安装所需库

source ~/.profile 
workon cv 
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 由于代码过长,这里就不贴出来了
  • 该程序会显示摄像头实时拍摄的画面,如果检测到人脸会有一个方框显示,该程序能特别的识别出奥巴马和拜登的人脸。

4、结合微服务的进阶任务

(一)使用微服务,部署opencv的docker容器

(1)安装Docker

  • 下载安装脚本
#脚本安装docker
sudo curl -sSL https://get.docker.com | sh
#填加用户到docker组
sudo usermod -aG docker pi
#重新登陆以用户组生效
exit && ssh pi@raspiberry
  • 查看docker版本,验证是否安装成功
docker --version

  • 创建对应目录

(2)定制opencv镜像

  • 拉取镜像
docker pull sixsq/opencv-python

(3)创建并运行容器

docker run -it sixsq/opencv-python /bin/bash

(4)自定义镜像

  • 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

(二)选做:在opencv的docker容器中跑通步骤(3)的示例代码

环境准备

  • 开启树莓派的ssh配置中的X11
cat /etc/ssh/sshd_config

  • 查看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
  • 打开终端,运行run.sh
sh run.sh

可以正确识别人脸

5、 以小组为单位,发表一篇博客,记录遇到的问题和解决方法,提供小组成员名单以及在线协作的图片

(一)遇到的问题以及解决方法

(1)软件包有未满足的依赖关系(忘记截图了)

  • 安装sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev依赖时

错误显示如下

下列软件包有未满足的依赖关系:
libavcodec-dev : 依赖: libavcodec58 (= 7:4.1.4-1~deb10u1)
依赖: libavutil-dev (= 7:4.1.4-1~deb10u1) 但是它将不会被安装
依赖: libswresample-dev (= 7:4.1.4-1~deb10u1) 但是它将不会被安装
libavformat-dev : 依赖: libavformat58 (= 7:4.1.4-1~deb10u1) 但是 7:4.1.4-1+rpt7~deb10u1 正要被安装
依赖: libavutil-dev (= 7:4.1.4-1~deb10u1) 但是它将不会被安装
依赖: libswresample-dev (= 7:4.1.4-1~deb10u1) 但是它将不会被安装
libswscale-dev : 依赖: libavutil-dev (= 7:4.1.4-1~deb10u1) 但是它将不会被安装
依赖: libswscale5 (= 7:4.1.4-1~deb10u1) 但是 7:4.1.4-1+rpt7~deb10u1 正要被安装
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系

这里卡了许久,通过网上查到解决方法以及问同学,最后通过卸载之前下载的依赖,以及换源才得以解决

(2)fatal error: features2d/test/test_detectors_regression.impl.hpp: 没有那个文件或目录

解决方法:头文件的include路径不对,后来通过修改路径、以及下载一些本来就不存在文件导入才得以解决

  • boostdesc_bgm.i
  • boostdesc_bgm_bi.i
  • boostdesc_bgm_hd.i
  • boostdesc_lbgm.i
  • boostdesc_binboost_064.i
  • boostdesc_binboost_128.i
  • boostdesc_binboost_256.i
  • vgg_generated_120.i
  • vgg_generated_64.i
  • vgg_generated_80.i
  • vgg_generated_48.i

这些是缺少的文件、网上可以下载到相关文件

(二)小组成员名单

学号 姓名 分工
031702340 张逸杰 实机操作,查阅资料
031702331 杨锦镔 博客编写,查阅资料
031702341 黄彬煌 查阅资料,寻找解决方法

(三)在线协作图片

本次实验主要采用的是群内分享屏幕,遇到问题大家一起查找资料和解决困难

 posted on 2020-06-07 21:17  Youngmits  阅读(609)  评论(3编辑  收藏  举报