How do I search for a saved fingerprint using sensor R305?
<p><strong>What am I doing?</strong>
I have GUI interface built using PyQt, which grants access to the users by validating their Fingerprint.</p>

<p>I am using Fingerprint sensor R305, for my module.</p>

<p><strong>My issue?</strong>
I have my code available on GIT, and it saves the data at a particular ID.
But it fails to search for the specified fingerprint.</p>

<p><strong>Enroll the fingerprint:</strong>
*</p>

<pre><code>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 &gt;= 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 &gt;= 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)

*
</code></pre>

<p><strong>Searching the fingerprint:</strong>
*</p>

<pre><code>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 &gt;= 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 &gt;= 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()
</code></pre>

<p>*</p>

<p><strong>THE GUI CODE:</strong>
*</p>

<pre><code>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 &gt;= 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 &gt;= 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_())
</code></pre>

<p>*</p>

<p>After going through your code, I get that in your GUI code you are using new() to generate random id values for saving fingerprint template data. But in your search code you are checking the value of r[2] in array [0,1] which is actually your currently stored id list. This is why your search program cant find your fingerprint
To generate dynamic values of id in python you can use dynamic array or looping.
Replace this with your new() in GUI code:</p>

<pre><code>    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
</code></pre>

<p>And get the value of id by calling main1():</p>

<pre><code>    id=self.main1()
</code></pre>

<p>And in your search code make the following changes:</p>

<pre><code>    if r[0]==0 and r[2] in lst:
</code></pre>

python pyqt fingerprint