def get_diffed_response_codes(cur_rc: Image, list_dir: str) -> Tuple[Image, Image]: cur_rc = cur_rc.threshold() # Load the reference response codes, bold and not bold version. ref_rc = Image.from_file(list_dir + utils.RESPONSE_CODES_IMAGE_FILENAME, Rotation.NONE) ref_rc = ref_rc.threshold() bold_ref_rc = Image.from_file( list_dir + utils.BOLD_RESPONSE_CODES_IMAGE_FILENAME, Rotation.NONE) bold_ref_rc = bold_ref_rc.threshold(bold_ref_rc) # Align and diff against both reference images. aligned_rc = cur_rc.align_to(ref_rc) bold_aligned_rc = cur_rc.align_to(bold_ref_rc) diff = aligned_rc.diff_against(ref_rc) bold_diff = bold_aligned_rc.diff_against(bold_ref_rc) # Count how many white pixels are in each diff. white_pixels = diff.numWhitePixels() bold_white_pixels = bold_diff.numWhitePixels() # The one with the least white pixels should be the correct image. if white_pixels < bold_white_pixels: return diff, aligned_rc else: return bold_diff, bold_aligned_rc
def main() -> None: args = parse_args() check_for_errors(args) if args["skip_bold"]: args["bold_page_number"] = None args["bold_line_number"] = None rotate_dir = utils.map_rotation(args["rotate_dir"]) retval = ingest_clean_page(args["clean"], args["page_number"], rotate_dir, args["bold_page_number"], args["bold_line_number"]) if not retval: print("Could not ingest the clean page.") return list_id, clean_page = retval num_pages = ingest_walklist(list_id, args["walklist"]) list_dir = utils.get_list_dir(list_id) response_codes: List[ResponseCode] = [] if not args["skip_markup"]: response_codes = markup_response_codes(clean_page, list_id, args["line_number"]) else: response_codes = utils.load_response_codes(list_id) bounding_box = get_response_codes_bounding_box(response_codes, clean_page) # Normalize the response code coords if not args["skip_markup"]: for code in response_codes: code.coords = Point(code.coords.x - bounding_box.top_left.x, code.coords.y - bounding_box.top_left.y) # Save out the ResponseCodes themselves. save_response_codes(list_id, response_codes) # Save out the response code image. rc_image_path = utils.get_list_dir( list_id) + utils.RESPONSE_CODES_IMAGE_FILENAME rc_roi = clean_page.get_roi(bounding_box) rc_roi.save_to_file(rc_image_path) # Save out the response code bounding box. utils.save_ref_boxes(list_dir, {"response_codes": bounding_box.to_list()}) if not args["skip_bold"]: # Save out the bold response code image. list_dir = utils.get_list_dir(list_id) rc_roi = Image.from_file( list_dir + utils.RESPONSE_CODES_IMAGE_FILENAME, Rotation.NONE) bold_rc_roi = Image.from_file( list_dir + utils.BOLD_RESPONSE_CODES_IMAGE_FILENAME, Rotation.NONE) aligned_bold_rc_roi = bold_rc_roi.align_to(rc_roi) if utils.__DEBUG__: aligned_bold_rc_roi.show() bold_rc_image_path = list_dir + utils.BOLD_RESPONSE_CODES_IMAGE_FILENAME aligned_bold_rc_roi.save_to_file(bold_rc_image_path) print("Saved out reference response codes.") print("Done, now run scan.py")