Пример #1
0
    def _find_remove_box(self, ingredient_box):
        bw_box = bio.make_bw(ingredient_box)
        positions = bio.find_shapes(bw_box, self.remove_text_kernel, th=1100)
        assert len(positions) <= 1, 'At most one "Remove" text is allowed'
        if len(positions) == 0:
            return None
        x = int(positions[0][0])
        y = int(positions[0][1])
        x0 = x - 85
        x1 = x - 30
        y0 = y - 10
        y1 = y + 15
        is_cannot = np.sum(bw_box[y0:y1, x0:x1] > 0) > 100

        if is_cannot:
            upper_edge = y - 24
            lower_edge = y + 24
            remove_box = None
        else:
            x0 = x - 165
            x1 = x + 165
            y0 = y + 15
            y1 = y + 144
            upper_edge = y0 - 31
            lower_edge = y1
            remove_box = bio.cut(ingredient_box, ((x0, x1), (y0, y1)))
        return remove_box, upper_edge, lower_edge
    def _read_hoover_cure_concentrations(self, cure_box):
        box_bw = bio.make_bw(cure_box)

        active_range_text_position = bio.find_shapes(
            box_bw, self.active_range_text_kernel, 1850)
        assert len(active_range_text_position) == 1

        # max_text_position = bio.find_shapes(box_bw, self.max_text_kernel, 700)
        # assert len(max_text_position) == 1

        # flask_positions = bio.find_shapes(box_bw, self.flask_kernel, 500)
        # assert len(flask_positions) == 2

        arx, ary = active_range_text_position[0]
        # mx, my = max_text_position[0]
        # f1x, f1y = flask_positions[0]
        # f2x, f2y = flask_positions[1]

        # range_coordinates = [[arx + 50, f1x - 12], [ary - 10, ary + 10]]
        # max_coordinates = [[mx + 20, f2x - 12], [my - 10, my + 10]]
        slider_coordinates = [[arx - 51, arx + 189], [ary + 25, ary + 42]]

        # conc_optimal = self._read_concentration(bio.cut(cure_box, max_coordinates))
        # conc_range = self._read_concentration_range(bio.cut(cure_box, range_coordinates))

        conc_range_slider, conc_optimal_slider = self._read_cure_slider(
            bio.cut(cure_box, slider_coordinates))
        return conc_range_slider, conc_optimal_slider
 def _cut_machine(self, process_box):
     kernel = self.upgrade_with_text_kernel
     im_bw = bio.make_bw(process_box)
     upgrade_with_position = bio.find_shapes(im_bw, kernel, th=1750)
     if len(upgrade_with_position) != 1:
         return None
     x, y = upgrade_with_position[0]
     segment = bio.cut(process_box, [[x - 50, x + 50], [y + 10, y + 111]])
     return segment
Пример #4
0
def read_digits_by_kernels(segment, list_of_digit_kernels):
    im_bw = bio.make_bw(segment, th=200)
    digits = []
    for digit in range(0, 10, 1):
        kernel = list_of_digit_kernels[digit]
        positions = bio.find_shapes(im_bw, kernel, th=160)
        for pos in positions:
            x = pos[0]
            digits.append((x, digit))
    digits.sort()
    return tuple([d[1] for d in digits])
Пример #5
0
def cut_boxes(im):
    """
    Locates ingredient boxes and process boxes
    """
    main_im = bio.cut_main_screen(im)
    bw_im = bio.make_bw(main_im)

    kernel = np.load('./kernels/Import-text.npy')
    import_positions = bio.find_shapes(bw_im, kernel, th=5700)

    kernel = np.load('./kernels/Upgrade-text.npy')
    upgrade_positions = bio.find_shapes(bw_im, kernel, th=1950)

    x_offsets = {'import': -512, 'upgrade': -145}
    y_offsets = {'import': -80, 'upgrade': -395}
    if len(import_positions) >= len(upgrade_positions):
        x_offset = x_offsets['import']
        y_offset = y_offsets['import']
        positions = import_positions
    else:
        x_offset = x_offsets['upgrade']
        y_offset = y_offsets['upgrade']
        positions = upgrade_positions

    width = 580
    height = 430
    boxes = []
    for pos in positions:
        x0 = int(pos[0] + x_offset)
        y0 = int(pos[1] + y_offset)
        x1 = x0 + width
        y1 = y0 + height
        if x0 < 0 or y0 < 0 or \
           x1 > main_im.shape[1] or y1 > main_im.shape[0]:
            continue
        box = main_im[y0:y1, x0:x1, :]
        boxes.append(box)

    return boxes
 def _cut_price(self, cure_box):
     """
     Selects the part of the image that contains
     the price of the cure after the dollar sign
     """
     bw = bio.make_bw(cure_box)
     dollar_centers = bio.find_shapes(bw, self.dollar_kernel, th=650)
     if len(dollar_centers) != 1:
         return None
     x, y = dollar_centers[0]
     height = 20
     padding = 10
     return cure_box[int(y) - height // 2:int(y) + height // 2,
                     int(x) + padding:]
Пример #7
0
 def _cut_concentration(self, remove_box):
     """
     Selects the part of the image that contain concentration range,
     using the position of the flask icon in a process box,
     from the Cures screen.
     """
     bw = bio.make_bw(remove_box)
     kernel = self.flask_kernel
     flask_center = bio.find_shapes(bw, kernel, 465)
     if len(flask_center) != 1:
         return None
     x, y = flask_center[0]
     height = 20
     padding = 5
     length = 60
     return remove_box[int(y) - height // 2:int(y) + height // 2 + 2,
                       int(x) - length - padding:int(x) - padding]
def cut_boxes(im):
    """
    Locates effect boxes and process boxes on the b&w
    screen shot of the Cures screen.
    """
    im_main_screen = bio.cut_main_screen(im)
    im_bw = bio.make_bw(im_main_screen)

    # find triangles
    kernel = np.load('./kernels/triangle.npy')
    triangle_centers = bio.find_shapes(im_bw, kernel, 950)
    triangle_centers.sort()

    # group triangles into columns
    columns_of_triangles = []
    columns_of_triangles.append([triangle_centers[0]])
    for triangle in triangle_centers[1:]:
        if np.abs(columns_of_triangles[-1][0][0] - triangle[0]) < 50:
            columns_of_triangles[-1].append(triangle)
        else:
            columns_of_triangles.append([triangle])

    cure_box_height = 63
    process_box_height = 125
    box_width = 290
    padding = 15
    last_padding = 22

    cure_box_gap = cure_box_height + 2 * padding
    process_box_gap = process_box_height + 2 * padding

    columns_of_box_identities = []  # 'drug' or 'process'
    columns_of_box_coordinates = []  # (x1, x2, y1, y2)
    for idx, col in enumerate(columns_of_triangles):
        if len(col) < 2:
            print('Warning: missing triangles in column ' + str(idx))
            break

        box_identities = []
        box_coordinates = []
        columns_of_box_identities.append(box_identities)
        columns_of_box_coordinates.append(box_coordinates)
        for i in range(len(col) - 1):

            gap = col[i + 1][1] - col[i][1]
            gap_identity = identify_gap(gap, cure_box_gap, process_box_gap)
            if gap_identity in ['cure', 'process']:
                box_identities.append(gap_identity)
                y1 = col[i][1] + padding
                y2 = col[i + 1][1] - padding
                x1 = col[i][0] - box_width // 2
                x2 = col[i][0] + box_width // 2
                box_coordinates.append((x1, x2, y1, y2))

            else:
                box_identities.append('cure')
                x, y = col[i]
                y1 = y + last_padding
                y2 = y + last_padding + cure_box_height
                x1 = x - box_width // 2
                x2 = x + box_width // 2
                box_coordinates.append((x1, x2, y1, y2))

                box_identities.append('cure')
                x, y = col[i + 1]
                y1 = y - padding - cure_box_height
                y2 = y - padding
                x1 = x - box_width // 2
                x2 = x + box_width // 2
                box_coordinates.append((x1, x2, y1, y2))

        # revise first box revision
        if box_identities[0] == 'cure':
            box_identity = 'process'
            box_height = process_box_height
        elif box_identities[0] == 'process':
            box_identity = 'cure'
            box_height = cure_box_height
        x, y = col[0]
        y1 = y - padding - box_height
        y2 = y - padding
        x1 = x - box_width // 2
        x2 = x + box_width // 2
        if y1 > 0 and y2 < im_main_screen.shape[0]:
            box_identities.insert(0, box_identity)
            box_coordinates.insert(0, (x1, x2, y1, y2))

        # revise last box
        if box_identities[-1] == 'cure':
            box_identity = 'process'
            box_height = process_box_height
        elif box_identities[-1] == 'process':
            box_identity = 'cure'
            box_height = cure_box_height
        x, y = col[-1]
        y1 = y + last_padding
        y2 = y + last_padding + box_height
        x1 = x - box_width // 2
        x2 = x + box_width // 2
        if y1 > 0 and y2 < im_main_screen.shape[0]:
            box_identities.append(box_identity)
            box_coordinates.append((x1, x2, y1, y2))

    # cut from image
    columns_of_boxes = []
    for box_coordinates in columns_of_box_coordinates:
        boxes = []
        columns_of_boxes.append(boxes)
        for box_coord in box_coordinates:
            x1, x2, y1, y2 = box_coord
            boxes.append(im_main_screen[int(y1):int(y2), int(x1):int(x2), :])

    return columns_of_boxes, columns_of_box_identities