From 23bc89dab6b259c8c7b8e7f0f5e2459af0bb587b Mon Sep 17 00:00:00 2001 From: tom_taylor Date: Sat, 21 Sep 2024 23:08:53 +0000 Subject: [PATCH] update to read_qrcodes.py 1) included code to read qr from four 90 degree rotations for improved robustness of read 2) exlarged the region to read qr codes from. This is a delicate balance between excluding irrelevant text that may confuse the qr code reader and cutting off pieces of the qr code that have been wobbled by the scanner 3) included code for manual entry of the filenames for student's quizzes that cannot be deciphered by the qr code reader --- read_qr/read_qrcodes.py | 54 +++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/read_qr/read_qrcodes.py b/read_qr/read_qrcodes.py index 21ea7d2..e778f81 100644 --- a/read_qr/read_qrcodes.py +++ b/read_qr/read_qrcodes.py @@ -1,5 +1,7 @@ from os import chdir, listdir import numpy as np +from time import gmtime + def regulater(binary_array) : if not (len(binary_array.shape) == 2 and set([itm for row in binary_array for itm in row]) <= set([0,255]) ): print('binary_array is not two dimensional or not binary', len(binary_array.shape), set([itm for row in binary_array for itm in row])) @@ -31,16 +33,25 @@ def read_qrcodes(pdfdoc, inventory, pagenum=0) : csvreader = csv.reader(invent, delimiter = '\t') for line in csvreader : try : - student_inventory[ line[ 4 ] ] = line[ 5 ] + serial_number = line[4] + filename = line[5] + #student_inventory[ line[0] ] = line[ 1 ] - except IndexError : - print('index error', line) - raise IndexError + except IndexError : + if len(line) == 0 : + continue + else : + print('index error csv', line) + raise IndexError + else : + student_inventory[ line[ 4 ] ] = line[ 5 ] + print('inventory', student_inventory) doc = fitz.open(pdfdoc) print('doc_length',doc.page_count) mat = fitz.Matrix(8.0,8.0) - clip = fitz.Rect( (480,5), (600,110) ) + clip = fitz.Rect( (480,5), (630,120) ) + #(650,120) qcd = cv2.QRCodeDetector() pageno = pagenum assignment = {} @@ -56,31 +67,44 @@ def read_qrcodes(pdfdoc, inventory, pagenum=0) : kernel = np.ones((3,3),dtype=np.uint8) ret,thresh = cv2.threshold(gray,200,255,cv2.THRESH_BINARY) thresh=cv2.bitwise_not(cv2.dilate(cv2.erode(cv2.bitwise_not(thresh),kernel,iterations=1),kernel,iterations=1)) - - data = qcd.detectAndDecodeMulti(thresh) - if data[0] == True and data[1][0]!='': - assignment[pageno] = data[1] + data = [] + for k in range(4) : + dat = qcd.detectAndDecodeMulti(np.rot90(thresh,k)) + if dat[0] == True : + data += [dat[1]] + if len(set(data)) == 1 and data[0]!=('',): + print(data) + assignment[pageno] = data[0] newdoc = fitz.open() newdoc.insert_pdf(doc,from_page=indx, to_page=indx, start_at=-1, rotate=-1, links=True, annots=True) - newbytes = newdoc.tobytes() + newbytes = newdoc.tobytes(garbage=4,deflate=True,deflate_images=True, deflate_fonts=True) try : newfilename = student_inventory[data[1][0]] except KeyError : print('key error', pageno) pageno += 1 continue + except IndexError : + print('index error', pageno, data) else : - with open('/home/tomtaylor/exams_/quiz2/'+student_inventory[data[1][0]], 'wb') as f: + '''it would be good to downsample this for smaller filesize''' + with open('/path/to/directory/to/store/student/quizzes/'+newfilename, 'wb') as f: f.write(newbytes) else : weirds += [pageno] + print("I can't read the qr code at page number " + str(pageno+1) +", could you enter the filename for me?") + img = Image.fromarray(thresh.astype('uint8')) + '''we want to keep an eye on any unreadable qr codes so we can keep track of what goes wrong''' + img.save('/path/to/store/weird/qrs/'+str(gmtime().tm_yday)+'_'+str(pageno)+'.jpg') + img.show() + newfilename = input('page '+str(pageno+1)+'-->') newdoc = fitz.open() newdoc.insert_pdf(doc,from_page=indx, to_page=indx, start_at=-1, rotate=-1, links=True, annots=True) newbytes = newdoc.tobytes() - with open('/home/tomtaylor/exams_/quiz2/'+str(pageno)+'.pdf', 'wb') as f: + with open('/path/to/directory/to/store/student/quizzes/' + newfilename, 'wb') as f: f.write(newbytes) - img = Image.fromarray(thresh.astype('uint8')) - img.show() + + pageno += 1 return (assignment, weirds, pageno) @@ -89,7 +113,7 @@ def read_qrcodes(pdfdoc, inventory, pagenum=0) : if __name__=='__main__' : inventory = [] outp = {} - assignment,weirds,pageno = read_qrcodes('/path/to/yourquiz_scan.pdf', '/.csv') + assignment,weirds,pageno = read_qrcodes('/path/to/scanned/quizzes/somefilename.pdf', '/path/to/inventory/file/q4_inventory_mat343.txt') print('pageno', pageno) for ky,val in assignment.items() : outp[ ky ] = val