def __init__(self, parent_window, template_dict, input_folder_path, output_file_path, abort_event=None): Thread.__init__(self) Abortable.__init__(self, abort_event) self.id = uuid.uuid4() self._parent_window = parent_window self.omr_factory = OmrFactory( template_dict, abort_event=self.abort_event, id=self.id) self.input_folder_path = input_folder_path self.output_file_path = output_file_path
def attendance_omr_factory_1(res_folder): config = json.load( open( str( Path(res_folder) / 'attendance_form' / 'attendance_form_config' / 'omr_config.json'))) return OmrFactory(config)
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_process_example_scan_folder(self, res_folder): config = json.load( open( str( Path(res_folder) / 'attendance_form' / 'scan_example' / 'omr_config.json'))) omr_factory = OmrFactory(config) image_folder_path = str( Path(res_folder) / 'attendance_form' / 'scan_example' / 'images') df = omr_factory.process_images_folder(image_folder_path) df = df.sort_values(by=['file', 'sub_form']) df = df.sort_index(axis=1) correct_result = pd.read_csv(str( Path(res_folder) / 'attendance_form' / 'scan_example' / 'correct_result.csv'), index_col=0) correct_result = correct_result.sort_values(by=['file', 'sub_form']) correct_result = correct_result.sort_index(axis=1) print(df.to_string()) assert_correct_result(df, correct_result)
class DataExtractionWorker(Thread, Abortable): def __init__(self, parent_window, template_dict, input_folder_path, output_file_path, abort_event=None): Thread.__init__(self) Abortable.__init__(self, abort_event) self.id = uuid.uuid4() self._parent_window = parent_window self.omr_factory = OmrFactory( template_dict, abort_event=self.abort_event, id=self.id) self.input_folder_path = input_folder_path self.output_file_path = output_file_path def run(self): try: df = self.omr_factory.process_images_folder( self.input_folder_path, self.output_file_path) if df is None: wx.PostEvent(self._parent_window, DataExtractionNonFatalEvent()) elif isinstance(df, pd.DataFrame): wx.PostEvent( self._parent_window, DataExtractionSuccessEvent( data=df, input_path=self.input_folder_path, output_path=self.output_file_path, worker_id=self.id)) else: raise TypeError( f'got unexpected return type {type(df)} from omr factory') except EmptyFolderException as e: print(e) dlg = ExceptionDialog( 'No image files found in the selected folder', parent=None, fatal=False, title='Error') dlg.ShowModal() dlg.Destroy() wx.PostEvent(self._parent_window, DataExtractionNonFatalEvent()) except AbortException: print(f'aborted processing worker {self.id}') wx.PostEvent(self._parent_window, DataExtractionNonFatalEvent()) except BaseException as e: print(f'fatal exception in worker {self.id}') wx.PostEvent(self._parent_window, FatalEvent(e))
def test_process_problem_old_forms_math_l2(self, res_folder): config = json.load( open( str( Path(res_folder) / 'exam_form' / 'problem_scans' / 'omr_config.json'))) omr_factory = OmrFactory(config) image_folder_path = str( Path(res_folder) / 'exam_form' / 'problem_scans' / 'images') df = omr_factory.process_images_folder(image_folder_path) correct_result = pd.read_csv(str( Path(res_folder) / 'exam_form' / 'problem_scans' / 'correct_result.csv'), index_col=0) print('extracted sub_form_data:') df = df.sort_values(by=['file', 'sub_form']) df = df.sort_index(axis=1) print(df.to_string()) print('correct sub_form_data:') correct_result = correct_result.sort_values(by=['file', 'sub_form']) correct_result = correct_result.sort_index(axis=1) print(correct_result.to_string()) assert_correct_result(df, correct_result)
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)