From c26987a6ee0a1644522414bba98b8d46a6a164b9 Mon Sep 17 00:00:00 2001 From: liangsen Date: Sun, 3 Apr 2022 00:31:52 +0800 Subject: [PATCH] extract landmarks --- scripts/Actor1Data/run-00-check-data.py | 35 +++++++++ .../Actor1Data/run-30-extract-landmarks.py | 74 +++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 scripts/Actor1Data/run-00-check-data.py create mode 100644 scripts/Actor1Data/run-30-extract-landmarks.py diff --git a/scripts/Actor1Data/run-00-check-data.py b/scripts/Actor1Data/run-00-check-data.py new file mode 100644 index 0000000..f103a2e --- /dev/null +++ b/scripts/Actor1Data/run-00-check-data.py @@ -0,0 +1,35 @@ +from pathlib import Path +import json +import moviepy +from moviepy.editor import VideoFileClip, AudioFileClip + + +if __name__ == '__main__': + + # path_root = Path("/yrfs/lsen/DATA/Actor1") + path_root = Path("/data1/yrfs/lsen/DATA/bj_chinsee_data") + + vpath_videos = sorted(path_root.glob("**/*.mp4"), reverse=False) + print(f"Have {len(vpath_videos)} dir files") + + durations = 0 + # for each video + for i, ipath_item in enumerate(vpath_videos): + iname = ipath_item.stem + print(f"processing {i}/{len(vpath_videos)} -> {ipath_item} ... ") + + # load video + video = VideoFileClip(str(ipath_item)) + print(f"video duration = {video.duration}, fps = {video.fps}, (start, end)=({video.start},{video.end}), nframes={int(video.duration * video.fps)}") + + # durations += video.duration + # print(f"\t -> Total durations = ({durations / 60 } min) ({durations / 3600 } h))") + # continue + + if video.audio is None: + print("\nERROR: audio info is None") + continue + + durations += video.duration + print(f"\n\t -> Total durations = ({durations / 60 } min) ({durations / 3600 } h))") + # cont \ No newline at end of file diff --git a/scripts/Actor1Data/run-30-extract-landmarks.py b/scripts/Actor1Data/run-30-extract-landmarks.py new file mode 100644 index 0000000..3ad10bf --- /dev/null +++ b/scripts/Actor1Data/run-30-extract-landmarks.py @@ -0,0 +1,74 @@ + +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/Actor1/processing") + vpath_videos = sorted(path_data.glob("*")) + print(f"Have {len(vpath_videos)} dir files") + + device = "cuda:0" + 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 \ No newline at end of file