예제 #1
0
def test_image_and_solve():
    file_path = FILE_PATH.parent / "img/lvl_725.jpg"
    with open(file_path, 'rb') as f:
        file_bytes = np.asarray(bytearray(f.read()), dtype=np.uint8)
        image_parser = ImageParser(file_bytes)
        colors = image_parser.to_colors()

    puzzle = BallSortPuzzle(colors)
    result = puzzle.solve()
    assert result is True
    assert (
        str(puzzle.moves)
        == "[Ball(0 -> 12), Ball(1 -> 13), Ball(7 -> 12), Ball(11 -> 1), Ball(13 -> 11), Ball(3 -> 13), "
        "Ball(4 -> 13), Ball(9 -> 4), Ball(3 -> 9), Ball(12 -> 3), Ball(12 -> 3), Ball(4 -> 12), "
        "Ball(4 -> 12), Ball(0 -> 4), Ball(6 -> 0), Ball(11 -> 12), Ball(11 -> 12), Ball(7 -> 11), "
        "Ball(4 -> 7), Ball(4 -> 7), Ball(4 -> 11), Ball(1 -> 4), Ball(1 -> 4), Ball(5 -> 4), Ball(2 -> "
        "5), Ball(8 -> 4), Ball(5 -> 2), Ball(9 -> 8), Ball(2 -> 5), Ball(10 -> 9), Ball(1 -> 10), "
        "Ball(5 -> 2), Ball(10 -> 1), Ball(10 -> 1), Ball(2 -> 5), Ball(10 -> 6), Ball(5 -> 2), "
        "Ball(10 -> 13), Ball(2 -> 5), Ball(3 -> 10), Ball(3 -> 10), Ball(3 -> 10), Ball(2 -> 3), "
        "Ball(2 -> 10), Ball(5 -> 3), Ball(5 -> 3), Ball(2 -> 5), Ball(6 -> 2), Ball(6 -> 2), Ball(5 -> "
        "6), Ball(5 -> 6), Ball(5 -> 1), Ball(2 -> 5), Ball(2 -> 5), Ball(6 -> 2), Ball(6 -> 2), "
        "Ball(6 -> 2), Ball(6 -> 13), Ball(5 -> 6), Ball(5 -> 6), Ball(7 -> 5), Ball(7 -> 5), Ball(7 -> "
        "5), Ball(7 -> 2), Ball(6 -> 7), Ball(6 -> 7), Ball(8 -> 6), Ball(8 -> 6), Ball(7 -> 8), "
        "Ball(7 -> 8), Ball(6 -> 7), Ball(6 -> 7), Ball(8 -> 6), Ball(8 -> 6), Ball(8 -> 6), Ball(8 -> "
        "5), Ball(7 -> 8), Ball(7 -> 8), Ball(9 -> 7), Ball(7 -> 8), Ball(9 -> 7), Ball(7 -> 8), "
        "Ball(9 -> 7), Ball(9 -> 0), Ball(7 -> 9), Ball(11 -> 7), Ball(7 -> 9), Ball(11 -> 7), "
        "Ball(7 -> 9), Ball(11 -> 7), Ball(7 -> 9), Ball(11 -> 6)]"
    )
예제 #2
0
def handler(event: Optional[dict], context: Optional[dict]):
    body = json.loads(event['body'])  # type: ignore
    print(body)
    message = body['message']
    chat_id = message['chat']['id']

    if photos := message.get('photo'):
        # here photos is an array with same photo of different sizes
        hd_photo = max(photos, key=lambda x: x['file_size']
                       )  # get one with the highest resolution
        try:
            file = telegram_client.download_file(hd_photo['file_id'])
        except TelegramClientError:
            text = "Cant download the image from TG :("
        else:
            file_bytes = np.asarray(bytearray(file.read()), dtype=np.uint8)
            try:
                image_parser = ImageParser(file_bytes)
                colors = image_parser.to_colors()
            except ImageParserError as exp:
                text = f"Cant parse image: {exp}"
            else:
                puzzle = BallSortPuzzle(colors)
                solved = puzzle.solve()
                if solved:
                    text = get_telegram_repr(puzzle)
                else:
                    text = "This lvl don't have a solution"
예제 #3
0
def test_no_solution(filename):
    file_path = FILE_PATH.parent / filename
    with open(file_path, 'rb') as f:
        file_bytes = np.asarray(bytearray(f.read()), dtype=np.uint8)
        image_parser = ImageParser(file_bytes, debug=False)
        colors = image_parser.to_colors()

    assert colors

    puzzle = BallSortPuzzle(colors)
    result = puzzle.solve()
    assert result is False
예제 #4
0
def test_image3():
    file_path = FILE_PATH.parent / "img/lvl_1051.jpg"
    with open(file_path, 'rb') as f:
        file_bytes = np.asarray(bytearray(f.read()), dtype=np.uint8)
        image_parser = ImageParser(file_bytes)
        colors = image_parser.to_colors()

    assert colors

    puzzle = BallSortPuzzle(colors)
    result = puzzle.solve()
    assert result is True
예제 #5
0
def test_image1():
    file_path = FILE_PATH.parent / "img/lvl_725.jpg"
    with open(file_path, 'rb') as f:
        file_bytes = np.asarray(bytearray(f.read()), dtype=np.uint8)
        image_parser = ImageParser(file_bytes, debug=False)
        colors = image_parser.to_colors()

    assert colors == [
        [color.L_BLUE, color.L_BLUE, color.GREEN, color.L_GREEN],
        [color.PINK, color.PINK, color.BLUE, color.BROWN],
        [color.LIME, color.L_GREEN, color.VIOLET, color.VIOLET],
        [color.VIOLET, color.L_GREEN, color.GRAY, color.ORANGE],
        [color.RED, color.GREEN, color.BROWN, color.ORANGE],
        [color.PINK, color.LIME, color.VIOLET, color.BLUE],
        [color.ORANGE, color.LIME, color.YELLOW, color.L_BLUE],
        [color.LIME, color.GREEN, color.RED, color.L_GREEN],
        [color.GREEN, color.YELLOW, color.GRAY, color.BLUE],
        [color.L_BLUE, color.RED, color.GRAY, color.BROWN],
        [color.ORANGE, color.YELLOW, color.PINK, color.GRAY],
        [color.YELLOW, color.RED, color.BROWN, color.BLUE],
        [],
        [],
    ]
예제 #6
0
def imgs_for_pred(
    df: pd.DataFrame,
    path_col: str,
    img_dims: tuple,
    batch_size: int,
):
    image_set = (tf.data.Dataset.from_tensor_slices((
        df[path_col],
        tuple([]),
    )).map(ImageParser.ImageParser(),
           num_parallel_calls=tf.data.experimental.AUTOTUNE).map(
               ImageResizer.ImageResizer(img_dims, "stretch"),
               num_parallel_calls=tf.data.experimental.AUTOTUNE).batch(
                   batch_size).prefetch(tf.data.experimental.AUTOTUNE))
    return image_set