4B
裸板SD
卡TypeC
电源CSI
摄像头(没有/dev/video0的解决办法参见此解决办法)Rasbian Buster OS
(Raspbian Buster with desktop and recommended software
,必须新版)VNC
远程连接软件(Windows自带的远程桌面连接
比起这个很Low,强推这个,可以直接显示远程主机的视频)Thonny Python IDE
(系统会自带)PCManFM
(文件管理器,启动树莓派后,发现文件管理打不开,更新安装此软件即可)Buster
版本)PIP
源运行raspi-config
,在终端命令行中输入:
$ sudo raspi-config
$ sudo reboot
查看扩充后的分区
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 15G 4.3G 9.5G 32% /
devtmpfs 404M 0 404M 0% /dev
tmpfs 437M 0 437M 0% /dev/shm
tmpfs 437M 6.0M 431M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 437M 0 437M 0% /sys/fs/cgroup
/dev/mmcblk0p1 253M 52M 201M 21% /boot
tmpfs 88M 4.0K 88M 1% /run/user/1000
OK,成功后进去下一步。
$ sudo apt-get update
$ sudo apt-get upgrade
用于配置OpenCV构建过程
$ sudo apt-get install build-essential cmake pkg-config
用于加载各种格式的图片
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng-dev
相应地用于加载各种格式的视频
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev
用于编译OpenCV 子模块highgui
$ sudo apt-get install libfontconfig1-dev libcairo2-dev
$ sudo apt-get install libgdk-pixbuf2.0-dev libpango1.0-dev
$ sudo apt-get install libgtk2.0-dev libgtk-3-dev
用于优化矩阵操作
$ sudo apt-get install libatlas-base-dev gfortran
Python 3
头文件用于使用绑定的Python编译OpenCV
$ sudo apt-get install python3-dev
成功后进入下一步。
virtualenvwrapper
$ sudo pip install virtualenv virtualenvwrapper
在文件后面追加
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
$ source ~/.bashrc
取名字为cv
$ mkvirtualenv cv -p python3
在虚拟环境下安装OpenCV4
(cv)$ pip install opencv-contrib-python==4.1.0.25
$ python
>>> import cv2
>>> cv2.__version__
'4.1.0'
>>>
import cv2
#要更改自己的文件绝对路径
face_cascade = cv2.CascadeClassifier('/home/pi/Desktop/opencv-master/data/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/pi/Desktop/opencv-master/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml')
#加载CSI摄像头,通过libv4l2
cap = cv2.VideoCapture(0)
#OpenCV版本测试
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
while cap.isOpened():
_, img = cap.read()
#计算摄像头的FPS
if int(major_ver) < 3 :
fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
else :
fps = cap.get(cv2.CAP_PROP_FPS)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y , w ,h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0 , 0), 3)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey ,ew, eh) in eyes:
cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0, 255, 0), 5)
font = cv2.FONT_HERSHEY_SIMPLEX
text = 'FPS: '+ str(fps)
#将FPS的值展现到视频帧中
img = cv2.putText(img, text,(10,50), font, 1,
(0,255,255), 2, cv2.LINE_AA)
# Display the output
cv2.imshow('img', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
在虚拟环境中跑一下代码
(cv) pi@raspberrypi:cd code
(cv) pi@raspberrypi:~/code $ python eye_detection.py