add Actor7 support

This commit is contained in:
sen.liang committed 2022-05-28 00:01:31 +08:00
1 parent a4c53e3265
commit 7aed8b140a
16 files changed
+403 -316

No files matched your search

+59 -17
View File
@@ -7,15 +7,17 @@ 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)
print(f"Have {len(self.list_image)} images, like {self.list_image[:3]}")
logger.info(f"Have {len(self.list_image)} images, like {self.list_image[:3]}")
def __len__(self):
return len(self.list_image)
@@ -28,36 +30,51 @@ class ImagesDataset(Dataset):
if __name__ == "__main__":
path_data = Path("/ssdata1/lsen/DATA/Actor5/processing")
actor = "Actor7"
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)
print(f"Have {len(vpath_videos)} dir files")
logger.info(f"Have {len(vpath_videos)} dir files")
device = "cuda:7"
model = FaceAlignment(LandmarksType._2D, device=device, face_detector='sfd')
print(f"load FaceAlignment model in {device}")
logger.info(f"load FaceAlignment model in {device}")
# IDs = ["2-23min"]
# for each word
for i, ipath_item in enumerate(vpath_videos[16:]):
for i, ipath_item in enumerate(vpath_videos[:16]):
iname = ipath_item.stem
print(f"processing {i}/{len(vpath_videos)} -> {ipath_item} ... ")
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)
# check
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:
print(f"\n Skip: have already done!")
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)
@@ -65,18 +82,43 @@ if __name__ == "__main__":
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]}")
# logger.info(f"ibatch_data shape = {ibatch_data.shape}, path={ibatch_name[:3]}")
if k%100 == 0:
print(f"batching {k}/{len(dloader)} ... ")
logger.info(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}")
# 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