| import os |
| import cv2 |
| import torch |
| import torchvision.transforms as transforms |
| from torch.utils.data import Dataset |
| import utils |
|
|
|
|
| class AVLip(Dataset): |
| def __init__(self, opt): |
| assert opt.data_label in ["train", "val"] |
| self.data_label = opt.data_label |
| self.real_list = utils.get_list(opt.real_list_path) |
| self.fake_list = utils.get_list(opt.fake_list_path) |
| self.label_dict = dict() |
| for i in self.real_list: |
| self.label_dict[i] = 0 |
| for i in self.fake_list: |
| self.label_dict[i] = 1 |
| self.total_list = self.real_list + self.fake_list |
|
|
| def __len__(self): |
| return len(self.total_list) |
|
|
| def __getitem__(self, idx): |
| |
| tried_indices = set() |
| return self._get_item_with_skip(idx, tried_indices) |
| |
| def _get_item_with_skip(self, idx, tried_indices): |
| |
| if len(tried_indices) >= len(self.total_list): |
| raise RuntimeError("All samples are corrupted or cannot be read!") |
| |
| tried_indices.add(idx) |
| img_path = self.total_list[idx] |
| label = self.label_dict[img_path] |
| |
| |
| try: |
| |
| if not os.path.exists(img_path): |
| print(f"WARNING: File not found, skipping: {img_path}") |
| |
| return self._get_item_with_skip((idx + 1) % len(self.total_list), tried_indices) |
| |
| |
| img_cv = cv2.imread(img_path) |
| if img_cv is None: |
| print(f"WARNING: Failed to read image, skipping: {img_path}") |
| |
| return self._get_item_with_skip((idx + 1) % len(self.total_list), tried_indices) |
| |
| img = torch.tensor(img_cv, dtype=torch.float32) |
| img = img.permute(2, 0, 1) |
| except Exception as e: |
| print(f"WARNING: Error processing {img_path}: {e}, skipping...") |
| |
| return self._get_item_with_skip((idx + 1) % len(self.total_list), tried_indices) |
| crops = transforms.Normalize(mean=[0.48145466, 0.4578275, 0.40821073], |
| std=[0.26862954, 0.26130258, 0.27577711])(img) |
| |
| |
| |
| |
| |
| |
| crops = [[transforms.Resize((224, 224))(img[:, 500:, i*500:(i+1)*500]) for i in range(5)], [], []] |
| crop_idx = [(28, 196), (61, 163)] |
| for i in range(len(crops[0])): |
| crops[1].append(transforms.Resize((224, 224)) |
| (crops[0][i][:, crop_idx[0][0]:crop_idx[0][1], crop_idx[0][0]:crop_idx[0][1]])) |
| crops[2].append(transforms.Resize((224, 224)) |
| (crops[0][i][:, crop_idx[1][0]:crop_idx[1][1], crop_idx[1][0]:crop_idx[1][1]])) |
| img = transforms.Resize((1120, 1120))(img) |
|
|
| return img, crops, label, img_path |
|
|