第7次實踐作業
第19小組 rm-f隊
一、在樹莓派中安裝opencv庫
opencv我在第6次實驗中安裝過了,編譯源碼的方式太慢了,這邊用pip安裝
同時糾正下我的第6次實驗博客“遇到的問題”中對安裝版本的認識,4B可以安裝opencv4,在這一次實驗遇到的問題中具體講。
首先安裝依賴
pip3 install --upgrade setuptools
pip3 install numpy Matplotlib
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
sudo apt install libqt4-test
然后安裝opencv
這樣默認安裝最新版
pip3 install opencv-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(0.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)
代碼中感光時間不夠長
即time.sleep(0.1)
處僅為0.1秒,如果拍照環境比較暗,例如下圖1,效果就不太好,建議將感光時間稍微改長一點,例如圖2,同樣環境,感光時間是2s
圖1
圖2
下面這個是第6次實驗中我已經完成過了的
通過攝像頭實時拍攝查看視頻
import cv2
cap = cv2.VideoCapture(0)
while(1):
ret, frame = cap.read()
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
三、利用樹莓派的攝像頭實現人臉識別
1.facerec_on_raspberry_pi.py
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))
代碼所在目錄下應放一張用於比對的照片,文件名obama_small.jpg
2.facerec_from_webcam_faster.py
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()
四、結合微服務的進階任務
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指令之前就不需要加sudo了
(2).配置docker的鏡像加速
具體請參考我的第1次作業博客
sudo nano /etc/docker/daemon.json
編輯完成后,restart一下docker
service docker restart
(3).定制自己的opencv鏡像
首先拉取鏡像
docker pull sixsq/opencv-python
運行這個鏡像
docker run -it sixsq/opencv-python /bin/bash
在容器中,pip安裝 "picamera[array]" dlib face_recognition
pip install "picamera[array]" dlib face_recognition
安裝成功,退出容器
然后commit
編寫Dockerfile
FROM zqzopencv
MAINTAINER ZhuQingzhang031702426
RUN mkdir /myapp
WORKDIR /myapp
COPY myapp .
build
docker build -t myopencv .
(4).運行容器執行facerec_on_raspberry_pi.py
docker run -it --device=/dev/vchiq --device=/dev/video0 --name facerec myopencv
root@38afdcc52062:/myapp# ls
biden.jpg facerec_from_webcam_faster.py facerec_on_raspberry_pi.py obama.jpg obama_small.jpg
root@38afdcc52062:/myapp# python3 facerec_on_raspberry_pi.py
如果不加--device=/dev/vchiq
參數
則會出現以下報錯
* failed to open vchiq instance
(5).附加選做:opencv的docker容器中運行facerec_from_webcam_faster.py
在Windows系統中安裝Xming
安裝過程一路默認即可
(https://sourceforge.net/projects/xming/)
檢查樹莓派的ssh配置中的X11是否開啟
cat /etc/ssh/sshd_config
putty中勾起X11選項
然后使用Putty的ssh登錄樹莓派
查看DISPLAY環境變量值
printenv
可以看到
DISPLAY=localhost:10.0
然后編寫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 \
myopencv \
python3 facerec_from_webcam_faster.py
在putty中用ssh運行
sh run.sh
同樣,也可以在vnc中運行
編寫啟動腳本
runvnc.sh
#sudo apt-get install x11-xserver-utils
xhost +
docker run -it \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e DISPLAY=$DISPLAY \
-e QT_X11_NO_MITSHM=1 \
--device=/dev/vchiq \
--device=/dev/video0 \
--name facerecguivnc \
myopencv \
python3 facerec_from_webcam_faster.py
然后運行
sh runvnc.sh
五、遇到的問題
1.關於OpenCV的版本
使用版本4的時候可能出現以下問題
Traceback (most recent call last):
File "/home/pi/Desktop/opencv.py", line 1, in <module>
import cv2
File "/home/pi/.local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: /home/pi/.local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8
在上一篇博客我的第6次實驗博客“遇到的問題”中,我的描述是4B可能和opencv4不太兼容,所以當時我回退了版本到3,解決了。
這邊更正下安裝4消除這個問題的做法
遇到這個問題需要手動加載一個庫文件
sudo nano .bashrc
添加:export LD_ sudo nano .bashrc PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1
這樣就不會報錯了。
六、在線協作
| 031702426 | 朱慶章 | 負責實際操作 |
| 031702428 | 潘海東 | 查找資料並提供了問題1的解決方案 |
| 031702405 | 陳夢雪 | 查找資料 |
主要通過qq聊天和屏幕分享協作
屏幕分享
討論大作業選題
總的來說,這次實驗難度不大,opencv我在上一次實驗中就已經配過了,基本沒有遇到難題。