如何使用传感器 R305 搜索保存的指纹?

How do I search for a saved fingerprint using sensor R305?

我在做什么? 我有使用 PyQt 构建的 GUI 界面,它通过验证用户的指纹来授予用户访问权限。

我正在为我的模块使用指纹传感器 R305。

我的问题? 我的代码在 GIT 上可用,它以特定 ID 保存数据。 但是搜索指定指纹失败

录入指纹: *

ser = serial.Serial(port='COM4', baudrate=57600)
pack = [0xef01, 0xffffffff, 0x1]

def printx(l):
    for i in l:
        print (i)
    print ('')

def readPacket():
    time.sleep(1)
    w = ser.inWaiting()
    ret = []
    if w >= 9:
        s = ser.read(9) #partial read to get length
        ret.extend(struct.unpack('!HIBH', s))
        ln = ret[-1]

        time.sleep(1)
        w = ser.inWaiting()
        if w >= ln:
            s = ser.read(ln)
            form = '!' + 'B' * (ln - 2) + 'H'
            ret.extend(struct.unpack(form, s))
    return ret


def writePacket(data):
    pack2 = pack + [(len(data) + 2)]
    a = sum(pack2[-2:] + data)
    pack_str = '!HIBH' + 'B' * len(data) + 'H'
    l = pack2 + data + [a]
    s = struct.pack(pack_str, *l)
    ser.write(s)


def verifyFinger():
    data = [0x13, 0x0, 0, 0, 0]
    writePacket(data)
    s = readPacket()
    return s[4]

def genImg():
    data = [0x1]
    writePacket(data)
    s = readPacket()
    return s[4] 

def img2Tz(buf):
    data = [0x2, buf]
    writePacket(data)
    s = readPacket()
    return s[4]

def regModel():
    data = [0x5]
    writePacket(data)
    s = readPacket()
    return s[4]

def store(id):
    data = [0x6, 0x1, 0x0, id]
    writePacket(data)
    s = readPacket()
    return s[4] 


if verifyFinger():
    print ('Verification Error')
    sys.exit(0)

print ('Put finger')
sys.stdout.flush()

time.sleep(1)   
while genImg():
    time.sleep(0.1)

    print ('.')
    sys.stdout.flush()

print ('')
sys.stdout.flush()

if img2Tz(1):
    print ('Conversion Error')
    sys.exit(0)

print ('Put finger again')
sys.stdout.flush()

time.sleep(1)   
while genImg():
    time.sleep(0.1)
    print ('.')
    sys.stdout.flush()

print ('')
sys.stdout.flush()

if img2Tz(2):
    print ('Conversion Error')
    sys.exit(0)

if regModel():
    print ('Template Error')
    sys.exit(0)
id = 1
if store(id):
    print ('Store Error')
    sys.exit(0) 

print ("Enrolled successfully at id %d"%id)

*

正在搜索指纹: *

ser = serial.Serial('COM4',57600)
pack = [0xef01, 0xffffffff, 0x1]

def printx():
    for i in l:
        print(i)
    print( '')

def readPacket():
    time.sleep(1)
    w = ser.inWaiting()
    ret = []
    if w >= 9:
        s = ser.read(9) #partial read to get length
        ret.extend(struct.unpack('!HIBH', s))
        ln = ret[-1]

        time.sleep(1)
        w = ser.inWaiting()
        if w >= ln:
            s = ser.read(ln)
            form = '!' + 'B' * (ln - 2) + 'H'
            ret.extend(struct.unpack(form, s))
    return ret


def writePacket(data):
    pack2 = pack + [(len(data) + 2)]
    a = sum(pack2[-2:] + data)
    pack_str = '!HIBH' + 'B' * len(data) + 'H'
    l = pack2 + data + [a]
    s = struct.pack(pack_str, *l)
    ser.write(s)


def verifyFinger():
    data = [0x13, 0x0, 0, 0, 0]
    writePacket(data)
    s = readPacket()
    return s[4]

def genImg():
    data = [0x1]
    writePacket(data)
    s = readPacket()
    return s[4] 

def img2Tz(buf):
    data = [0x2, buf]
    writePacket(data)
    s = readPacket()
    return s[4]

def regModel():
    data = [0x5]
    writePacket(data)
    s = readPacket()
    return s[4]

def search():
    data = [0x4, 0x1, 0x0, 0x0, 0x0, 0x5]
    writePacket(data)
    s = readPacket()
    return s[4:-1]  

def mainfuncn():
    if verifyFinger():
        print( 'Verification Error')
        sys.exit(-1)

    print('Put finger')
    sys.stdout.flush()

    time.sleep(1)   
    for _ in range(5):
        g = genImg()
        if g == 0:
            break
        #time.sleep(1)

        print( '.')
        sys.stdout.flush()

    print( '')
    sys.stdout.flush()
    if g != 0:
        sys.exit(-1)

    if img2Tz(1):
        print('Conversion Error')
        sys.exit(-1)

    r = search()
    print('Search result', r)
    if r[0] == 0 and r[2] in [0,1]:
        print('Successful')
        sys.exit(0)
    else:
        print('Unsuccessful')
    sys.exit(1)

mainfuncn()

*

GUI 代码: *

from PyQt4 import QtCore, QtGui
import serial, time, datetime, struct
import sys
import random
newarray=[]
distarray=[]
data1=""
m=""
k=0
ser = serial.Serial('COM4',57600)
pack = [0xef01, 0xffffffff, 0x1]


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.verticalLayoutWidget = QtGui.QWidget(Dialog)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 381, 281))
        self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))

        self.pushButton = QtGui.QPushButton(self.verticalLayoutWidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.verticalLayout.addWidget(self.pushButton)
        self.pushButton.clicked.connect(self.finger_call_main)

        self.pushButton1 = QtGui.QPushButton(self.verticalLayoutWidget)
        self.pushButton1.setObjectName(_fromUtf8("pushButton1"))
        self.verticalLayout.addWidget(self.pushButton1)
        self.pushButton1.clicked.connect(self.mainfuncn)

        self.lineEdit = QtGui.QLineEdit(self.verticalLayoutWidget)
        self.lineEdit.setText(_fromUtf8(""))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.verticalLayout.addWidget(self.lineEdit)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def printx(self,l):
        for i in l:
            print (i)
        print ('')

    def readpacket(self):
        time.sleep(1)
        w = ser.inWaiting()
        ret = []
        if w >= 9:
            s = ser.read(9) #partial read to get length
            ret.extend(struct.unpack('!HIBH', s))
            ln = ret[-1]

            time.sleep(1)
            w = ser.inWaiting()
            if w >= ln:
                s = ser.read(ln)
                form = '!' + 'B' * (ln - 2) + 'H'
                ret.extend(struct.unpack(form, s))
        return ret


    def writePacket(self,data):
        pack2 = pack + [(len(data) + 2)]
        a = sum(pack2[-2:] + data)
        pack_str = '!HIBH' + 'B' * len(data) + 'H'
        l = pack2 + data + [a]
        s = struct.pack(pack_str, *l)
        #print("Writepacket S value: " +s)
        ser.write(s)


    def verifyFinger(self):
        data = [0x13, 0x0, 0, 0, 0]
        self.writePacket(data)
        s = self.readpacket()
        return s[4]

    def genImg(self):
        data = [0x1]
        self.writePacket(data)
        s = self.readpacket()
        return s[4] 

    def img2Tz(self,buf):
        data = [0x2, buf]
        self.writePacket(data)
        s = self.readpacket()
        return s[4]

    def regModel(self):
         data = [0x5]
         self.writePacket(data)
         s = self.readpacket()
         return s[4]

    def store(self,id):
        data = [0x6, 0x1, 0x0, id]
        self.writePacket(data)
        s = self.readpacket()
        return s[4]

    def new(self):

        m=random.randrange(1,250)
        if m in distarray:
            print("hii")
            # newarray.append(m)
            self.new()

        else:
            print("else")
            distarray.append(m)
        id1=m
        print(id1)
        return id1
        #for x in distarray:
            #print(x)

    def search(self):
        data = [0x4, 0x1, 0x0, 0x0, 0x0, 0x5]
        self.writePacket(data)
        s = self.readpacket()
        return s[4:-1]  



    def finger_call_main(self):


        if self.verifyFinger():
            print ('Verification Error1')
            sys.exit(0)

        print ('Put finger')
        sys.stdout.flush()

        time.sleep(1)   
        while self.genImg():
            time.sleep(0.1)

            print ('.')
            sys.stdout.flush()

        print ('')
        sys.stdout.flush()

        if self.img2Tz(1):
            print ('Conversion Error')
            sys.exit(0)

        print ('Put finger again')
        sys.stdout.flush()

        time.sleep(1)   
        while self.genImg():
            time.sleep(0.1)
            print ('.')
            sys.stdout.flush()

        print ('')
        sys.stdout.flush()

        if self.img2Tz(2):
            print ('Conversion Error')
            sys.exit(0)

        if self.regModel():
            print ('Template Error')
            sys.exit(0)

        id=self.new()

        if self.store(id):
            print ('Store Error')
            sys.exit(0) 
        self.lineEdit.setText("Enrolled successfully at id %d"%id)

        print ("Enrolled successfully at id %d"%id)

    def mainfuncn(self):
        if self.verifyFinger():
            print( 'Verification Error')
            sys.exit(-1)

        print('Put finger')
        sys.stdout.flush()

        time.sleep(1)   
        for _ in range(5):
            g = self.genImg()
            if g == 0:
                break
            #time.sleep(1)

            print( '.')
            sys.stdout.flush()

        print( '')
        sys.stdout.flush()
        if g != 0:
            sys.exit(-1)

        if self.img2Tz(1):
            print('Conversion Error')
            sys.exit(-1)

        r = self.search()
        #print('Search result', r)
        if r[0] == 0:
            print('Successful')
            sys.exit(0)
        else:
            print('Unsuccessful')
        sys.exit(1)






    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.pushButton.setText(_translate("Dialog", "PushButton", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

*

通过您的代码后,我在您的 GUI 代码中了解到您正在使用 new() 生成随机 ID 值以保存指纹模板数据。但是在您的搜索代码中,您正在检查数组 [0,1] 中 r[2] 的值,这实际上是您当前存储的 id 列表。这就是为什么你的搜索程序找不到你的指纹 要在 python 中生成 id 的动态值,您可以使用动态数组或循环。 用 GUI 代码中的 new() 替换它:

    lst=[]
    def new(self):
            print("new")

    def main1(self):
            for z in range(0,256):
               if z in lst: 
                 z=z+1
                 self.new()
               else:
                  lst.append(z)
                  a=z
                  z=z+1
                  break
           return a

并调用main1()得到id的值:

    id=self.main1()

并在您的搜索代码中进行以下更改:

    if r[0]==0 and r[2] in lst: