update
This commit is contained in:
1 parent
4a53051fee
commit
039ec3cf52
179 files changed
+22667
No files matched your search
@@ -0,0 +1,124 @@
|
||||
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
|
||||
|
||||
import os, sys
|
||||
sys.path.insert(0, os.getcwd())
|
||||
from common.logger import Logger
|
||||
|
||||
class ImagesDataset(Dataset):
|
||||
def __init__(self, path_folder:Path):
|
||||
|
||||
self.path_folder = path_folder
|
||||
self.list_image = sorted(path_folder.glob("*.jpg"), reverse=False)
|
||||
logger.info(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__":
|
||||
|
||||
actor = "Actor6"
|
||||
sub_folder = "processing" # smile, processing, processing2
|
||||
device = "cuda:1"
|
||||
|
||||
# logger
|
||||
path_log = Path(f"./data/{actor}")
|
||||
path_log.mkdir(parents=True, exist_ok=True)
|
||||
logger = Logger(path_log, postfix=f"detect-lm68-{sub_folder}")
|
||||
|
||||
|
||||
path_data = Path(f"/mfs/lsen/DATA/{actor}/{sub_folder}")
|
||||
path_data.mkdir(parents=True, exist_ok=True)
|
||||
vpath_videos = sorted(path_data.glob("*"), reverse=True)
|
||||
logger.info(f"Have {len(vpath_videos)} dir files")
|
||||
|
||||
|
||||
model = FaceAlignment(LandmarksType._2D, device=device, face_detector='sfd')
|
||||
logger.info(f"load FaceAlignment model in {device}")
|
||||
|
||||
# IDs = ["2-23min"]
|
||||
# for each word
|
||||
for i, ipath_item in enumerate(vpath_videos[:16]):
|
||||
iname = ipath_item.stem
|
||||
logger.info(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)
|
||||
|
||||
num_frame = len(list(ipath_frame.glob("*.jpg")))
|
||||
# check frame have exsit
|
||||
if num_frame < 50:
|
||||
logger.info(f"\n Skip: num_frame = {num_frame} is small!")
|
||||
continue
|
||||
|
||||
# check have done
|
||||
num_ladmk = len(list(ipath_ldmks.glob("*.txt")))
|
||||
if num_frame == num_ladmk:
|
||||
logger.info(f"\n Skip: have already done!")
|
||||
continue
|
||||
|
||||
|
||||
prev_ldmk = None
|
||||
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
|
||||
# logger.info(f"ibatch_data shape = {ibatch_data.shape}, path={ibatch_name[:3]}")
|
||||
if k%100 == 0:
|
||||
logger.info(f"batching {k}/{len(dloader)} ... ")
|
||||
|
||||
pred = model.get_landmarks_from_batch(ibatch_data, return_landmark_score=False)
|
||||
# logger.info(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")
|
||||
|
||||
# if have multipe-landmarks
|
||||
if jldmk.shape[0] > 68:
|
||||
logger.info(f"ERROR: > use first 68-kpts, cropped_ldmk = {jldmk.shape}, jpath_ldmk={jpath_ldmk}")
|
||||
|
||||
max_cos = 0
|
||||
kpt_selected = None
|
||||
if prev_ldmk is None:
|
||||
kpt_selected = jldmk[:68]
|
||||
else:
|
||||
n_kpt = jldmk.shape[0] // 68
|
||||
for idx in range(n_kpt):
|
||||
cur_kpt = jldmk[idx*68:(idx+1)*68, :]
|
||||
cur_cos = np.dot(prev_ldmk, cur_kpt.reshape(-1)) / (np.linalg.norm(prev_ldmk) * np.linalg.norm(cur_kpt.reshape(-1)))
|
||||
if cur_cos > max_cos:
|
||||
max_cos = cur_cos
|
||||
kpt_selected = cur_kpt
|
||||
|
||||
# get jldmk, which match the prev_landmarks
|
||||
logger.info(f"\t SELETCT: max_cos = {max_cos} --> {kpt_selected.reshape(-1)[::10]}")
|
||||
jldmk = kpt_selected
|
||||
|
||||
# record prev
|
||||
prev_ldmk = jldmk.copy().reshape(-1) # record prev_ldmk
|
||||
|
||||
# save ldmks
|
||||
assert jldmk.shape[0] == 68, f"jldmak shape = {jldmk.shape}, should be 68! "
|
||||
np.savetxt(jpath_ldmk, jldmk, fmt=" %3d")
|
||||
|
||||
# break
|
||||
Reference in new issue
Block a user