关于树莓派安装OpenCV虚拟环境进行人脸识别笔记

0.序言

0.这只是一个笔记,记录一下过程踩的坑。

1.网上有说conda 安装opencv很完美的,我进过尝试发现树莓派安装conda后conda换国内清华源后发现不能下载opencv。

2.在安装opencv之前一定得先安装依赖包,关于依赖包版本过高无法安装,方法一是采用逐个安装所需降级的方法一个个重新安装依赖包;方法二是用aptitude代替 apt-get来安装,会问你是否保留原来版本输入N,后面两个输入Y。

3.重要说明:这是笔记,不是教程,因为是边做边写的,而且有几次忘记保存了,所以有很多衔接比较乱。仅供参考。

1.扩展树莓派

sudo raspi-config

选择Advanced Options->A1 Expand Filesystem  ->回车->ok ->Finish

然后会让你重新启动树莓派选择yes,如果没有就命令重启

 sudo reboot

再多释放点空间(删除Wolfram引擎和LibreOffice)

sudo apt-get purge wolfram-engine
sudo apt-get purge libreoffice*
sudo apt-get clean
sudo apt-get autoremove

2.安装依赖关系

sudo raspi-config   # 进入后打开摄像头、SSH (Interfacing Options进入  选择Camera和SSH   enable)
sudo apt-get update && sudo apt-get upgrade  (此处时间很长,你可以干点其他的事)

——————出现错误分界线——————

ps:我更新时出现如下错误:

Errors were encountered while processing:
 mysql-server-5.5
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

解决方法:

删除 /var/lib/mysql/etc/mysql两个文件夹

sudo rm /var/lib/mysql/ -R
sudo rm /etc/mysql/ -R

卸载mysql数据库和apparmor

sudo apt-get autoremove mysql* --purge
sudo apt-get remove apparmor

重新安装mysql

sudo apt-get install mysql-server mysql-common

然后输入你数据库的账号和密码

——————之后重新出错步骤再安装——————

如果你是刚开始弄树莓派,建议先换个国内源更新会比较好。

ps:我自己重新安装过一个树莓派系统,但是用windows电脑自带的远程控制进入不了,可以先把sd用读卡器插在电脑上,然后添加文本文件,改名为SSH(此处扩展名去掉。比如:新建文本.txt 改成 SSH)

安装一些开发工具,包括CMake,它可以帮助我们配置OpenCV构建过程

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

安装GTK开发库

sudo apt-get install libgtk2.0-dev libgtk-3-dev

=========================================================================================

此处也忘保存了

大概就是我重装系统之后发现少安装很多东西,aptitude重复让它解决方案太多卡死也无法解决GTK安装

解决方法:

1.用中科大的源 

修改/etc/apt/sources.list    

添加deb http://mirrors.ustc.edu.cn/raspbian/raspbian/ jessie main contrib non-free rpi

修改/etc/apt/sources.list.d/raspi.list

添加deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ jessie main ui

其余备注掉

2.aptitude多尝试几次,多让他找些方案

=========================================================================================

安装一些额外的依赖关系进一步优化

sudo apt-get install libatlas-base-dev gfortran

安装Python 2.7和Python 3头文件,以便我们可以用Python绑定来编译OpenCV

sudo apt-get install python2.7-dev python3-dev

3.下载OpenCV源代码

cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip
unzip opencv.zip

获取opencv_contrib存储库

wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
unzip opencv_contrib.zip

注意:确保您的 opencv和 opencv_contrib版本相同

4.Python2.7或Python 3.X安装

安装 Python包管理器pip

wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py# python2.7
sudo python3 get-pip.py# python3.x

virtualenv和 virtualenvwrapper安装

sudo pip install virtualenv virtualenvwrapper

#上面为python2.7命令  如果为python3.x版本,请在pip后面加个3

sudo rm -rf ~/.cache/pip

配置文件更新

方法一:

echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.profile
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.profile
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.profile
source ~/.profile

方法二:

或者直接添加

nano .profile#在文件最下面添加如下:

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

#复制完上面后  Ctrl+x  ->  y  回车
#返回输入命令更新
source ~/.profile

——————出现错误分界线——————

此处更新我出现错误如下:

Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/home/pi/miniconda3/bin/python and that PATH is
set properly.

错误原因:树莓派安装了2.7和3.x两个版本的python,关于查看树莓派python版本命令输入python查看,再输入exit()退出,我的是python3.4所以应该在安装时使用的是sudo pip3 install virtualenvwrapper(回顾上面的注释),重新上面步骤就行。

——————重新安装错的步骤——————

5.创建你的Python虚拟环境

创建我们将用于计算机视觉开发的Python虚拟环境

#按照你的版本选一个
mkvirtualenv cv -p python2
mkvirtualenv cv -p python3

检查您是否在“cv”虚拟环境中

source ~/.profile
workon cv

输入命令前面出现(cv)成功

在 cv虚拟环境中安装Python依赖的NumPy,一个用于数值处理的Python包

pip install numpy

6.编译并安装OpenCV

确保在cv虚拟环境中,没有使用workon cv进入

使用CMake来设置我们的版本

cd ~/opencv-3.3.0/
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-3.3.0/modules \
    -D BUILD_EXAMPLES=ON ..

树莓派记得一定要散热

(此处笔记失误忘记保存了,就简略写了)

通常四核编译会卡死,网上推荐单核编译,网上看到交换空间的方法来进行四核编译

记住安装完成后交换空间得还原

打开你的 sudo  nano /etc/dphys-swapfile然后编辑 CONF_SWAPSIZE变量

#set size to absolute value, leaving empty (default) then uses computed value
#you most likely don't want this, unless you have an special disk situation
#CONF_SWAPSIZE=100  #备注次行
CONF_SWAPSIZE=1024  #添加次行

Ctrl+x保存退出后,激活新的交换空间

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

开始四核编译

make -j4

在你的树莓派上安装OpenCV

sudo make install
sudo ldconfig

=========================未保存的错误及解决方法================

ls -l /usr/local/lib/python3.4/site-packages/

total 1852

此处验证我没问题,绑定时出现错误,原因是cv2.so以前绑定过导致软连接失败

解决方法删除软连接再重新绑定

==========================简略说明===========================

ls命令验证

ls -l /usr/local/lib/python3.4/site-packages/

total 不是0 就行

此处python后面取决于你的版本,当然你可以cd进去看看,或者直接输入python查看版本

我有python2.7和python3.4两个版本,本来ban绑在python3.4,误操作cv2.so与python2.7软连接了,所有此后需要用到cv2的程序我都使用python2.7执行

OpenCV绑定对应到Python XX 的 cv虚拟环境中

cd ~/.virtualenvs/cv/lib/python3.4/site-packages/#python XX,你的python版本
ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so#python XX,你的python版本

验证成功事例

source ~/.profile 
workon cv
python
>>> import cv2
>>> cv2.__version__
'3.3.0'
>>>

完成安装后交换的大小改回来

7.测试摄像头

新建.py文件

sudo nano simpleCamTest.py

输入以下程序

import numpy as np
import cv2
 
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
  
while(True):
    ret, frame = cap.read()
    frame = cv2.flip(frame, -1) # Flip camera vertically
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     
    cv2.imshow('frame', frame)
    cv2.imshow('gray', gray)
     
    k = cv2.waitKey(30) & 0xff
    if k == 27: # press 'ESC' to quit
        break
 
cap.release()
cv2.destroyAllWindows()

================================================================================

此处putty就不够用了,如果你没有HDMI连接树莓派的显示屏,推荐使用VNC开启图形界面

 raspi-config ->Interfacing Options -> VNC -> Yes

然后电脑下载vnc   网站  Download VNC Viewer | VNC® Connect

此处出现错误

X.Org X Server 1.18.4
Release Date: 2016-07-19
X Protocol Version 11, Revision 0
Build Operating System: Linux 4.4.26-v7+ armv7l Raspbian
Current Operating System: Linux raspberrypi 4.9.35-v7+ #1014 SMP Fri Jun 30 14:47:43 BST 2017 armv7l
Kernel command line: 8250.nr_uarts=0 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2708_fb.fbswap=1 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=ttyS0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
Build Date: 11 November 2016  11:59:59AM
xorg-server 2:1.18.4-2+rpi1 (https://www.debian.org/support)
Current version of pixman: 0.33.3
        Before reporting problems, check http://wiki.x.org
        to make sure that you have the latest version.
Markers: (--) probed, (**) from config file, (==) default setting,
        (++) from command line, (!!) notice, (II) informational,
        (WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/home/pi/.local/share/xorg/Xorg.0.log", Time: Mon Mar 11 02:21:01 2019
(==) Using system config directory "/usr/share/X11/xorg.conf.d"
(EE)
Fatal server error:
(EE) parse_vt_settings: Cannot open /dev/tty0 (Permission denied)
(EE)
(EE)
Please consult the The X.Org Foundation support
         at http://wiki.x.org
 for help.
(EE) Please also check the log file at "/home/pi/.local/share/xorg/Xorg.0.log" for additional information.
(EE)
(EE) Server terminated with error (1). Closing log file.
xinit: giving up
xinit: unable to connect to X server: Connection refused
xinit: server error
Couldn't get a file descriptor referring to the console

解决方法:startx 命令前面加sudo

出现错误


X.Org X Server 1.18.4
Release Date: 2016-07-19
X Protocol Version 11, Revision 0
Build Operating System: Linux 4.4.26-v7+ armv7l Raspbian
Current Operating System: Linux raspberrypi 4.9.35-v7+ #1014 SMP Fri Jun 30 14:47:43 BST 2017 armv7l
Kernel command line: 8250.nr_uarts=0 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2708_fb.fbswap=1 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=ttyS0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
Build Date: 11 November 2016  11:59:59AM
xorg-server 2:1.18.4-2+rpi1 (https://www.debian.org/support)
Current version of pixman: 0.33.3
        Before reporting problems, check http://wiki.x.org
        to make sure that you have the latest version.
Markers: (--) probed, (**) from config file, (==) default setting,
        (++) from command line, (!!) notice, (II) informational,
        (WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/var/log/Xorg.0.log", Time: Mon Mar 11 02:25:18 2019
(==) Using system config directory "/usr/share/X11/xorg.conf.d"
modprobe: FATAL: Module g2d_23 not found.
^Cxinit: connection to X server lost

waiting for X server to shut down (II) Server terminated successfully (0). Closing log file.

xinit: unexpected signal 2

解决方法:这是用putty输入的肯定无效

换用vnc

问题:vnc一直黑屏

解决方法:

sudo apt-get install tightvncserver

vncserver -geometry 1024x768

输入密码:pi123456  验证密码:pi123456  (不能纯数字,不能超8位,不能太短)

是否要访客:n

vnc输入的树莓派地址不对,应该为192.168.2.101::5901(树莓派IP两个:加5901)

或者192.168.2.101:1(树莓派IP一个:加1)

问题:最终vnc能显示命令符了,但却没有图形界面

解决方法:

sudo raspi-config 第五个Interfacing Options-> VNC 开启

错误的输入了tightvncserver指令(网上教程看的,应该是老版指令),而正确的开启VNC指令是(只限于树莓派3B、3B+)

vncserver

输入后你会看到最后一句  New desktop is raspberrypi:3 (192.168.2.101:3)

用此处的IP进行VNC连接

然后我进入了Raspberry Pi Edition - Virtual Mode。树莓PI版-虚拟模式(什么鬼?我好想吐槽,这个图像界面好难操作,这是图像界面吗?)求大神解答

最终选择重新安装树莓派系统

Operating system images – Raspberry Pi 下载 Raspbian Stretch with desktop

关机取出sd卡,使用SDFormatter格式化sd卡,Win32DiskImager写入树莓派系统

新建SSH文件,每次重启树莓派都要新建一次

用网线连接树莓派与路由器,电脑putty进入,sudo raspi-config->Interfacing Options->vnc开启(SSH开启)

输入命令vncserver,电脑vnc连接,进入图形界面,从图像界面开始重新操作。

===============================================================================

===============================================================================

问题:workon cv出现/usr/bin/python: No module named virtualenvwrapper问题,虽然有(cv)

解决方法:

重新添加python有关的环境变量:

nano ~/.profile命令进入在最下面一行上面加上下面两句话

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv

===============================================================================

如果程序在图形界面无法执行,请确保你是否开启了摄像头! raspi-config第五个Cramer

esc退出程序

8.人脸检测

准备人脸训练的图片

新建目录  mkdir dataset用它来储存人脸样本

新建nano 01_face_training.py

输入如下代码:

import cv2
import os
 
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video width
cam.set(4, 480) # set video height
 
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
 
# For each person, enter one numeric face id
face_id = input('\n enter user id end press <return> ==>  ')
 
print("\n [INFO] Initializing face capture. Look the camera and wait ...")
# Initialize individual sampling face count
count = 0
 
while(True):
    ret, img = cam.read()
    img = cv2.flip(img, -1) # flip video image vertically
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_detector.detectMultiScale(gray, 1.3, 5)
 
    for (x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)    
        count += 1
 
        # Save the captured image into the datasets folder
        cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])
 
        cv2.imshow('image', img)
 
    k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting video
    if k == 27:
        break
    elif count >= 30: # Take 30 face sample and stop video
         break
 
# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()

输入用户 ID(整数),就能在dataset/ 目录下得到用户 ID(整数)的样本。(每一个 ID 捕捉 30 个样本)

可加一个判断

在ret, img = cam.read() 下面加
    if ret == True:

下面所有程序tab ,放入此判断内

================================================================================================

问题如下:

 [INFO] Initializing face capture. Look the camera and wait ...
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp, line 10638
Traceback (most recent call last):
  File "01_face_dataset.py", line 20, in <module>
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor

解决方法:树莓派关机重新拔插下摄像头,先安装好摄像头,再启动树莓派

ps:程序读取视频太慢卡顿,是树莓派芯片太烫导致,没散热情况下,注意关机让树莓派散热。

================================================================================================

准备训练

创建子目录以储存训练数据  mkdir trainer

新建nano 02_face_training.py

输入如下代码:

import numpy as np
from PIL import Image
import cv2
import os
 
# Path for face image database
path = 'dataset'
 
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
 
# function to get the images and label data
def getImagesAndLabels(path):
    imagePaths = [os.path.join(path,f) for f in os.listdir(path)]    
    faceSamples=[]
    ids = []
    for imagePath in imagePaths:
        PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
        img_numpy = np.array(PIL_img,'uint8')
        id = int(os.path.split(imagePath)[-1].split(".")[1])
        faces = detector.detectMultiScale(img_numpy)
        for (x,y,w,h) in faces:
            faceSamples.append(img_numpy[y:y+h,x:x+w])
            ids.append(id)
    return faceSamples,ids
 
print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces,ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
 
# Save the model into trainer/trainer.yml
recognizer.write('trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi
 
# Print the numer of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))

确定在 Rpi 中已经安装了 PIL 库,如果没有的话,在终端运行以下命令:

pip install pillow

安装  pip install opencv-contrib-python

识别器

新建nano 03_face_training.py

#-*- coding:utf-8 –*-
import cv2
import numpy as np
import os
 
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
 
font = cv2.FONT_HERSHEY_SIMPLEX
 
#iniciate id counter
id = 0
 
# names related to ids: example ==> 你需要该的姓名: id=1,  etc  注意不能为中文
names = ['ID=0的名字', 'ID=1的名字', 'ID=2的名字', 'ID=3的名字', 'ID=4的名字', 'ID=5的名字']//
 
# Initialize and start realtime video capture
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video widht
cam.set(4, 480) # set video height
 
# Define min window size to be recognized as a face
minW = 0.1*cam.get(3)
minH = 0.1*cam.get(4)
 
while True:
    ret, img =cam.read()
    img = cv2.flip(img, -1) # Flip vertically
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
     
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor = 1.2,
        minNeighbors = 5,
        minSize = (int(minW), int(minH)),
       )
 
    for(x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
        id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
 
        # Check if confidence is less them 100 ==> "0" is perfect match
        if (confidence < 100):
            id = names[id]
            confidence = "  {0}%".format(round(100 - confidence))
        else:
            id = "unknown"
            confidence = "  {0}%".format(round(100 - confidence))
         
        cv2.putText(img, str(id), (x+5,y-5), font, 1, (255,255,255), 2)
        cv2.putText(img, str(confidence), (x+5,y+h-5), font, 1, (255,255,0), 1) 
     
    cv2.imshow('camera',img)
 
    k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
    if k == 27:
        break
 
# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()

这里我们包含了一个新数组,因此我们将会展示“名称”,而不是编号的 id:

names = ['None', 'Marcelo', 'Paula', 'Ilza', 'Z', 'W']

所以,如上所示的列表,Marcelo 的 ID 或索引为 1,Paula 的 ID 等于 2

==================================================

关于中文输出乱码(树莓派使用中文)

sudo apt-get install ttf-wqy-zenhei

Menu->Preferences->Raspberry Pi Configuraction->Localisaction->Set Locale 将系统语言设置成中文。

language:zh(chinese)

character set :UTF-8

==================================================

问题:vnc无法完全显示,vnc分辨率调节

解决方法:sudo raspi-config->第7个advanced options->第5个 resolution  选择大点的分辨率确认重启

==================================================

关于视频显示中文暂时还没解决,因为opencv库不支持中文。

==================================================

结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值