Spaces:
Sleeping
Sleeping
upload dataset module
Browse files- dataset/__init__.py +0 -0
- dataset/val_dataset.py +130 -0
dataset/__init__.py
ADDED
|
File without changes
|
dataset/val_dataset.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
from torch.utils.data import Dataset
|
| 7 |
+
import torchvision.transforms as T
|
| 8 |
+
|
| 9 |
+
from transformers import CLIPImageProcessor
|
| 10 |
+
|
| 11 |
+
import sys
|
| 12 |
+
sys.path.append("/path/to/FollowYourEmoji")
|
| 13 |
+
from media_pipe import FaceMeshDetector, FaceMeshAlign
|
| 14 |
+
from media_pipe.draw_util import FaceMeshVisualizer
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def val_collate_fn(samples):
|
| 18 |
+
return {
|
| 19 |
+
'ref_frame': [sample['ref_frame'] for sample in samples],
|
| 20 |
+
'clip_image': [sample['clip_image'] for sample in samples],
|
| 21 |
+
'motions': [sample['motions'] for sample in samples],
|
| 22 |
+
'file_name': [sample['file_name'] for sample in samples],
|
| 23 |
+
'lmk_name': [sample['lmk_name'] for sample in samples],
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class ValDataset(Dataset):
|
| 28 |
+
def __init__(self, input_path, lmk_path, resolution_w=512, resolution_h=512):
|
| 29 |
+
|
| 30 |
+
print(f'Loading dataset from {input_path} and {lmk_path}')
|
| 31 |
+
|
| 32 |
+
all_img_paths = self._get_path_files(Path(input_path), file_suffix=['.jpg', '.jpeg', '.png', '.webp'])
|
| 33 |
+
all_lmk_paths = self._get_path_files(Path(lmk_path), file_suffix=['.npy'])
|
| 34 |
+
|
| 35 |
+
print(f'Found {len(all_img_paths)} image files and {len(all_lmk_paths)} lmk files')
|
| 36 |
+
print(f"ALL IMG PATH: {all_img_paths}")
|
| 37 |
+
print(f"ALL LKM PATH: {all_lmk_paths}")
|
| 38 |
+
self.all_paths = []
|
| 39 |
+
for lmk_path in all_lmk_paths:
|
| 40 |
+
for img_path in all_img_paths:
|
| 41 |
+
self.all_paths.append((img_path, lmk_path))
|
| 42 |
+
|
| 43 |
+
self.W = resolution_w
|
| 44 |
+
self.H = resolution_h
|
| 45 |
+
self.to_tensor = T.ToTensor()
|
| 46 |
+
|
| 47 |
+
self.detector = FaceMeshDetector()
|
| 48 |
+
self.aligner = FaceMeshAlign()
|
| 49 |
+
|
| 50 |
+
self.clip_image_processor = CLIPImageProcessor()
|
| 51 |
+
self.vis = FaceMeshVisualizer(forehead_edge=False, iris_edge=False, iris_point=True)
|
| 52 |
+
|
| 53 |
+
def __len__(self):
|
| 54 |
+
return len(self.all_paths)
|
| 55 |
+
|
| 56 |
+
def _get_path_files(self, path, file_suffix):
|
| 57 |
+
all_paths = []
|
| 58 |
+
if path.is_file():
|
| 59 |
+
if path.suffix.lower() in file_suffix:
|
| 60 |
+
all_paths = [path]
|
| 61 |
+
else:
|
| 62 |
+
raise ValueError('Path is not valid image file.')
|
| 63 |
+
elif path.is_dir():
|
| 64 |
+
all_paths = sorted(
|
| 65 |
+
[
|
| 66 |
+
f
|
| 67 |
+
for f in path.iterdir()
|
| 68 |
+
if f.is_file() and f.suffix.lower() in file_suffix
|
| 69 |
+
]
|
| 70 |
+
)
|
| 71 |
+
if len(all_paths) == 0:
|
| 72 |
+
raise ValueError('Folder does not contain any images.')
|
| 73 |
+
else:
|
| 74 |
+
raise ValueError
|
| 75 |
+
|
| 76 |
+
return all_paths
|
| 77 |
+
|
| 78 |
+
def get_align_motion(self, ref_lmk, temp_lmks):
|
| 79 |
+
motions = self.aligner(ref_lmk, temp_lmks)
|
| 80 |
+
motions = [self.to_tensor(motion) for motion in motions]
|
| 81 |
+
motions = torch.stack(motions).permute((1,0,2,3))
|
| 82 |
+
return motions
|
| 83 |
+
|
| 84 |
+
def __getitem__(self, index):
|
| 85 |
+
img_path, lmk_path = self.all_paths[index]
|
| 86 |
+
W, H = self.W, self.H
|
| 87 |
+
|
| 88 |
+
image = Image.open(img_path).convert('RGB')
|
| 89 |
+
|
| 90 |
+
# resize and center crop
|
| 91 |
+
scale = min(W / image.size[0], H / image.size[1])
|
| 92 |
+
ref_image = image.resize(
|
| 93 |
+
(int(image.size[0] * scale), int(image.size[1] * scale)))
|
| 94 |
+
w, h = ref_image.size[0], ref_image.size[1]
|
| 95 |
+
ref_image = ref_image.crop((w//2-W//2, h//2-H//2, w//2+W//2, h//2+H//2))
|
| 96 |
+
ref_image = np.array(ref_image)
|
| 97 |
+
|
| 98 |
+
# reference image lmk
|
| 99 |
+
ref_lmk_image, ref_lmk = self.detector(ref_image)
|
| 100 |
+
|
| 101 |
+
# clip image
|
| 102 |
+
clip_image = Image.fromarray(np.array(ref_image))
|
| 103 |
+
clip_image = self.clip_image_processor(images=clip_image, return_tensors="pt").pixel_values[0]
|
| 104 |
+
|
| 105 |
+
# reference image
|
| 106 |
+
ref_image = self.to_tensor(ref_image).unsqueeze(1)
|
| 107 |
+
ref_image = ref_image * 2.0 - 1.0
|
| 108 |
+
|
| 109 |
+
# motion sequence
|
| 110 |
+
temp_lmks = np.load(lmk_path, allow_pickle=True)
|
| 111 |
+
# landmark align and draw motions
|
| 112 |
+
if ref_lmk is not None:
|
| 113 |
+
motions = self.get_align_motion(ref_lmk, temp_lmks)
|
| 114 |
+
else:
|
| 115 |
+
motions = [
|
| 116 |
+
self.vis.draw_landmarks((H, W), lmk['lmks'].astype(np.float32), normed=True)
|
| 117 |
+
for lmk in temp_lmks
|
| 118 |
+
]
|
| 119 |
+
motions = [self.to_tensor(motion) for motion in motions]
|
| 120 |
+
motions = torch.stack(motions).permute((1,0,2,3))
|
| 121 |
+
|
| 122 |
+
example = dict()
|
| 123 |
+
example["file_name"] = str(img_path.stem).split('/')[-1]
|
| 124 |
+
example["lmk_name"] = str(lmk_path.stem).split('/')[-1]
|
| 125 |
+
example["motions"] = motions # value in [0, 1]
|
| 126 |
+
example["ref_frame"] = ref_image # value in [-1, 1]
|
| 127 |
+
example["ref_lmk_image"] = ref_lmk_image
|
| 128 |
+
example["clip_image"] = clip_image
|
| 129 |
+
|
| 130 |
+
return example
|