2020系统综合实践 第7次实践作业 30组

一、在树莓派中安装opencv库

参考教程:关于opencv的编译安装,可以参考Adrian Rosebrock的Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi。

(1)安装依赖项

# 更新软件源,更新软件
sudo apt-get update && sudo apt-get upgrade

# Cmake等开发者工具
sudo apt-get install build-essential cmake pkg-config

# 图片I/O包
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用于显示图片的子模块需要GTK
sudo apt-get install libgtk2.0-dev libgtk-3-dev

# 性能优化包
sudo apt-get install libatlas-base-dev gfortran

# 安装 Python2.7 & Python3
sudo apt-get install python2.7-dev python3-dev

下载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

安装pip

wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo python3 get-pip.py

安装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

进入虚拟机

source ~/.profile && workon cv

安装numpy

pip install numpy

编译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

经历九九八十一难终于编译成功了 (哭

安装OpenCV

sudo make install
sudo ldconfig

检查OpenCV安装位置,并建立软链

ls -l /usr/local/lib/python3.7/site-packages/ #查看cv2
cd ~/.virtualenvs/cv/lib/python3.7/site-packages/
ln -s /usr/local/lib/python3.7/site-packages/cv2.cpython-37m-arm-linux-gnueabihf.so cv2.so #建立软链

验证安装

source ~/.profile 
workon cv
python
import cv2
cv2.__version__  #查看cv2版本

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

参考教程:还是可以参考Adrian Rosebrock的Accessing the Raspberry Pi Camera with OpenCV and Python
跑通教程的示例代码(有可能要调整里面的参数)

picamera模块安装
开启虚拟机(以下操作都在虚拟机中进行)
安装picamera

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(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)

三、利用树莓派的摄像头实现人脸识别

人脸识别有开源的python库face_recognition,这当中有很多示例代码
参考教程:树莓派实现简单的人脸识别
要求:跑通face_recognition的示例代码facerec_on_raspberry_pi.py以及facerec_from_webcam_faster.py

(1)安装所需依赖库

source ~/.profile 
workon cv 
pip install dlib
pip install face_recognition

测试安装

python
import face_recognition

(2)切换到代码和图片所在文件夹运行代码
1.将代码和图片传入树莓派

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)")
image = face_recognition.load_image_file("Yao.jpg")
face_encoding = face_recognition.face_encodings(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([face_encoding], face_encoding)
        name = "<Unknown Person>"

        if match[0]:
            name = "Yao ming"
        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.
Jay_Chou_image = face_recognition.load_image_file("jay.jpg")
Jayface_encoding = face_recognition.face_encodings(Jay_Chou_image)[0]

# Load a second sample picture and learn how to recognize it.
Messi_image = face_recognition.load_image_file("messi.jpg")
Messi_face_encoding = face_recognition.face_encodings(Messi_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    Jay_encoding,
    Messi_face_encoding
]

known_face_names = [
    "Jay",
    "Messi"
]



# 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()


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

使用微服务,部署opencv的docker容器(要能够支持arm),并在opencv的docker容器中跑通(3)的示例代码facerec_on_raspberry_pi.py
选做:在opencv的docker容器中跑通步骤(3)的示例代码facerec_from_webcam_faster.py

(1)Docker的安装和配置

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

拉取镜像(支持arm)

sudo docker pull  sixsq/opencv-python

进入容器并安装所需依赖

docker run -it [imageid] /bin/bash
pip install "picamera[array]" dlib face_recognition

commit更新容器建立新的镜像

docker commit [containerid] [自定义镜像名]

编写dockerfile文件构建镜像
dockerfile文件:

FROM yqc_cv
MAINTAINER 555
RUN mkdir /myapp
WORKDIR /myapp
COPY myapp .

构建镜像:

sudo docker build opencv2 .

构建成功后,运行容器

docker run -it --device=/dev/vchiq --device=/dev/video0 myopencv opencv2

运行facerec_on_raspberry_pi.py

python3 facerec_on_raspberry_pi.py

选做:在opencv的docker容器中跑通步骤(3)的示例代码facerec_from_webcam_faster.py
在Windows系统中安装XMing

启动putty

查看DISPLAY环境变量值

printenv

编辑并启动脚本run.sh

xhost +	#允许来自任何主机的连接
docker run -it \
        --rm \
        -v ${PWD}/workdir:/worksapce \
        --net=host \
        -v $HOME/.Xauthority:/root/.Xauthority \
        -e DISPLAY=:0.0  \	#此处填写上面查看到的变量值
        -e QT_X11_NO_MITSHM=1 \
        --device=/dev/vchiq \
        --device=/dev/video0 \
        opencv2 \
        recognition.py

执行脚本sh run.sh

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

说起来的话这次的实验真的让我没有想到会这么困难,幸亏鼠标质量好,不然...
1.问题


这个东西一直在报错,我一直不知道啥问题,就只是文件夹的位置不正确然后让我浪费了很长时间

②编译的时候缺少文件,查资料在网上找文件

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

还有很多小问题...

2.小组成员名单

学号 姓名 分工
061700232 闫佳豪 实际操作和博客撰写
031702435 张昊 提供代码及测试图片
011703120 王玥 提供代码及测试图片

3.在线协作的图片
在QQ视频协作

posted @ 2020-06-12 21:39  Yuqiancha  阅读(226)  评论(0编辑  收藏  举报