def run_program(pubquiz_answer_sheets, save_image=False, db=None): print("De officiele Ordina pub-quiz antwoord vinder") model = Model(open('model/charList.txt').read()) answersheet_id = -1 for answer_sheets in pubquiz_answer_sheets: # The pdf file. We can it and it returns 1 to multiple answer pages pages = convert_pdf_to_image(answer_sheets) for p in range(0, len(pages)): # We take the name from the file. But we want it without any extension. file_extension = os.path.splitext(answer_sheets) sheet_name = answer_sheets if file_extension[1] == ".pdf": sheet_name = sheet_name[0:-4] sheet_name = sheet_name + "_" + str(p) answersheet_id = save_to_database.save_answersheet_database( db, pages[p]) if answersheet_id == -1: print("something went wrong, no answersheet id present") exit() else: process_sheet(pages[p], model, save_image, sheet_name, db, answersheet_id)
def test_multiple_pdf_pages(self): file_name = "grayscale_pubquiz" path = '../original_scan/' pubquiz_answer_sheet = path + file_name + '.pdf' answer_sheets_image = convert_pdf_to_image(pubquiz_answer_sheet) # If the test is successful we save the the images if the folder is empty so we can use these in the next tests path = 'test_files/image_files/' if not os.path.exists(path): os.makedirs(path) if len(os.listdir(path)) == 0: for index in range(0, len(answer_sheets_image)): # We only save if the test was ok and the file does not exist yet. cv2.imwrite(path + file_name + "_" + str(index) + "_image.png", answer_sheets_image[index])
def test_multiple_pdf_pages(self): file_name = "new3_2" path = '../original_scan/' pubquiz_answer_sheet = path + file_name + '.pdf' # If the test is successful we save the the images if the folder is empty so we can use these in the next tests path = 'test_files/image_files/' if not os.path.exists(path): os.makedirs(path) index = 0 for answer_sheets_image in convert_pdf_to_image(pubquiz_answer_sheet): cv2.imwrite(path + file_name + "_" + str(index) + "_image.png", answer_sheets_image) index += 1
def run_pubquiz_program(answer_sheets): print("De officiele Ordina pub-quiz antwoord vinder") model = Model(open('model/charList.txt').read()) # Not the cleanest way to detect an error, but the fastest to implement problem = False for p in convert_pdf_to_image(answer_sheets): if p is not None: try: print("start processing answersheet") if process_sheet(p, model): problem = True except: return "Bestand uploaden mislukt. Het bestand kan niet uitgelezen worden." if problem: return "Bestand uploaden mislukt. Het bestand kan niet uitgelezen worden."
def test_pdf_to_image_conversion(self): """ converts the pdf to the image based on the give path. Check if the pdf is correctly converted. No test with multiple scans is included. If multiple scans are read they are converted individually. This is because we will have 2 separate files rather than 1 pdf with 2 pages so we would need to call the function twice regardless. """ file_name = "grayscale_pubquiz" pubquiz_answer_sheet = '../original_scan/' + file_name + '.pdf' answer_sheets_image = convert_pdf_to_image(pubquiz_answer_sheet) # For this test we only read 1 file answer_sheet_image = answer_sheets_image[0] # The conversion should give a colour image, which is the final parameter. # We test if the conversion is successful by seeing if the result is a double array with a colour parameter # If something went wrong it will not be a double array with rgb values so we give an error. self.assertEqual(len(answer_sheet_image[0][0]), 3)
def test_no_pdf_file(self): file_name = "scan_0" pubquiz_answer_sheet = 'test_files/image_files/' + file_name + '_image.png' answer_sheet_image = convert_pdf_to_image(pubquiz_answer_sheet) self.assertEqual(answer_sheet_image, None)
def test_wrong_file_path(self): file_name = "wrong_file_name" pubquiz_answer_sheet = '../original_scan/' + file_name + '.pdf' # For this test we only read 1 file answer_sheet_image = convert_pdf_to_image(pubquiz_answer_sheet) self.assertEqual(answer_sheet_image, None)
def save_answersheet(): pubquiz_anser_sheets = 'scan.pdf' pages = convert_pdf_to_image(pubquiz_anser_sheets) return pages