修改 Python 类以过滤文件

Posted

技术标签:

【中文标题】修改 Python 类以过滤文件【英文标题】:Modify Python Class to filter files 【发布时间】:2022-01-19 23:17:58 【问题描述】:

python 新手。如何修改类以使用字符串过滤文件夹中的文件。现在它返回文件夹中的所有文件,其中可能是数百万个项目。以下工作但是我想隔离包含特定字符串的文件,例如,隔离所有包含'v_1234_frame'的文件:

# Image loader
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Lambda(lambda x: x.mul(255))
])
image_dataset = utils.ImageFolderWithPaths(folder_containing_the_content_folder, transform=transform)
image_loader = torch.utils.data.DataLoader(image_dataset, batch_size=batch_size)

有效的类需要修改以过滤包含“v_1234_frame”的文件名:

class ImageFolderWithPaths(datasets.ImageFolder):
"""Custom dataset that includes image file paths.
Extends torchvision.datasets.ImageFolder()
Reference: https://discuss.pytorch.org/t/dataloader-filenames-in-each-batch/4212/2
"""

# override the __getitem__ method. this is the method dataloader calls
def __getitem__(self, index):
    # this is what ImageFolder normally returns
    original_tuple = super(ImageFolderWithPaths, self).__getitem__(index)

    # the image file path
    path = self.imgs[index][0]

    # make a new tuple that includes original and the path
    tuple_with_path = (*original_tuple, path)
    return tuple_with_path

我正在学习 python,但似乎无法提出解决方案。希望您可以帮助/建议更改类或调用方法。

【问题讨论】:

【参考方案1】:

构建我自己的数据加载器以通过 glob 中的通配符模式隔离文件,然后循环遍历这些文件以为每个图像创建一个张量,将其传递给我的模型,该模型需要将其转换为浮点数。从路径中提取基本名称(图像名称)(例如 img_frame1.jpg)。将结果保存到我的样式文件夹。这种方法使我可以通过通配符完全控制文件。我已经包含了解决方案中使用的其他功能。注意:我没有使用 gpu 来处理这些,所以我可以在标准的 python web 服务器上运行它。希望这对将来的某人有所帮助。简单有时更好:)

# Load image file
# def load_image(path):
#     # Images loaded as BGR
#     img = cv2.imread(path)
#     return img

# def itot(img, max_size=None):
#     # Rescale the image
#     if (max_size == None):
#         itot_t = transforms.Compose([
#             # transforms.ToPILImage(),
#             transforms.ToTensor(),
#             transforms.Lambda(lambda x: x.mul(255))
#         ])
#     else:
#         H, W, C = img.shape
#         image_size = tuple([int((float(max_size) / max([H, W])) * x) for x in [H, W]])
#         itot_t = transforms.Compose([
#             transforms.ToPILImage(),
#             transforms.Resize(image_size),
#             transforms.ToTensor(),
#             transforms.Lambda(lambda x: x.mul(255))
#         ])
# 
#     # Convert image to tensor
#     tensor = itot_t(img)
# 
#     # Add the batch_size dimension
#     tensor = tensor.unsqueeze(dim=0)
#     return tensor

folder_data = glob.glob(folder_containing_the_content_folder + "content_folder/" +  video_token + "_frame*.jpg")

# image_dataset = utils.ImageFolderWithPaths(folder_containing_the_content_folder, transform)
# image_loader = torch.utils.data.DataLoader(image_dataset, batch_size=batch_size)

# Load Transformer Network
net = transformer.TransformerNetwork()
net.load_state_dict(torch.load(style_path))
net = net.to(device)

with torch.no_grad():
    for image_name in folder_data:
        img = utils.load_image(image_name)
        img = img / 255.0  # standardize the data/transform
        img_tensor = utils.itot(img)

        # style image tensor
        generated_tensor = net(img_tensor.float())
        
        # convert image the model modified tensor back to an image
        generated_image = utils.ttoi(generated_tensor)
        image_name = os.path.basename(image_name)
        
        # save generated image to folder
        utils.saveimg(generated_image, save_folder + image_name)

【讨论】:

以上是关于修改 Python 类以过滤文件的主要内容,如果未能解决你的问题,请参考以下文章

如何通过就地过滤来修改python集合?

如何在python中同时使用applymap、lambda和dataframe来过滤/修改dataframe?

MYSQL 从库过滤在线修改

过滤器的使用

iPhone 音频修改(过滤器/效果)

过滤器的使用