update actor5 & actor6 data processing code
This commit is contained in:
1 parent
2095b48c56
commit
922a98e36b
29 files changed
+3694
-94
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import cv2
|
||||
from cv2 import norm
|
||||
import numpy as np
|
||||
from pathlib2 import Path
|
||||
from skimage.transform import estimate_transform, warp
|
||||
@@ -6,7 +7,7 @@ from skimage.transform import estimate_transform, warp
|
||||
from torch.utils.data import Dataset, DataLoader
|
||||
|
||||
class ImagesDataset(Dataset):
|
||||
def __init__(self, path_frames, path_landmk, suffix="jpg", crop_size=224, scale=1.25, roation_image=False):
|
||||
def __init__(self, path_frames, path_landmk, suffix="jpg", crop_size=512, scale=1.25, roation_image=False, use_same_bbox=False, path_same_bbox = None):
|
||||
'''
|
||||
path_frames: folder, vpath_image, image path, video path
|
||||
'''
|
||||
@@ -19,6 +20,14 @@ class ImagesDataset(Dataset):
|
||||
self.scale = scale # 1.25
|
||||
self.crop_size = crop_size # 224
|
||||
self.roation_image =roation_image
|
||||
self.use_same_bbox = use_same_bbox
|
||||
|
||||
self.prev_ldmk = None
|
||||
|
||||
if self.use_same_bbox:
|
||||
self.same_bbox = np.load(path_same_bbox).reshape(-1) # (4,) [left, right, top, bottom]
|
||||
print(f"same_bbox = {self.same_bbox}")
|
||||
|
||||
|
||||
def bbox2point(self, left, right, top, bottom, type='bbox'):
|
||||
''' bbox from detector and landmarks are different
|
||||
@@ -36,10 +45,10 @@ class ImagesDataset(Dataset):
|
||||
|
||||
return old_size, center
|
||||
|
||||
|
||||
def __len__(self):
|
||||
return len(self.vpath_image)
|
||||
|
||||
|
||||
def __getitem__(self, index):
|
||||
ipath_frame = self.vpath_image[index]
|
||||
image_name = ipath_frame.stem # (frame-0000)
|
||||
@@ -51,16 +60,40 @@ class ImagesDataset(Dataset):
|
||||
raise Exception(f"ipath_ldmk not exists --> {ipath_ldmk}")
|
||||
|
||||
# load orignal image & landmarks
|
||||
image = cv2.imread(str(ipath_frame)) # (H, W, 3), dtype=uint8
|
||||
image = cv2.imread(str(ipath_frame)) # (H, W, 3), dtype=uint8
|
||||
kpt = np.loadtxt(ipath_ldmk) # (68, 2)
|
||||
|
||||
kpt_selected = None
|
||||
if kpt.shape[0] > 68:
|
||||
# raise Exception(f"cropped_ldmk = {cropped_ldmk.shape}, frame={image_name}, ipath_ldmk={ipath_ldmk}")
|
||||
n_kpt = kpt.shape[0] // 68
|
||||
max_cos = 0
|
||||
for idx in range(n_kpt):
|
||||
cur_kpt = kpt[idx*68:(idx+1)*68, :]
|
||||
cur_cos = np.dot(self.prev_ldmk, cur_kpt.reshape(-1)) / (np.linalg.norm(self.prev_ldmk) * np.linalg.norm(cur_kpt.reshape(-1)))
|
||||
if cur_cos > max_cos:
|
||||
kpt_selected = cur_kpt
|
||||
|
||||
# get kpt, which match the prev_landmarks
|
||||
kpt = kpt_selected
|
||||
print(f"ERROR: > use first 68-kpts, cropped_ldmk = {kpt.shape}, frame={image_name}, ipath_ldmk={ipath_ldmk}")
|
||||
|
||||
# record prev_ldmk
|
||||
self.prev_ldmk = kpt.copy().reshpae(-1)
|
||||
|
||||
# reverse H&W
|
||||
if self.roation_image:
|
||||
image = image.transpose(1,0,2) # (H, W, 3)
|
||||
kpt = kpt[:, ::-1] # (68, 2)
|
||||
|
||||
left = np.min(kpt[:,0]); right = np.max(kpt[:,0])
|
||||
top = np.min(kpt[:,1]); bottom = np.max(kpt[:,1])
|
||||
if self.use_same_bbox:
|
||||
left, right = self.same_bbox[0], self.same_bbox[1]
|
||||
top, bottom = self.same_bbox[2], self.same_bbox[3]
|
||||
else:
|
||||
left = np.min(kpt[:,0]); right = np.max(kpt[:,0])
|
||||
top = np.min(kpt[:,1]); bottom = np.max(kpt[:,1])
|
||||
|
||||
|
||||
old_size, center = self.bbox2point(left, right, top, bottom, type='kpt68')
|
||||
|
||||
# crop transform
|
||||
@@ -78,17 +111,12 @@ class ImagesDataset(Dataset):
|
||||
|
||||
# image = image/255.
|
||||
# (crop_size, crop_size, 3) ,float64
|
||||
cropped_imag = warp(image, tform.inverse, output_shape=(self.crop_size, self.crop_size)) # scale to (0~1)
|
||||
cropped_ldmk = np.dot(tform.params, np.hstack([kpt, np.ones([kpt.shape[0],1])]).T).T # (68,3)
|
||||
cropped_imag = warp(image, tform.inverse, output_shape=(self.crop_size, self.crop_size)) # scale to (0~1)
|
||||
cropped_ldmk = np.dot(tform.params, np.hstack([kpt, np.ones([kpt.shape[0],1])]).T).T # (68,3)
|
||||
cropped_ldmk = cropped_ldmk[:, :2]
|
||||
|
||||
# cropped_ldmk[:,:2] = cropped_ldmk[:,:2] / self.crop_size * 2 - 1
|
||||
|
||||
if cropped_ldmk.shape[0] > 68:
|
||||
# raise Exception(f"cropped_ldmk = {cropped_ldmk.shape}, frame={image_name}, ipath_ldmk={ipath_ldmk}")
|
||||
cropped_ldmk = cropped_ldmk[:68, :]
|
||||
print(f"ERROR: > use first 68-kpts, cropped_ldmk = {cropped_ldmk.shape}, frame={image_name}, ipath_ldmk={ipath_ldmk}")
|
||||
|
||||
|
||||
# cropped_mask = warp(mask, tform.inverse, output_shape=(self.crop_size, self.crop_size)) # scale to (0~1)
|
||||
|
||||
# print(f"image dytpe = {image.dtype}, shape = {image.shape} ")
|
||||
@@ -107,13 +135,17 @@ class ImagesDataset(Dataset):
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
sub_folder = "processing" # processing, body, smile
|
||||
path_data = Path(f"/ssdata1/lsen/DATA/Actor5/{sub_folder}")
|
||||
vpath_videos = sorted(path_data.glob("*"), reverse=False)
|
||||
sub_folder = "processing2" # processing, processing2
|
||||
path_data = Path(f"/ssdata1/lsen/DATA/Actor6/{sub_folder}")
|
||||
path_same_bbox = f"data/Actor6/max_bbox_{sub_folder}.npy"
|
||||
|
||||
vpath_videos = sorted(path_data.glob("*"), reverse=True)
|
||||
print(f"Have {len(vpath_videos)} dir files")
|
||||
|
||||
# whole body need roation frame
|
||||
roation_image = True if sub_folder == "body" else False
|
||||
print(f"roation_image = {roation_image}")
|
||||
|
||||
|
||||
# IDs = ["2-23min"]
|
||||
# for each word
|
||||
@@ -133,17 +165,27 @@ if __name__ == "__main__":
|
||||
ipath_tfrom.mkdir(parents=True, exist_ok=True)
|
||||
ipath_lcrop.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# check
|
||||
# check have done
|
||||
num_frame = len(list(ipath_frame.glob("*.jpg")))
|
||||
num_fcrop = len(list(ipath_fcrop.glob("*.jpg")))
|
||||
if num_frame == num_fcrop:
|
||||
print(f"\n Skip: have already done!")
|
||||
continue
|
||||
|
||||
# check landmarks have exists
|
||||
num_ldmks = len(list(ipath_ldmks.glob("*.txt")))
|
||||
if num_frame != num_ldmks:
|
||||
print(f"\n ERROR & Skip: ldmaks = {num_ldmks} != nframes {num_frame}!")
|
||||
continue
|
||||
|
||||
# only whole body image, need set roation_image=True
|
||||
dataset = ImagesDataset(ipath_frame, ipath_ldmks, suffix="jpg", crop_size=512, scale=1.5, roation_image=roation_image)
|
||||
dloader = DataLoader(dataset, batch_size=64, num_workers=4, shuffle=False)
|
||||
dataset = ImagesDataset(ipath_frame, ipath_ldmks, suffix="jpg", crop_size=512, scale=1.5,
|
||||
roation_image=roation_image, use_same_bbox=True, path_same_bbox = path_same_bbox )
|
||||
|
||||
dloader = DataLoader(dataset, batch_size=12, num_workers=1, shuffle=False)
|
||||
|
||||
|
||||
print(f"processing crop ... ")
|
||||
for j, jbatch in enumerate(dloader):
|
||||
# print(f"jbatch_data shape = {jbatch_data.shape}, path={jbatch_name[:3]}")
|
||||
if j%100 == 0:
|
||||
@@ -170,6 +212,6 @@ if __name__ == "__main__":
|
||||
np.save(kpath_tfrom, jbatch_tfrom[k])
|
||||
np.savetxt(kpath_lcrop, jbatch_ldmks[k], fmt=" %3d")
|
||||
cv2.imwrite(str(kpath_fcrop), jbatch_crop[k])
|
||||
|
||||
# cv2.imwrite(str(ipath_vis.joinpath(f"{jname}_{vis_name}.jpg")), img)
|
||||
|
||||
# break
|
||||
Reference in new issue
Block a user