def markup_response_codes(page: Page, list_id: str, line_number: int) -> List[ResponseCode]: # Get line in the page. line_bb = page.get_line_bb(line_number, utils.get_list_dir(list_id), padding=100, right_half=True) markup_roi = page.get_roi(line_bb) # Iterate through and mark each scan code. response_codes = [] question_number = 1 while True: print("Please mark each response code in survey question %d." % question_number) bounding_box = markup_roi.markup() text = markup_roi.find_text(bounding_box) # sometimes OCR picks up stray symbols, get rid of them. text = ''.join(ch for ch in text if ch.isalnum()) bounding_box = bounding_box.update_coordinate_system(line_bb.top_left) roi = page.get_roi(bounding_box) response_code = ResponseCode(bounding_box, question_number, text) print("Extracted scan code: \"%s\"" % response_code.value) while True: print("Is this correct? [y|n]") yes_no = input().lower() if yes_no == "y": break else: print("Please enter the correct response code: ") response_code.value = input() response_codes.append(response_code) print( r"""Hit enter (no input) to mark another response in the same survey question. Enter 'n' to move to the next survey question. Enter 'q' to finish. [enter|n|q]""" ) next_step = input().lower() if next_step == "n": question_number += 1 elif next_step == "q": break return response_codes
def check_num_barcodes(page: Page, list_dir: str, num_scanned_barcodes: int, results_stats) -> None: # Manually loop and count barcodes num_actual_barcodes = 0 for line_number in range(1, utils.MAX_BARCODES_ON_PAGE + 1): line_bb = page.get_line_bb(line_number, list_dir) # extract the barcode portion line_bb.top_left.x = line_bb.bottom_right.x - 700 barcode_roi = page.get_roi(line_bb).invert() BARCODE_EXISTS_THRESHOLD = 20000 # if a barcode exists in the area it averages 29k black pixels. if barcode_roi.numWhitePixels() > BARCODE_EXISTS_THRESHOLD: num_actual_barcodes += 1 else: break # we have likely reached the end of the page. if num_actual_barcodes < num_scanned_barcodes: print( "Something went wrong with the image alignment! Cannot accurately count missed barcodes." ) elif num_actual_barcodes > num_scanned_barcodes: results_stats[ "num_missed_barcodes"] += num_actual_barcodes - num_scanned_barcodes