Exemple #1
0
 def done(self):
     Path(self.directory).mkdir(parents=True, exist_ok=True)
     stream = torch.from_numpy(np.stack(self.t))
     for image in stream:
         write_png(image.permute(2, 0, 1),
                   f'{self.directory}/{self.image_id}.png')
         self.image_id += 1
Exemple #2
0
def example_load_frame():
    v, a, info = read_video("/home/tt/Videos/VID_20201202_133703_090.mp4",
                            pts_unit='sec')
    print(v.shape)  # torch.Size([467, 1080, 1920, 3])
    # write a frame
    single_frame = v[100]
    print(single_frame.shape)  # torch.Size([1080, 1920, 3])
    single_frame = single_frame.permute(2, 0, 1)  # to CHW
    print(single_frame.shape)
    file_out = os.path.join(out_path, "single_frame.png")
    write_png(single_frame, file_out)
    print("done write to ", file_out)
def generate_image(feature, save_as_numpy=False):
    save_as_numpy = feature.get("save_as_numpy", save_as_numpy)

    try:
        from torchvision.io import write_png
    except ImportError:
        logger.error(" torchvision is not installed. "
                     "In order to install all image feature dependencies run "
                     "pip install ludwig[image]")
        sys.exit(-1)

    # Read num_channels, width, height
    destination_folder = feature.get("destination_folder", "image_files")
    if PREPROCESSING in feature:
        height = feature[PREPROCESSING].get("height", 28)
        width = feature[PREPROCESSING].get("width", 28)
        num_channels = feature[PREPROCESSING].get("num_channels", 1)
    else:
        height = feature.get("height", 28)
        width = feature.get("width", 28)
        num_channels = feature.get("num_channels", 1)

    if width <= 0 or height <= 0 or num_channels < 1:
        raise ValueError("Invalid arguments for generating images")

    # Create a Random Image
    img = torch.randint(0,
                        255, (num_channels, width, height),
                        dtype=torch.uint8)

    # Generate a unique random filename
    image_filename = uuid.uuid4().hex[:10].upper() + ".png"

    # Save the image to disk either in a specified location/new folder
    try:
        if not os.path.exists(destination_folder):
            os.makedirs(destination_folder)

        image_dest_path = os.path.join(destination_folder, image_filename)
        # save_image(torch.from_numpy(img.astype("uint8")), image_dest_path)
        if save_as_numpy:
            with open(image_dest_path, "wb") as f:
                np.save(f, img.detach().cpu().numpy())
        else:
            write_png(img, image_dest_path)

    except OSError as e:
        raise OSError(
            "Unable to create a folder for images/save image to disk."
            "{}".format(e))

    return image_dest_path
Exemple #4
0
# Blog: https://blog.csdn.net/fengbingchun/article/details/121191194

# 下载MNIST数据集: torchvision.datasets包
test = datasets.MNIST("../../data", train=False, download=True)
train = datasets.MNIST("../../data", train=True, download=False)
print(f"raw_folder: test: {test.raw_folder}, train: {train.raw_folder}")
print(
    f"processed_folder: test: {test.processed_folder}, train: {train.processed_folder}"
)
print(f"extra_repr:\ntest: {test.extra_repr}\ntrain: {train.extra_repr}")
print(f"class to index: {test.class_to_idx}")

# 读写图像: torchvision.io包
tensor = io.read_image("../../data/image/1.jpg")
print("tensor shape:", tensor.shape)
io.write_png(tensor, "../../data/image/result.png")

tensor = io.read_image("../../data/image/lena.png")
print("tensor shape:", tensor.shape)
io.write_jpeg(tensor, "../../data/image/result.jpg")

# 下载pre-trained AlexNet模型: torchvision.models包
net = models.alexnet(pretrained=True)

# 计算机视觉操作: torchvision.ops包
boxes = torch.tensor([[1, 1, 101, 101], [3, 5, 13, 15], [2, 4, 22, 44]])
area = ops.box_area(boxes)
print(f"area: {area}")

index = ops.remove_small_boxes(boxes, min_size=20)
print(f"index: {index}")
print("density_map_tensor.shape",
      density_map_tensor.shape)  # torch.Size([1, 1, 46, 82])
# module = nn.UpsamplingBilinear2d(scale_factor=8)
# upsampling_density_map_tensor = module(density_map_tensor)
upsampling_density_map_tensor = nn.functional.interpolate(density_map_tensor,
                                                          scale_factor=8) / 64
print(upsampling_density_map_tensor.sum())
print(upsampling_density_map_tensor.shape)

pad_density_map_tensor = torch.zeros(
    (1, 3, img_tensor.shape[1], img_tensor.shape[2]))
pad_density_map_tensor[:, 0, :upsampling_density_map_tensor.
                       shape[2], :upsampling_density_map_tensor.
                       shape[3]] = upsampling_density_map_tensor
print(pad_density_map_tensor.shape)
pad_density_map_tensor = (pad_density_map_tensor.squeeze(dim=0) /
                          pad_density_map_tensor.max() * 255)
# pad_density_map_tensor = pad_density_map_tensor.squeeze(dim=0)

print(img_tensor.dtype)
print(pad_density_map_tensor.dtype)

overlay_density_map = img_tensor.detach().clone()
overlay_density_map[0] = torch.clamp_max(img_tensor[0] +
                                         pad_density_map_tensor[0] * 2,
                                         max=255)

write_png(overlay_density_map.type(torch.uint8),
          "../visualize/pic/overlay.png")
write_png(pad_density_map_tensor.type(torch.uint8), "../visualize/pic/pad.png")