示例#1
0
def find_limits(state, sections, ticket_settings, section_data):
    c_i = state.curr_image.copy()
    b_i = state.background_image.copy()

    diff_mask = difference_mask(c_i, b_i, ticket_settings)

    limit = ""
    for section_num, section in sections.items():
        if section.function == Function.LIMIT:
            mask = cv2.bitwise_and(section.mask, diff_mask)
            _, contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                              cv2.CHAIN_APPROX_NONE)
            contour = sorted(contours, key=cv2.contourArea, reverse=True)

            if len(contour) > 0:
                contour = contour[0]
            else:
                continue
            x, y, w, h = cv2.boundingRect(contour)

            if h > 40 and w > 40:
                thresh_limit, colored_limit = cluster(c_i[y:y + h, x:x + w])
                limit = find_numbers_in_mask(thresh_limit, colored_limit,
                                             state.classifier,
                                             ticket_settings["Digits"])

                if limit != "" and limit != 0:
                    prev_limit = section_data[section.name]
                    section_data[section.name] = int(limit)
                    Section.map_section_names(sections, section_data)
                    if int(prev_limit) != int(limit):
                        logging.info(
                            f"SECTION - The \"{section.name}\" has changed from {prev_limit} to {limit} (ID:#{section_num})"
                        )
                        state.board_sections = state.draw_section_rectangles(
                            sections)

    return sections, section_data
示例#2
0
def demo(logging_level, photo_folder="", demo_id="", load_pkls=False):
    load_logging(logging_level)
    object_file_names = ["settings", "sections", "board"]

    stage = None
    settings = []
    sections = {}
    database = None
    board = None

    if load_pkls:
        stage = Stage.MAIN
        settings, sections, board = load_from_pickle_files(object_file_names)
        database = Database(settings["General"]["token"])
    else:
        stage = Stage.INIT
        settings = load_settings("../json/settings.json", demo_id)
        board = Board(photo_folder)
        board.classifier = load_classifiers(
            settings["Tickets"]["Digits"]["recognition_alg"])

        database = Database(settings["General"]["token"])
        database.reset()
        database.init_ticket_collection(settings["General"]["ticket_file"],
                                        demo_id)
        database.init_section_collection(settings["General"]["section_file"],
                                         demo_id)
        board.data = database.data

    load_thresholds(settings)
    photo = board.next_photo()

    while photo is not None:
        if stage == Stage.INIT and find_board(photo, settings["Board"]):
            background_image, sections, points = find_and_transform_board(
                photo, sections, settings["Board"])

            board.set_background_image(background_image)
            board.set_points(points)

            board.unmapped_sections = board.draw_section_rectangles(sections)
            sections = Section.map_section_names(sections,
                                                 board.data["Sections"])
            board.board_sections = board.draw_section_rectangles(sections)

            stage = Stage.MAIN

        elif stage == Stage.MAIN and find_board(photo, settings["Board"]):
            board.set_curr_image(transform_corners(photo, board.points))
            added_tickets, removed_tickets = find_tickets(
                board, settings["Tickets"], board.data["Tickets"])

            assignees = find_assignees(board)

            find_limits(board, sections, settings["Tickets"],
                        board.data["Sections"])

            board.map_tickets_to_sections(added_tickets, removed_tickets,
                                          assignees, sections, database)

            save_view(sections, board)
            interface(object_file_names, settings, sections, board, demo_id)

        photo = board.next_photo()

    sys.exit()