73 lines
2.5 KiB
Python
73 lines
2.5 KiB
Python
from torch.utils.data import Dataset, DataLoader
|
|
|
|
import torch
|
|
import numpy as np
|
|
from PIL import Image
|
|
from pathlib2 import Path
|
|
from face_alignment import FaceAlignment, LandmarksType
|
|
from zmq import device
|
|
|
|
|
|
class ImagesDataset(Dataset):
|
|
def __init__(self, path_folder:Path):
|
|
|
|
self.path_folder = path_folder
|
|
self.list_image = sorted(path_folder.glob("*.jpg"), reverse=False)
|
|
print(f"Have {len(self.list_image)} images, like {self.list_image[:3]}")
|
|
|
|
|
|
def __len__(self):
|
|
return len(self.list_image)
|
|
|
|
def __getitem__(self, idx):
|
|
path_frame = self.list_image[idx]
|
|
image = Image.open(str(path_frame)).convert('RGB') # (h, w, 3)
|
|
image = np.asarray(image)
|
|
return {"image_name": path_frame.stem, "image": image}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
path_data = Path("/mfs/lsen/DATA/Actor3/processing")
|
|
vpath_videos = sorted(path_data.glob("*"))
|
|
print(f"Have {len(vpath_videos)} dir files")
|
|
|
|
device = "cuda:1"
|
|
model = FaceAlignment(LandmarksType._2D, device=device, face_detector='sfd')
|
|
print(f"load FaceAlignment model in {device}")
|
|
|
|
# IDs = ["2-23min"]
|
|
# for each word
|
|
for i, ipath_item in enumerate(vpath_videos):
|
|
iname = ipath_item.stem
|
|
print(f"processing {i}/{len(vpath_videos)} -> {ipath_item} ... ")
|
|
|
|
# if iname not in IDs: continue
|
|
|
|
ipath_infor = ipath_item.joinpath("video/info.json")
|
|
ipath_ldmks = ipath_item.joinpath("landmarks_FAN")
|
|
ipath_frame = ipath_item.joinpath("frames")
|
|
ipath_ldmks.mkdir(parents=True, exist_ok=True)
|
|
|
|
dataset = ImagesDataset(ipath_frame)
|
|
dloader = DataLoader(dataset, batch_size=16, num_workers=8)
|
|
|
|
for k, ibatch in enumerate(dloader):
|
|
ibatch_name = ibatch["image_name"]
|
|
ibatch_data = ibatch["image"].to(device) # B x H x W x 3
|
|
ibatch_data = ibatch_data.permute(0, 3, 1, 2) # B x H x W x 3
|
|
# print(f"ibatch_data shape = {ibatch_data.shape}, path={ibatch_name[:3]}")
|
|
if k%100 == 0:
|
|
print(f"batching {k}/{len(dloader)} ... ")
|
|
|
|
pred = model.get_landmarks_from_batch(ibatch_data, return_landmark_score=False)
|
|
# print(f"pred len = {len(pred)}, type= {type(pred)}, [0] shape={pred[0].shape}")
|
|
|
|
# save landmarks
|
|
for jldmk, jname in zip(pred, ibatch_name):
|
|
jpath_ldmk = ipath_ldmks.joinpath(jname + ".txt")
|
|
np.savetxt(jpath_ldmk, jldmk, fmt=" %3d")
|
|
|
|
|
|
|
|
# break |