Exemple #1
0
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
Exemple #2
0
def handle_missing_page_id(aligned_page: Page, original_page: Page,
                           list_id: str, id_bounding_box: BoundingBox,
                           page_number: int) -> Tuple[bool, Page]:
    # to check if it's a homography issue, see if the list ID is visible on the raw page
    test_id = original_page.get_list_id(id_bounding_box)
    if test_id == list_id:
        print('Homography error on page {}, using uncorrected page instead.'.
              format(page_number))
        return True, original_page

    # didn't find on raw page, ask the user to confirm the ID
    id_area = original_page.get_roi(id_bounding_box)
    id_area = id_area.add_border(top=30)

    # annotate the response image
    question = "ID: {}? (y|n)".format(list_id)

    # keep looping until the y or n key is pressed
    while True:
        # display the image and wait for a keypress
        key = id_area.show(title="review", message=question, resize=False)

        # if the 'y' key is pressed, user approves
        if key == ord("y"):
            cv2.destroyAllWindows()
            return True, original_page

        # if the 'n' key is pressed, user rejects
        elif key == ord("n"):
            cv2.destroyAllWindows()
            break

    return False, aligned_page
Exemple #3
0
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
Exemple #4
0
def create_error_image(page: Page, barcode_coords: BoundingBox,
                       first_response_coords: BoundingBox) -> Image:
    full_response_bounding_box = get_response_including_barcode(
        barcode_coords, first_response_coords, page.size)
    error_image = page.get_roi(full_response_bounding_box)
    return error_image