当前位置:   article > 正文

毕设项目分享 基于树莓派的人脸识别系统(Python)_智能佳 基于树莓派的人脸识别系统

智能佳 基于树莓派的人脸识别系统


0 项目说明

基于树莓派的人脸识别系统(Python)

提示:适合用于课程设计或毕业设计,工作量达标,源码开放


1 简介

使用OpenCV for Python图像识别库,运行在树莓派RASPBIAN JESSIE Linux系统平台上,搭配树莓派官方摄像头模块。

2 运行要求

  1. OpenCV 2.4.9 for Python
  2. Python 2.7
  3. v4l2
  4. PyQt4

3 安装要求

sudo apt-get install build-essential cmake pkg-config python-dev libgtk2.0-dev libgtk2.0 zlib1g-dev libpng-dev libjpeg-dev libtiff-dev libjasper-dev libavcodec-dev swig unzip
  • 1

1、启用v4l2

sudo nano /etc/modules
# 增加一行记录
bcm2835-v4l2
# 重启后可以找到/dev/video0

# 编译v4l2-util
apt-get install autoconf gettext libtool libjpeg8 libjpeg8-dev
git clone git://git.linuxtv.org/v4l-utils.git
cd v4l-utils/
sudo ./bootstrap.sh
./configure
make
sudo make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2、编译OpenCV 2.4.9

wget https://jaist.dl.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip
unzip opencv-2.4.9.zip
cd opencv-2.4.9/
cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_gpu=OFF -DBUILD_opencv_ocl=OFF

# 要使OpenCV开启对v4l2的支持 cmake之后要有以下输出
# V4L/V4L2:                    Using libv4l (ver 1.13.0)

sudo make
sudo make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3、安装PyQt4

sudo apt-get install python-qt4
  • 1

4、运行

python main.py
  • 1

4 项目源码

# -*- coding: utf-8 -*-
'''

@author: imhey_000
'''
import serial
import struct

from PyQt4.QtCore import QThread
from ctypes import *
from time import sleep

#基本应答信息定义
ACK_SUCCESS = 0x00
ACK_FAIL    = 0x01
ACK_FULL    = 0x04
ACK_NO_USER = 0x05
ACK_TIMEOUT = 0x08
ACK_GO_OUT  = 0x0F

#用户信息定义
ACK_ALL_USER    = 0x00
ACK_GUEST_USER  = 0x01
ACK_NORMAL_USER = 0x02
ACK_MASTER_USER = 0x03

#设置容量 MAX = 1000
USER_MAX_CNT    = 40

#命令定义
CMD_HEAD        = 0xF5
CMD_TAIL        = 0xF5
CMD_ADD_1       = 0x01
CMD_ADD_2       = 0x02
CMD_ADD_3       = 0x03
CMD_MATCH       = 0x0C
CMD_DEL         = 0x04
CMD_DEL_ALL     = 0x05
CMD_USER_CNT    = 0x09
CMD_COM_LEV     = 0x28
CMD_LP_MODE     = 0x2C
CMD_TIMEOUT     = 0x2E

CMD_FINGER_DETECTED = 0x14    

class FingerPrint(object):
    
    gTxBuf = None
    gRsBuf = None
    device = None
    resource = ''
    rate = 0
    
    def __init__(self, resource='/dev/ttyAMA0', rate=19200):
        self.gTxBuf = (c_ubyte * 9)()
        self.gTxBuf[0] = 0x00
        self.gTxBuf[1] = 0x00
        self.gTxBuf[2] = 0x00
        self.gTxBuf[3] = 0x00
        self.gTxBuf[4] = 0x00
        self.gTxBuf[5] = 0x00
        self.gTxBuf[6] = 0x00
        self.gTxBuf[7] = 0x00
        self.gTxBuf[8] = 0x00
        
        self.gRsBuf = (c_ubyte * 9)()
        
        self.baudRate = rate
        self.resource = resource
        self.device = serial.Serial(resource, baudrate=rate)
        
    def packTxData(self):
        data = struct.pack('BBBBBBBBB', self.gTxBuf[0],
                                        self.gTxBuf[1],
                                        self.gTxBuf[2],
                                        self.gTxBuf[3],
                                        self.gTxBuf[4],
                                        self.gTxBuf[5],
                                        self.gTxBuf[6],
                                        self.gTxBuf[7],
                                        self.gTxBuf[8])
        return data
       
    def packRsData(self):
        data = struct.pack('BBBBBBBB',  self.gRsBuf[0],
                                        self.gRsBuf[1],
                                        self.gRsBuf[2],
                                        self.gRsBuf[3],
                                        self.gRsBuf[4],
                                        self.gRsBuf[5],
                                        self.gRsBuf[6],
                                        self.gRsBuf[7])
        return data
       
    def unpackRsData(self):
        out = struct.unpack('BBBBBBBB', self.gRsBuf)
        return out
        
    def txAndRsCmd(self, sCnt, rCnt, delay):
        checkSum = 0
        self.device = serial.Serial(self.resource, self.baudRate, timeout=delay)
        
        data = self.packTxData()
        
        self.device.write(struct.pack('B', CMD_HEAD))
        for i in range(0, sCnt):
            self.device.write(data[i])
            #self.unpackData(data)
            checkSum ^= self.gTxBuf[i]
        self.device.write(struct.pack('B', checkSum))
        self.device.write(struct.pack('B', CMD_TAIL))
        
        self.gRsBuf = self.device.read(8)
        
        print 'gRsBuf len:', len(self.gRsBuf)
        
        if len(self.gRsBuf) != rCnt:
            print 'time out'
            return ACK_TIMEOUT
        if self.gRsBuf[0] != struct.pack('B', CMD_HEAD):
            print 'fail type error1'
            return ACK_FAIL
        if self.gRsBuf[rCnt - 1] != struct.pack('B', CMD_TAIL):
            print 'fail type error2'
            return ACK_FAIL
        if self.gRsBuf[1] != struct.pack('B', self.gTxBuf[0]):
            print 'fail type error3'
            return ACK_FAIL
        
        #debug
        reponse = self.unpackRsData()
        for i in range(0, 8):
            print hex(reponse[i])
        
        checkSum = 0
        for i in range(1, (len(self.gRsBuf) - 1)):
            checkSum ^= struct.unpack('B', self.gRsBuf[i])[0]
        print struct.pack('B', checkSum)
        if checkSum != 0:
            print 'checksum error'
            return ACK_FAIL
            
        print 'success'
        return ACK_SUCCESS
        
    def addUser(self, userCode):
        userCount = self.getUserCount()
        if userCount >= USER_MAX_CNT:
            return ACK_FAIL
        
        print 'add 1'
        self.gTxBuf[0] = CMD_ADD_1
        self.gTxBuf[1] = 0x00
        self.gTxBuf[2] = userCode
        self.gTxBuf[3] = 0x03
        self.gTxBuf[4] = 0x00
        result = self.txAndRsCmd(5, 8, 200)
        if result == ACK_SUCCESS and struct.unpack('B', self.gRsBuf[4])[0] == ACK_SUCCESS:
            print 'add 2'
            self.gTxBuf[0] = CMD_ADD_2
            result = self.txAndRsCmd(5, 8, 200)
            if result == ACK_SUCCESS and struct.unpack('B', self.gRsBuf[4])[0] == ACK_SUCCESS:
                print 'add 3'
                self.gTxBuf[0] = CMD_ADD_3
                result = self.txAndRsCmd(5, 8, 200)
                if result == ACK_SUCCESS and struct.unpack('B', self.gRsBuf[4])[0] == ACK_SUCCESS:
                    print 'add success'
                    return ACK_SUCCESS
                else:
                    return ACK_FAIL
            else:
                return ACK_FAIL
        else:
            return ACK_FAIL
        
    def clearAllUser(self):
        self.gTxBuf[0] = CMD_DEL_ALL
        self.gTxBuf[1] = 0x00
        self.gTxBuf[2] = 0x00
        self.gTxBuf[3] = 0x00
        self.gTxBuf[4] = 0x00
        
        result = self.txAndRsCmd(5, 8, 50)
        print result
        #print struct.unpack('B', result)[0]
        #if result == ACK_SUCCESS and self.gRsBuf[4] == ACK_SUCCESS:
        if result == 0x00 and struct.unpack('B', self.gRsBuf[4])[0] == 0x00:
            print 'user all clear'
            None
            
    def isMasterUser(self, userID):
        if userID == 1 or userID == 2 or userID == 3:
            return True
        else:
            return False
        
    def verifyUser(self):
        self.gTxBuf[0] = CMD_MATCH
        self.gTxBuf[1] = 0x00
        self.gTxBuf[2] = 0x00
        self.gTxBuf[3] = 0x00
        self.gTxBuf[4] = 0x00
        
        result = self.txAndRsCmd(5, 8, 150)
        print hex(result)
        if result != ACK_TIMEOUT:
            if result == ACK_SUCCESS and self.isMasterUser(struct.unpack('B', self.gRsBuf[4])[0]):
                print 'verify success'
                return ACK_SUCCESS
            elif struct.unpack('B', self.gRsBuf[4])[0] == ACK_NO_USER:
                print 'no user'
                return ACK_NO_USER
            else:
                print 'verify fail'
                return ACK_GO_OUT
        else:
            return ACK_TIMEOUT
        
    def getUserCount(self):
        self.gTxBuf[0] = CMD_USER_CNT
        self.gTxBuf[1] = 0x00
        self.gTxBuf[2] = 0x00
        self.gTxBuf[3] = 0x00
        self.gTxBuf[4] = 0x00
        
        result = self.txAndRsCmd(5, 8, 10)
        if result == ACK_SUCCESS and struct.unpack('B', self.gRsBuf[4])[0] == ACK_SUCCESS:
            print 'user count: ',struct.unpack('B', self.gRsBuf[3])[0]
            return struct.unpack('B', self.gRsBuf[3])[0]
        else:
            return 0xFF
        
    def getCompareLevel(self):
        self.gTxBuf[0] = CMD_COM_LEV
        self.gTxBuf[1] = 0x00
        self.gTxBuf[2] = 0x00
        self.gTxBuf[3] = 0x01
        self.gTxBuf[4] = 0x00
        
        result = self.txAndRsCmd(5, 8, 10)
        if result == ACK_SUCCESS and struct.unpack('B', self.gRsBuf[4])[0] == ACK_SUCCESS:
            print 'compare level: ',struct.unpack('B', self.gRsBuf[3])[0]
            return struct.unpack('B', self.gRsBuf[3])[0]
        else:
            return 0xFF
        
    def setCompareLevel(self, tmp):
        self.gTxBuf[0] = CMD_COM_LEV
        self.gTxBuf[1] = 0x00
        self.gTxBuf[2] = tmp
        self.gTxBuf[3] = 0x00
        self.gTxBuf[4] = 0x00
        
        result = self.txAndRsCmd(5, 8, 10)
        if result == ACK_SUCCESS and struct.unpack('B', self.gRsBuf[4])[0] == ACK_SUCCESS:
            return struct.unpack('B', self.gRsBuf[3])[0]
        else:
            return 0xFF
        
    def getTimeOut(self):
        self.gTxBuf[0] = CMD_TIMEOUT
        self.gTxBuf[1] = 0x00
        self.gTxBuf[2] = 0x00
        self.gTxBuf[3] = 0x01
        self.gTxBuf[4] = 0x00
        
        result = self.txAndRsCmd(5, 8, 10)
        if result == ACK_SUCCESS and struct.unpack('B', self.gRsBuf[4])[0] == ACK_SUCCESS:
            return struct.unpack('B', self.gRsBuf[3])[0]
        else:
            return 0xFF
        
    
class FingerPrintThread(QThread):
        
    runFunction     = 1
    ADD_USER        = 0
    VERIFY          = 1
    CLEAR_ALL_USER  = 2
    SET_COM_LEV     = 3
    GET_COM_LEV     = 4
    GET_TIME_OUT    = 5
    GET_USER_CNT    = 6
    
    callBack = None
    resource = ''
    rate = 0
    
    def __init__(self, resource='/dev/ttyAMA0', rate=19200):
        super(FingerPrintThread, self).__init__()
        
        self.resource = resource
        self.rate = rate
        self.fingerPrint = FingerPrint(resource=self.resource, rate=self.rate)
    
        self.result = None
    
    def setFunction(self, runFunction):
        self.runFunction = runFunction
    
    def run(self):
        
        if self.runFunction == self.ADD_USER:
            self.result = self.fingerPrint.addUser(self.userCode)
        elif self.runFunction == self.VERIFY:
            self.result = self.fingerPrint.verifyUser()
        elif self.runFunction == self.CLEAR_ALL_USER:
            self.result = self.fingerPrint.clearAllUser()
        elif self.runFunction == self.SET_COM_LEV:
            self.result = self.fingerPrint.setCompareLevel(self.tmp)
        elif self.runFunction == self.GET_COM_LEV:
            self.result = self.fingerPrint.getCompareLevel()
        elif self.runFunction == self.GET_TIME_OUT:
            self.result = self.fingerPrint.getTimeOut()
        elif self.runFunction == self.GET_USER_CNT:
            self.result = self.fingerPrint.getUserCount()
        
        
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319

5 最后

项目分享 :
https://gitee.com/asoonis/htw

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/495003
推荐阅读
相关标签
  

闽ICP备14008679号