Exemplo n.º 1
0
def batch_show(batch,
               nr_show=16,
               grid_desc=('4v', '4h'),
               resize=(600, 800),
               title='batch_show'):
    """
    Show a batch of images.

    :param batch: The batched data: can be either a ndarray of shape (batch_size, h, w, c) or a list
    of images.
    :param nr_show: Number of images to be displayed. Default set to be 16.
    :param grid_desc: Grid description. See `tartist.image.image_grid` for details.
    :param resize: Resize factor, a tuple (min_dim, max_dim).
    :param title: The title of the shown window.
    """

    batch = batch[:nr_show]
    batch = np.array(batch)

    if len(batch) < 16:
        batch = np.concatenate([
            batch,
            np.zeros([
                16 - len(batch), batch.shape[1], batch.shape[2], batch.shape[3]
            ],
                     dtype=batch.dtype)
        ],
                               axis=0)

    img = image.image_grid(batch, grid_desc)
    img = image.resize_minmax(img, *resize, interpolation='NEAREST')
    image.imshow(title, img)
Exemplo n.º 2
0
def vis_and_action(args, cfg, controller, observation):
    # here, the observation is actually a state
    if hasattr(cfg, 'observe'):
        observation = cfg.observe(observation)
    display = image.resize_minmax(observation,
                                  *args.winsize,
                                  interpolation='NEAREST')
    controller.update(display)

    if args.fps > 0:
        action = 0
        for i in reversed(range(len(cfg.action_keys))):
            if controller.test_key(cfg.action_keys[i]):
                action = i
                break

        if controller.test_key(cfg.quit_action_key):
            action = __quit_action__

        time.sleep((1.0 / args.fps))
    else:
        action = 0
        key = controller.get_last_key()

        for i in reversed(range(len(cfg.action_keys))):
            if key == cfg.action_keys[i]:
                action = i
                break

        if key == cfg.quit_action_key:
            action = __quit_action__

    return action
Exemplo n.º 3
0
def demo(feed_dict, result, extra_info):
    img = result['output'][0, :, :, 0]

    img = np.repeat(img[:, :, np.newaxis], 3, axis=2) * 255
    img = img.astype('uint8')
    img = image.resize_minmax(img, 256)

    image.imshow('demo', img)
Exemplo n.º 4
0
def main(args, controller):
    if args.output is None:
        args.output = '{}.conf.py'.format(args.gamename)

    if osp.isfile(args.output):
        logger.warn('Output config file already exists: {}.'.format(
            args.output))
        if not yes_or_no('Do you want to overwrite?', default='no'):
            controller.quit()
            return

    game = automake(args.gamename)
    game.restart()

    action_space = game.action_space
    assert isinstance(action_space, DiscreteActionSpace)

    action_names = action_space.action_meanings
    action_keys = []

    logger.critical('All action names: {}'.format(action_names))
    logger.critical('Start recording...')

    img = image.resize_minmax(game.current_state,
                              *args.winsize,
                              interpolation='NEAREST')
    controller.update_title(args.gamename)
    controller.update(img)

    for i in range(len(action_names)):
        name = action_names[i]
        logger.info('{}-th action, name={}, waiting...'.format(i, name))
        action = controller.get_last_key()
        action_keys.append(action)
        logger.info('{}-th action, name={}, key={}, done.'.format(
            i, name, repr(action)))
    logger.critical('Recording end.')

    logger.info('Recoding quit action key')
    quit_key = controller.get_last_key()
    logger.info('quit action, key={}, done.'.format(repr(quit_key)))
    controller.quit()

    with open(args.output, 'w') as f:
        f.write('# -*- coding:utf8 -*-\n\n')
        f.write(__automake_format__.format(args.gamename))
        f.write("name = '{}'\n".format(args.gamename))
        f.write("action_names = {}\n".format(repr(action_names)))
        f.write("action_keys = {}\n".format(repr(action_keys)))
        f.write("quit_action_key = {}\n".format(repr(quit_key)))
        f.write("\n")
        f.write("# default min size is a tuple (min_dim, max_dim)\n")
        f.write("default_winsize = (600, 800)\n")
        f.write("# ignore no-action (action=0) in logging\n")
        f.write("mute_noaction = True\n")
        f.write("\n")
    logger.critical('Cfg file written to {}'.format(args.output))
Exemplo n.º 5
0
def imshow(img, resize=(600, 800), title='imshow'):
    """
    Image show with different parameter order.

    :param img: Image.
    :param resize: Resize factor, a tuple (min_dim, max_dim).
    :param title: The title of the shown window.
    """
    img = image.resize_minmax(img, *resize, interpolation='NEAREST')
    image.imshow(title, img)
Exemplo n.º 6
0
def demo_vae(feed_dict, result, extra_info):
    reconstruct = get_env('demo.is_reconstruct', False)
    if reconstruct:
        img = feed_dict['img'][0, :, :, 0]
        omg = result['output'][0, :, :, 0]
        img = np.hstack((img, omg))
    else:
        img = result['output'][0, :, :, 0]

    img = np.repeat(img[:, :, np.newaxis], 3, axis=2) * 255
    img = img.astype('uint8')
    img = image.resize_minmax(img, 256)

    image.imshow('demo', img)
Exemplo n.º 7
0
def main():
    dbdir = osp.realpath(args.output_dir)
    io.mkdir(dbdir)
    db = LMDBKVStore(dbdir, readonly=False)

    files = glob.glob(osp.join(args.input_dir, '*.jpg'))

    with db.transaction():
        for f in tqdm(files):
            img = image.imread(f)
            fid = osp.basename(f)[:-4]
            img = image.resize_minmax(img, 256, 10000)
            img = image.center_crop(img, 256)
            db.put(fid, image.jpeg_encode(img))
Exemplo n.º 8
0
def demo(feed_dict, result, extra_info):
    img = feed_dict['img'][0, :, :, 0]
    label = extra_info['label']

    img = np.repeat(img[:, :, np.newaxis], 3, axis=2) * 255
    img = img.astype('uint8')
    img = image.resize_minmax(img, 256)
    outputs = [img, np.zeros(shape=[50, 256, 3], dtype='uint8')]
    outputs = np.vstack(outputs)

    text = 'Pred: {}'.format(result['pred'][0])
    text += ' Gt: {}'.format(int(label))
    # cv2.putText(outputs, text, (20, 256 + 25), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255))
    print(text)

    image.imshow('demo', outputs)
Exemplo n.º 9
0
def demo(feed_dict, result, extra_info):
    nr_classes = get_env('dataset.nr_classes')

    img = feed_dict['img'][0]
    label = extra_info['label']
    labels_name = _cifar_labels_name[nr_classes]

    img = img.astype('uint8')
    img = image.resize_minmax(img, 256)
    outputs = [img, np.zeros(shape=[50, 256, 3], dtype='uint8')]
    outputs = np.vstack(outputs)

    text = 'Pred: {}'.format(labels_name[result['pred'][0]])
    text += ' Gt: {}'.format(labels_name[int(label)])
    # cv2.putText(outputs, text, (20, 256 + 25), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255))
    print(text)

    image.imshow('demo', outputs)
Exemplo n.º 10
0
def demo_draw(feed_dict, result, extra_info):
    reconstruct = get_env('demo.is_reconstruct', False)
    grid_desc = get_env('demo.draw.grid_desc')

    all_outputs = []
    for i in range(1000):
        name = 'canvas_step{}'.format(i)
        if name in result:
            all_outputs.append(result[name][0, :, :, 0])

    final = image.image_grid(all_outputs, grid_desc)
    final = (final * 255).astype('uint8')

    if reconstruct:
        img = feed_dict['img'][0, :, :, 0]
        h = final.shape[0]
        w = int(img.shape[1] * h / img.shape[0])
        img = (img * 255).astype('uint8')
        img = image.resize(img, (h, w))
        final = np.hstack((img, final))

    final = image.resize_minmax(final, 480, 720)

    image.imshow('demo', final)
Exemplo n.º 11
0
def r(m):
    return np.pad(image.resize_minmax(m.current_state,
                                      128,
                                      interpolation='NEAREST'),
                  pad_width=[(10, 10), (10, 10), (0, 0)],
                  mode='constant')