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
This commit is contained in:
parent
ea2a8c7d1a
commit
23bc89dab6
@ -1,5 +1,7 @@
|
|||||||
from os import chdir, listdir
|
from os import chdir, listdir
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from time import gmtime
|
||||||
|
|
||||||
def regulater(binary_array) :
|
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]) ):
|
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]))
|
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')
|
csvreader = csv.reader(invent, delimiter = '\t')
|
||||||
for line in csvreader :
|
for line in csvreader :
|
||||||
try :
|
try :
|
||||||
student_inventory[ line[ 4 ] ] = line[ 5 ]
|
serial_number = line[4]
|
||||||
|
filename = line[5]
|
||||||
|
|
||||||
#student_inventory[ line[0] ] = line[ 1 ]
|
#student_inventory[ line[0] ] = line[ 1 ]
|
||||||
except IndexError :
|
except IndexError :
|
||||||
print('index error', line)
|
if len(line) == 0 :
|
||||||
raise IndexError
|
continue
|
||||||
|
else :
|
||||||
|
print('index error csv', line)
|
||||||
|
raise IndexError
|
||||||
|
else :
|
||||||
|
student_inventory[ line[ 4 ] ] = line[ 5 ]
|
||||||
|
|
||||||
print('inventory', student_inventory)
|
print('inventory', student_inventory)
|
||||||
doc = fitz.open(pdfdoc)
|
doc = fitz.open(pdfdoc)
|
||||||
print('doc_length',doc.page_count)
|
print('doc_length',doc.page_count)
|
||||||
mat = fitz.Matrix(8.0,8.0)
|
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()
|
qcd = cv2.QRCodeDetector()
|
||||||
pageno = pagenum
|
pageno = pagenum
|
||||||
assignment = {}
|
assignment = {}
|
||||||
@ -56,31 +67,44 @@ def read_qrcodes(pdfdoc, inventory, pagenum=0) :
|
|||||||
kernel = np.ones((3,3),dtype=np.uint8)
|
kernel = np.ones((3,3),dtype=np.uint8)
|
||||||
ret,thresh = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
|
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))
|
thresh=cv2.bitwise_not(cv2.dilate(cv2.erode(cv2.bitwise_not(thresh),kernel,iterations=1),kernel,iterations=1))
|
||||||
|
data = []
|
||||||
data = qcd.detectAndDecodeMulti(thresh)
|
for k in range(4) :
|
||||||
if data[0] == True and data[1][0]!='':
|
dat = qcd.detectAndDecodeMulti(np.rot90(thresh,k))
|
||||||
assignment[pageno] = data[1]
|
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 = fitz.open()
|
||||||
newdoc.insert_pdf(doc,from_page=indx, to_page=indx, start_at=-1, rotate=-1, links=True, annots=True)
|
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 :
|
try :
|
||||||
newfilename = student_inventory[data[1][0]]
|
newfilename = student_inventory[data[1][0]]
|
||||||
except KeyError :
|
except KeyError :
|
||||||
print('key error', pageno)
|
print('key error', pageno)
|
||||||
pageno += 1
|
pageno += 1
|
||||||
continue
|
continue
|
||||||
|
except IndexError :
|
||||||
|
print('index error', pageno, data)
|
||||||
else :
|
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)
|
f.write(newbytes)
|
||||||
else :
|
else :
|
||||||
weirds += [pageno]
|
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 = fitz.open()
|
||||||
newdoc.insert_pdf(doc,from_page=indx, to_page=indx, start_at=-1, rotate=-1, links=True, annots=True)
|
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()
|
||||||
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)
|
f.write(newbytes)
|
||||||
img = Image.fromarray(thresh.astype('uint8'))
|
|
||||||
img.show()
|
|
||||||
pageno += 1
|
pageno += 1
|
||||||
return (assignment, weirds, pageno)
|
return (assignment, weirds, pageno)
|
||||||
|
|
||||||
@ -89,7 +113,7 @@ def read_qrcodes(pdfdoc, inventory, pagenum=0) :
|
|||||||
if __name__=='__main__' :
|
if __name__=='__main__' :
|
||||||
inventory = []
|
inventory = []
|
||||||
outp = {}
|
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)
|
print('pageno', pageno)
|
||||||
for ky,val in assignment.items() :
|
for ky,val in assignment.items() :
|
||||||
outp[ ky ] = val
|
outp[ ky ] = val
|
||||||
|
Loading…
Reference in New Issue
Block a user