Beispiel #1
0
def render(npy_p2b, out_dir, port, spp, img_size):
    npy_file = (os.path.expanduser("~") + "/minecraft_houses/" +
                ".".join(npy_p2b.split(".")[1:-2]) + "/schematic.npy")

    schematic = np.load(npy_file)
    house_name = os.path.basename(os.path.dirname(npy_file))

    # remove blocks below ground-level
    g = ground_height(schematic)
    schematic = schematic[(g or 0):, :, :, :]
    ys, zs, xs = np.nonzero(schematic[:, :, :, 0] > 0)

    xmid, ymid, zmid = np.mean(xs), np.mean(ys), np.mean(zs)
    focus = np.array([xmid, ymid + 63,
                      zmid])  # TODO: +63 only works for flat_world seed=0w

    yaw, distance = list(map(int, npy_p2b.split(".")[-2].split("_")))

    look = [yaw, 0]
    look_xyz = to_unit_vec(*look)
    camera = focus - (look_xyz * distance)

    logging.info("Launching cuberite at port {}".format(port))
    p = CuberiteProcess("flat_world",
                        seed=0,
                        game_mode="creative",
                        place_blocks_yzx=schematic,
                        port=port)
    logging.info("Destroying cuberite at port {}".format(port))
    p.destroy()

    world_dir = os.path.join(p.workdir, "world")

    render_view_bin = os.path.join(repo_home, "bin/render_view")
    assert os.path.isfile(
        render_view_bin
    ), "{} not found.\n\nTry running: make render_view".format(render_view_bin)

    procs = []

    chunky_id = "{}_{}".format(yaw, distance)
    out_file = "{}/chunky.{}.{}.png".format(out_dir, house_name, chunky_id)

    call = [
        str(a) for a in [
            "python3",
            "{}/../minecraft_render/render.py".format(repo_home),
            "--world",
            world_dir,
            "--out",
            out_file,
            "--camera",
            *camera,
            "--look",
            yaw,
            0,
            "--size",
            *img_size,
            "--spp",
            spp,
        ]
    ]
    logging.info("CALL: " + " ".join(call))
    procs.append(subprocess.Popen(call))

    for p in procs:
        p.wait()
def render(npy_p2b, out_dir, port, spp, img_size, mn=None):
    npy_file = (
        os.path.expanduser("~")
        + "/minecraft_houses/"
        + ".".join(npy_p2b.split(".")[1:-2])
        + "/schematic.npy"
    )

    schematic = np.load(npy_file)
    print(schematic.shape)
    house_name = os.path.basename(os.path.dirname(npy_file))
    p2b = np.load(npy_p2b)

    # remove blocks below ground-level
    g = ground_height(schematic)
    schematic = schematic[(g or 0) :, :, :, :]
    ys, zs, xs = np.nonzero(schematic[:, :, :, 0] > 0)

    xmid, ymid, zmid = np.mean(xs), np.mean(ys), np.mean(zs)
    focus = np.array([xmid, ymid + 63, zmid])  # TODO: +63 only works for flat_world seed=0w

    yaw, distance = list(map(int, npy_p2b.split(".")[-2].split("_")))

    look = [yaw, 0]
    look_xyz = to_unit_vec(*look)
    camera = focus - (look_xyz * distance)

    if mn == [0, 0]:
        M, N = p2b.shape[:2]
        while True:
            m = random.randint(0, M - 1)
            n = random.randint(0, N - 1)
            if p2b[m][n][0] != -1:
                break
    else:
        m, n = mn

    print("Select pixel at {}".format((m, n)))
    print("Mapped block {}".format(p2b[m][n]))
    change_block(schematic, p2b[m][n])

    logging.info("Launching cuberite at port {}".format(port))
    p = CuberiteProcess(
        "flat_world", seed=0, game_mode="creative", place_blocks_yzx=schematic, port=port
    )
    logging.info("Destroying cuberite at port {}".format(port))
    p.destroy()

    world_dir = os.path.join(p.workdir, "world")

    render_view_bin = os.path.join(repo_home, "bin/render_view")
    assert os.path.isfile(
        render_view_bin
    ), "{} not found.\n\nTry running: make render_view".format(render_view_bin)

    procs = []

    chunky_id = "{}_{}".format(yaw, distance)
    out_file = "{}/chunky_verify.{}.{}.png".format(out_dir, house_name, chunky_id)

    call = [
        str(a)
        for a in [
            "python3",
            "{}/../minecraft_render/render.py".format(repo_home),
            "--world",
            world_dir,
            "--out",
            out_file,
            "--camera",
            *camera,
            "--look",
            yaw,
            0,
            "--size",
            *img_size,
            "--spp",
            spp,
        ]
    ]
    logging.info("CALL: " + " ".join(call))
    procs.append(subprocess.Popen(call))

    for p in procs:
        p.wait()

    ## draw the sampled pixel for a better view
    img = cv2.imread(out_file)
    cv2.circle(img, (n, m), 2, (255, 0, 0))
    cv2.imwrite(out_file, img)
Beispiel #3
0
def render(npy_file, out_dir, world, seed, no_chunky, no_vision, port,
           distance, yaws, spp, img_size):
    """This function renders the npy_file in the world using cuberite"""

    # step 1: create the world with Cuberite, with npy blocks placed
    logging.info("Launching cuberite at port {}".format(port))
    if npy_file != "":
        schematic = np.load(npy_file)
        p = CuberiteProcess(world,
                            seed=0,
                            game_mode="creative",
                            place_blocks_yzx=schematic,
                            port=port)
    else:
        schematic = None
        p = CuberiteProcess(world,
                            seed=0,
                            game_mode="creative",
                            port=port,
                            plugins=["debug", "spawn_position"])
    logging.info("Destroying cuberite at port {}".format(port))
    p.destroy()

    world_dir = os.path.join(p.workdir, "world")

    print("==========================")
    print("WORLD directory:", world_dir)
    print("==========================")

    region_dir = os.path.join(world_dir, "region")
    mca_files = glob.glob(os.path.join(region_dir, "*.mca"))
    assert len(mca_files) > 0, "No region files at {}".format(region_dir)

    # step 2: render view binary
    render_view_bin = os.path.join(repo_home, "bin/render_view")
    assert os.path.isfile(
        render_view_bin
    ), "{} not found.\n\nTry running: make render_view".format(render_view_bin)

    if schematic is not None:
        # render a numpy object, set focus and distance
        # remove blocks below ground-level
        g = ground_height(schematic)
        schematic = schematic[(g or 0):, :, :, :]

        ymax, zmax, xmax, _ = schematic.shape
        ymid, zmid, xmid = ymax // 2, zmax // 2, xmax // 2
        focus = np.array([xmid, ymid + 63,
                          zmid])  # TODO: +63 only works for flat_world seed=0

        if distance is None:
            distance = int((xmax**2 + zmax**2)**0.5)
    else:
        f = open(p.workdir + "/spawn.txt", "r")
        playerx, playery, playerz = [float(i) for i in f.read().split("\n")]
        f.close()

        print("Spawn position:", playerx, playery, playerz)

    procs = []
    if yaws is None:
        yaws = range(0, 360, 90)
    for yaw in yaws:
        look = [yaw, 0]
        look_xyz = to_unit_vec(*look)

        if schematic is not None:
            camera = focus - (look_xyz * distance)
        else:
            camera = np.array([playerx, playery + 1.6, playerz])

        if not no_vision:
            call = [
                render_view_bin,
                "--out-prefix",
                out_dir + "/vision.%d" % yaw,
                "--mca-files",
                *mca_files,
                "--camera",
                *camera,
                "--sizes",
                *img_size,
                "--look",
                yaw,
                0,
                "--block",
                1,
                "--depth",
                1,
                "--blockpos",
                1,
            ]
            call = list(map(str, call))

            logging.info("CALL: " + " ".join(call))
            procs.append(subprocess.Popen(call))

        if not no_chunky:
            call = [
                "python",
                "{}/python/minecraft_render/render.py".format(repo_home),
                "--world",
                world_dir,
                "--out",
                "{}/chunky.{}.png".format(out_dir, yaw),
                "--camera",
                *camera,
                "--look",
                *look,
                "--size",
                *img_size,
                "--spp",
                spp,
                "--chunk-min",
                -10,
                "--chunk-max",
                10,
            ]
            call = list(map(str, call))
            logging.info("CALL: " + " ".join(call))
            procs.append(subprocess.Popen(call))

    for p in procs:
        p.wait()
Beispiel #4
0
def render(
    npy_file,
    out_dir,
    no_chunky,
    no_vision,
    port,
    yaw,
    pitch,
    camera,
    pos,
    spp,
    img_size,
    block_change,
):

    schematic = np.load(npy_file)
    house_name = os.path.basename(os.path.dirname(npy_file))

    # remove blocks below ground-level
    g = ground_height(schematic)
    schematic = schematic[(g or 0):, :, :, :]

    if yaw is None:
        yaw = random.randint(0, 360 - 1)

    if block_change:
        pitch, camera, pos = change_one_block(schematic, yaw)
        # TODO: +63 only works for flat_world seed=0
        camera[1] += 63  ## why??

    logging.info("Launching cuberite at port {}".format(port))
    p = CuberiteProcess("flat_world",
                        seed=0,
                        game_mode="creative",
                        place_blocks_yzx=schematic,
                        port=port)
    logging.info("Destroying cuberite at port {}".format(port))
    p.destroy()

    world_dir = os.path.join(p.workdir, "world")
    region_dir = os.path.join(world_dir, "region")
    mca_files = glob.glob(os.path.join(region_dir, "*.mca"))
    assert len(mca_files) > 0, "No region files at {}".format(region_dir)

    render_view_bin = os.path.join(repo_home, "bin/render_view")
    assert os.path.isfile(
        render_view_bin
    ), "{} not found.\n\nTry running: make render_view".format(render_view_bin)

    procs = []
    if not no_vision:
        call = [
            str(a) for a in [
                render_view_bin,
                "--out-dir",
                out_dir,
                "--mca-files",
                *mca_files,
                "--camera",
                *camera,
                "--look",
                yaw,
                pitch,
            ]
        ]
        logging.info("CALL: " + " ".join(call))
        procs.append(subprocess.Popen(call))

    if not no_chunky:
        chunky_id = "_".join(
            map(str,
                list(map(int, camera)) + [pitch, yaw] + pos))
        call = [
            str(a) for a in [
                "python3",
                "{}/minecraft_render/render.py".format(repo_home),
                "--world",
                world_dir,
                "--out",
                "{}/chunky.{}.{}.{}.png".format(out_dir, house_name, chunky_id,
                                                int(block_change)),
                "--camera",
                *camera,
                "--look",
                yaw,
                pitch,
                "--size",
                *img_size,
                "--spp",
                spp,
            ]
        ]
        logging.info("CALL: " + " ".join(call))
        procs.append(subprocess.Popen(call))

    for p in procs:
        p.wait()

    return yaw, pitch, camera, pos
Beispiel #5
0
"""
Copyright (c) Facebook, Inc. and its affiliates.
"""

import random
import atexit

from agent import Agent
from cuberite_process import CuberiteProcess

random.seed(0)

p = CuberiteProcess("flat_world", seed=0, game_mode="creative")
atexit.register(
    lambda: p.destroy())  # close Cuberite server when this script exits

agent = Agent("localhost", 25565, "example_bot")

# Random walk forever
while True:
    random.choice([
        agent.step_pos_x, agent.step_pos_z, agent.step_neg_x, agent.step_neg_z
    ])()
def render(npy_file, out_dir, port, spp, img_size):
    no_chunky = "p2b" in npy_file

    if no_chunky:  ## we're going to re-compute the correspondence
        npy_file = os.path.basename(npy_file)
        tokens = npy_file.split(".")
        yaw, distance = list(map(int, tokens[-2].split("_")))
        npy_file = (os.path.expanduser("~") + "/minecraft_houses/" +
                    ".".join(tokens[1:-2]) + "/schematic.npy")
    else:
        yaw, distance = None, None

    schematic = np.load(npy_file)
    house_name = os.path.basename(os.path.dirname(npy_file))

    # remove blocks below ground-level
    g = ground_height(schematic)
    schematic = schematic[(g or 0):, :, :, :]
    Y, Z, X, _ = schematic.shape
    ys, zs, xs = np.nonzero(schematic[:, :, :, 0] > 0)

    if len(ys) < 5:
        print("too few non-air blocks; will not render")
        return

    xmid, ymid, zmid = np.mean(xs), np.mean(ys), np.mean(zs)
    focus = np.array([xmid, ymid + y_offset,
                      zmid])  # TODO: +63 only works for flat_world seed=0w

    if yaw is None:
        yaw = random.randint(0, 360 - 1)
        sorted_xs = sorted(xs)
        sorted_zs = sorted(zs)
        N = len(xs)
        ## remove head and tail 2%
        X = sorted_xs[-N // 100] - sorted_xs[N // 100]
        Z = sorted_zs[-N // 100] - sorted_zs[N // 100]
        distance = max(X, Z)

    look = [yaw, 0]
    look_xyz = to_unit_vec(*look)
    camera = focus - (look_xyz * distance)

    logging.info("Launching cuberite at port {}".format(port))
    p = CuberiteProcess("flat_world",
                        seed=0,
                        game_mode="creative",
                        place_blocks_yzx=schematic,
                        port=port)
    logging.info("Destroying cuberite at port {}".format(port))
    p.destroy()

    world_dir = os.path.join(p.workdir, "world")
    region_dir = os.path.join(world_dir, "region")
    mca_files = glob.glob(os.path.join(region_dir, "*.mca"))
    assert len(mca_files) > 0, "No region files at {}".format(region_dir)

    render_view_bin = os.path.join(repo_home, "bin/render_view")
    assert os.path.isfile(
        render_view_bin
    ), "{} not found.\n\nTry running: make render_view".format(render_view_bin)

    procs = []

    chunky_id = "{}_{}".format(yaw, distance)
    out_file = "{}/chunky.{}.{}.png".format(out_dir, house_name, chunky_id)
    out_bin_prefix = "{}/{}.{}".format(out_dir, house_name, chunky_id)

    call = [
        str(a) for a in [
            render_view_bin,
            "--out-prefix",
            out_bin_prefix,
            "--mca-files",
            *mca_files,
            "--camera",
            *camera,
            "--sizes",
            *img_size,
            "--look",
            yaw,
            0,
            "--block",
            0,
            "--depth",
            0,
            "--blockpos",
            1,
        ]
    ]
    logging.info("CALL: " + " ".join(call))
    procs.append(subprocess.Popen(call))

    if not no_chunky:
        ## when re-computing the
        call = [
            str(a) for a in [
                "python3",
                "{}/../minecraft_render/render.py".format(repo_home),
                "--world",
                world_dir,
                "--out",
                out_file,
                "--camera",
                *camera,
                "--look",
                yaw,
                0,
                "--size",
                *img_size,
                "--spp",
                spp,
            ]
        ]
        logging.info("CALL: " + " ".join(call))
        procs.append(subprocess.Popen(call))

    for p in procs:
        p.wait()

    ## read the output blockpos bin and convert it to a npy file
    p2b = read_pixel2block(out_bin_prefix + ".blockpos.bin", X, Y, Z,
                           img_size[0], img_size[1])
    os.system("rm -f {}".format(out_bin_prefix + ".blockpos.bin"))

    ## write the correspondence to disk
    ## x-y-z is after applying ground_height
    p2b_file = "{}/p2b.{}.{}.npy".format(out_dir, house_name, chunky_id)
    np.save(p2b_file, p2b)
def render(npy_file, out_dir, port, spp, img_size):
    if "p2b" in npy_file:  ## we're going to re-compute the correspondence
        npy_file = os.path.basename(npy_file)
        tokens = npy_file.split(".")
        yaw, distance = list(map(int, tokens[-2].split("_")))
        npy_file = (os.path.expanduser("~") + "/minecraft_houses/" +
                    ".".join(tokens[1:-2]) + "/schematic.npy")
    else:
        yaw, distance = None, None

    schematic = np.load(npy_file)
    house_name = os.path.basename(os.path.dirname(npy_file))

    # remove blocks below ground-level
    g = ground_height(schematic)
    schematic = schematic[(g or 0):, :, :, :]
    ys, zs, xs = np.nonzero(schematic[:, :, :, 0] > 0)

    if len(ys) < 5:
        print("too few non-air blocks; will not render")
        return

    xmid, ymid, zmid = np.mean(xs), np.mean(ys), np.mean(zs)
    focus = np.array([xmid, ymid + 63,
                      zmid])  # TODO: +63 only works for flat_world seed=0w

    if yaw is None:
        yaw = random.randint(0, 360 - 1)
        sorted_xs = sorted(xs)
        sorted_zs = sorted(zs)
        N = len(xs)
        ## remove head and tail 2%
        X = sorted_xs[-N // 100] - sorted_xs[N // 100]
        Z = sorted_zs[-N // 100] - sorted_zs[N // 100]
        distance = max(X, Z)

    look = [yaw, 0]
    look_xyz = to_unit_vec(*look)
    camera = focus - (look_xyz * distance)

    repeat = 10
    schematic_hue = np.zeros(schematic.shape[:3] + (repeat - 1, ),
                             dtype=np.int32)
    tmp_images = []
    for i in range(repeat):
        if i < repeat - 1:
            new_schematic = randomly_change_blocks(schematic)
            add_new_schematic_hue(schematic_hue, new_schematic[:, :, :, 1], i)
        else:
            break  # do not render the full image again
            new_schematic = schematic
            img_size = [s * 3 for s in img_size]

        logging.info("Launching cuberite at port {}".format(port))
        p = CuberiteProcess("flat_world",
                            seed=0,
                            game_mode="creative",
                            place_blocks_yzx=new_schematic,
                            port=port)
        logging.info("Destroying cuberite at port {}".format(port))
        p.destroy()

        world_dir = os.path.join(p.workdir, "world")

        render_view_bin = os.path.join(repo_home, "bin/render_view")
        assert os.path.isfile(
            render_view_bin
        ), "{} not found.\n\nTry running: make render_view".format(
            render_view_bin)

        procs = []

        chunky_id = "{}_{}".format(yaw, distance)
        out_file = "{}/chunky.{}.{}.png".format(out_dir, house_name, chunky_id)
        if i < repeat - 1:  # tmp out file
            out_file += ".tmp.png"

        call = [
            str(a) for a in [
                "python3",
                "{}/../minecraft_render/render.py".format(repo_home),
                "--world",
                world_dir,
                "--out",
                out_file,
                "--camera",
                *camera,
                "--look",
                yaw,
                0,
                "--size",
                *img_size,
                "--spp",
                spp,
            ]
        ]
        logging.info("CALL: " + " ".join(call))
        procs.append(subprocess.Popen(call))

        for p in procs:
            p.wait()

        if i < repeat - 1:
            tmp_images.append(cv2.imread(out_file))
            os.system("rm -f " + out_file)  ## delete the tmp image

    ## now we need to compute the pixel-to-block correspondence
    p2b = pixel2block(tmp_images, schematic_hue)

    ## write the correspondence to disk
    ## x-y-z is after applying ground_height
    p2b_file = "{}/p2b.{}.{}.npy".format(out_dir, house_name, chunky_id)
    np.save(p2b_file, p2b)