Exemplo n.º 1
0
def main(args):
    cfg = setup(args)
    print("DATASETS: ", cfg.DATASETS.TRAIN)
    loader = build_x_train_loader(cfg)
    for i, items in tqdm.tqdm(enumerate(loader)):
        if i % 128 != 0:
            continue
        save_dir = Path(cfg.OUTPUT_DIR) / f"{items[0]['image_id']}"
        save_dir.mkdir(exist_ok=True, parents=True)
        write_jpeg(items[0]['image'], str(save_dir / f'img{i}.jpg'))
        for j, mask in enumerate(items[0]['instances'].gt_masks):
            Image.fromarray(mask.cpu().numpy()).save(save_dir / f'mask{j}.png')
Exemplo n.º 2
0
def test_write_jpeg(img_path, tmpdir):
    tmpdir = Path(tmpdir)
    img = read_image(img_path)
    pil_img = F.to_pil_image(img)

    torch_jpeg = str(tmpdir / "torch.jpg")
    pil_jpeg = str(tmpdir / "pil.jpg")

    write_jpeg(img, torch_jpeg, quality=75)
    pil_img.save(pil_jpeg, quality=75)

    with open(torch_jpeg, "rb") as f:
        torch_bytes = f.read()

    with open(pil_jpeg, "rb") as f:
        pil_bytes = f.read()

    assert_equal(torch_bytes, pil_bytes)
Exemplo n.º 3
0
def test_write_jpeg_reference(img_path, tmpdir):
    # FIXME: Remove this eventually, see test_encode_jpeg_reference
    data = read_file(img_path)
    img = decode_jpeg(data)

    basedir = os.path.dirname(img_path)
    filename, _ = os.path.splitext(os.path.basename(img_path))
    torch_jpeg = os.path.join(tmpdir, f"{filename}_torch.jpg")
    pil_jpeg = os.path.join(basedir, "jpeg_write", f"{filename}_pil.jpg")

    write_jpeg(img, torch_jpeg, quality=75)

    with open(torch_jpeg, "rb") as f:
        torch_bytes = f.read()

    with open(pil_jpeg, "rb") as f:
        pil_bytes = f.read()

    assert_equal(torch_bytes, pil_bytes)
def test_write_jpeg(img_path):
    with get_tmp_dir() as d:
        d = Path(d)
        img = read_image(img_path)
        pil_img = F.to_pil_image(img)

        torch_jpeg = str(d / 'torch.jpg')
        pil_jpeg = str(d / 'pil.jpg')

        write_jpeg(img, torch_jpeg, quality=75)
        pil_img.save(pil_jpeg, quality=75)

        with open(torch_jpeg, 'rb') as f:
            torch_bytes = f.read()

        with open(pil_jpeg, 'rb') as f:
            pil_bytes = f.read()

        assert_equal(torch_bytes, pil_bytes)
Exemplo n.º 5
0
    def test_write_jpeg(self):
        for img_path in get_images(IMAGE_ROOT, ".jpg"):
            img = read_jpeg(img_path)

            basedir = os.path.dirname(img_path)
            filename, _ = os.path.splitext(os.path.basename(img_path))
            torch_jpeg = os.path.join(basedir,
                                      '{0}_torch.jpg'.format(filename))
            pil_jpeg = os.path.join(basedir, 'jpeg_write',
                                    '{0}_pil.jpg'.format(filename))

            write_jpeg(img, torch_jpeg, quality=75)

            with open(torch_jpeg, 'rb') as f:
                torch_bytes = f.read()

            with open(pil_jpeg, 'rb') as f:
                pil_bytes = f.read()

            os.remove(torch_jpeg)
            self.assertEqual(torch_bytes, pil_bytes)
Exemplo n.º 6
0
    def test(self):
        multiplier = 4
        path = "/Users/amorvan/Documents/code_dw/neural-styles/images/bw_svg_neural_style/" \
               "result_n_paths200_im_size224_n_steps500_layer_nameVGGLayers.Conv4_3_layer_index2.svg"
        s = ScaledSvgGen(path, multiplier, multiplier)
        f = s.gen_func()

        shapes, shape_groups = f()

        self.assertEqual(len(shapes), multiplier * multiplier * 200)
        self.assertTrue(any(np.max(shape.points.numpy()) < multiplier * 224 for shape in shapes))

        render = pydiffvg.RenderFunction.apply
        scene_args = pydiffvg.RenderFunction.serialize_scene( \
            multiplier * 224, multiplier * 224, shapes, shape_groups)
        img = render(multiplier * 224, multiplier * 224, 2, 2, 0,
                     torch.ones((multiplier * 224, multiplier * 224, 4)), *scene_args)

        print(img.shape)

        write_jpeg(255 * img.permute(2, 0, 1)[:3, :, :].to(torch.uint8), "test.jpg")
Exemplo n.º 7
0
    def test_write_jpeg(self):
        with get_tmp_dir() as d:
            for img_path in get_images(ENCODE_JPEG, ".jpg"):
                data = read_file(img_path)
                img = decode_jpeg(data)

                basedir = os.path.dirname(img_path)
                filename, _ = os.path.splitext(os.path.basename(img_path))
                torch_jpeg = os.path.join(d, '{0}_torch.jpg'.format(filename))
                pil_jpeg = os.path.join(basedir, 'jpeg_write',
                                        '{0}_pil.jpg'.format(filename))

                write_jpeg(img, torch_jpeg, quality=75)

                with open(torch_jpeg, 'rb') as f:
                    torch_bytes = f.read()

                with open(pil_jpeg, 'rb') as f:
                    pil_bytes = f.read()

                self.assertEqual(torch_bytes, pil_bytes)
def test_write_jpeg_reference(img_path):
    # FIXME: Remove this eventually, see test_encode_jpeg_reference
    with get_tmp_dir() as d:
        data = read_file(img_path)
        img = decode_jpeg(data)

        basedir = os.path.dirname(img_path)
        filename, _ = os.path.splitext(os.path.basename(img_path))
        torch_jpeg = os.path.join(d, '{0}_torch.jpg'.format(filename))
        pil_jpeg = os.path.join(basedir, 'jpeg_write',
                                '{0}_pil.jpg'.format(filename))

        write_jpeg(img, torch_jpeg, quality=75)

        with open(torch_jpeg, 'rb') as f:
            torch_bytes = f.read()

        with open(pil_jpeg, 'rb') as f:
            pil_bytes = f.read()

        assert_equal(torch_bytes, pil_bytes)
from torchvision.io.video import read_video, write_video
from torchvision.io.image import write_jpeg
import torch
from args_util import meow_parse
from data_flow import get_predict_video_dataloader
from models import create_model
import os
from visualize_util import save_density_map_normalize, save_density_map

VIDEO_PATH = "/home/tt/Videos/VID_20201204_133931_404.mp4"
OUTPUT_PATH = "/data/my_crowd_image/video_bike_q100"
v, a, info = read_video(VIDEO_PATH, pts_unit='sec')
print(info)
print(v.shape)
length = v.shape[0]
print(length)

count = 0
for i in range(length):
    # if (i% 20 == 0):
    frame = v[i]
    frame = frame.permute(2, 0, 1)
    file_out_path = os.path.join(OUTPUT_PATH, "IMG_" + str(i) + ".jpg")
    write_jpeg(frame, file_out_path, quality=100)
    print(file_out_path)