class FormGenerationWorker(Thread, Abortable): def __init__(self, parent_window, excel_file_path, output_path, description, abort_event=None): Thread.__init__(self) Abortable.__init__(self, abort_event) self.id = uuid.uuid4() self._parent_window = parent_window self.form_maker = FormMaker( excel_file_path=excel_file_path, output_path=output_path, description=description, abort_event=abort_event, id=self.id) self.excel_file_path = excel_file_path self.output_folder_path = output_path def run(self): try: self.form_maker.make_form() wx.PostEvent( self._parent_window, FormGenerationSuccessEvent( input_path=self.excel_file_path, output_path=self.output_folder_path, worker_id=self.id)) except AbortException: print(f'aborted form generation worker {self.id}') wx.PostEvent(self._parent_window, FormGenerationNonFatalEvent()) except BaseException as e: print(f'fatal exception in worker {self.id}') wx.PostEvent(self._parent_window, FatalEvent(e))
def main(): import sys from pathlib import Path sys.path.append(str(Path(__file__).parent.parent.resolve())) import argparse from pyomrx.core.form_maker import FormMaker from pyomrx.core.omr_factory import OmrFactory parser = argparse.ArgumentParser( description='Tool for generating and extracting sub_form_data ' 'from optical mark recognition forms') parser.add_argument( 'mode', nargs=1, type=str, help='make|extract ' '(whether to make an omr form or extract sub_form_data from a folder of scans)' ) parser.add_argument('-i', '--input', type=str, nargs=1, help='path to input file (make) or folder (extract)') parser.add_argument('-o', '--output', type=str, nargs=1, help='path to output file') parser.add_argument( '-t', '--template', type=str, nargs=1, required=False, help='path to omr template (required if mode == extract)') args = parser.parse_args() input_path = args.input[0] output_path = args.output[0] if args.mode[0] == 'make': excel_path = input_path output_folder = output_path form_maker = FormMaker(excel_file_path=excel_path, output_path=output_folder) form_maker.make_form() elif args.mode[0] == 'extract': if not args.template: raise parser.error('no omr template file specified') omr_file_path = args.template[0] images_folder = input_path output_file_path = output_path omr_factory = OmrFactory.from_omr_file(omr_file_path=omr_file_path) omr_factory.process_images_folder(images_folder, output_file_path=output_file_path) else: parser.error( 'neither form gen mode or sub_form_data extract mode active') return 0
def test_no_prefix_if_none(self, res_folder, tmpdir): form_maker = FormMaker( str( Path(res_folder) / 'attendance_form' / 'Absence register v31.xlsx'), str(tmpdir / 'temp_config.omr')) config = form_maker.make_form() assert next( filter( lambda circle_group: circle_group['name'] == 'attendance', config['template']['sub_forms']['sub_form_template'] ['circles']))['column_prefix'] is None
def test_translate_sub_range_to_new_parent(self, res_folder, tmpdir): form_maker = FormMaker( str( Path(res_folder) / 'attendance_form' / 'Absence register v31.xlsx'), str(tmpdir / 'temp_config.omr')) page_1_range = form_maker.wb.get_named_range('page_1').attr_text page_2_range = form_maker.wb.get_named_range('page_2').attr_text circles_att_range = form_maker.wb.get_named_range( 'circles_attendance').attr_text translated_circles_range = form_maker.translate_sub_range_to_new_parent( circles_att_range, page_1_range, page_2_range) assert translated_circles_range == 'template!$D$32:$AH$56'
def __init__(self, parent_window, excel_file_path, output_path, description, abort_event=None): Thread.__init__(self) Abortable.__init__(self, abort_event) self.id = uuid.uuid4() self._parent_window = parent_window self.form_maker = FormMaker( excel_file_path=excel_file_path, output_path=output_path, description=description, abort_event=abort_event, id=self.id) self.excel_file_path = excel_file_path self.output_folder_path = output_path
def test_exam_form_example(request, res_folder): exam_example_folder = Path('examples/example_exam_score_form/') excel_file_path = exam_example_folder / 'example exam score form.xlsx' def delete_temp_files(): for path in exam_example_folder.iterdir(): if str(path) == str(excel_file_path): continue os.remove(str(path)) request.addfinalizer(delete_temp_files) omr_file_path = exam_example_folder / 'example exam score form.omr' example_output_file_path = exam_example_folder / 'example_output.csv' form_maker = FormMaker(str(excel_file_path), str(omr_file_path)) form_maker.make_form() omr_factory = OmrFactory.from_omr_file(str(omr_file_path)) omr_factory.process_images_folder(str(exam_example_folder), str(example_output_file_path)) correct_result_path = Path( res_folder) / 'correct_examples_answers/correct_result_exam_form.csv' correct_df = pd.read_csv(str(correct_result_path)) df = pd.read_csv(str(example_output_file_path)) assert_correct_result(df, correct_df)
def test_make_form_doesnt_crash(self, res_folder, tmpdir): form_maker = FormMaker( str(Path(res_folder) / 'exam_form' / 'example_exam_form.xlsx'), str(tmpdir / 'temp_config.omr')) config = form_maker.make_form()
def test_make_form_doesnt_crash(self, res_folder, tmpdir): form_maker = FormMaker( str( Path(res_folder) / 'attendance_form' / 'Absence register v31.xlsx'), str(tmpdir / 'temp_config.omr')) config = form_maker.make_form()