105 lines
3.0 KiB
Python
105 lines
3.0 KiB
Python
import os
|
|
from pathlib import Path
|
|
import numpy as np
|
|
import json
|
|
from scipy.signal import find_peaks
|
|
|
|
np.set_printoptions(precision=2, suppress=True)
|
|
|
|
|
|
# filter by windows
|
|
def filter_peaks(x, input_peaks, nwind= 1*50):
|
|
peaks = input_peaks.copy()
|
|
peaks_filter = []
|
|
|
|
i = 1
|
|
prev_idx = peaks[0]
|
|
while i < len(peaks):
|
|
cur_idx = peaks[i]
|
|
|
|
# filter peak
|
|
while cur_idx - prev_idx <= nwind and i < len(peaks):
|
|
# compare ssim
|
|
if x[cur_idx] > x[prev_idx]:
|
|
prev_idx = cur_idx
|
|
# next
|
|
i += 1
|
|
if i < len(peaks):
|
|
cur_idx = peaks[i]
|
|
else:
|
|
break
|
|
# update
|
|
i += 1
|
|
|
|
# add to
|
|
peaks_filter.append(prev_idx)
|
|
prev_idx = cur_idx
|
|
|
|
# last value
|
|
if cur_idx - peaks_filter[-1] > nwind:
|
|
peaks_filter.append(prev_idx)
|
|
# peaks_filter.append(prev_idx)
|
|
|
|
|
|
return peaks_filter
|
|
|
|
|
|
def get_landmarks(idx, path_ldmks):
|
|
# get lmd_distance
|
|
path_json = path_ldmks.joinpath(f"frame-{idx:06d}_keypoints.json")
|
|
dict_json = json.loads(open(str(path_json), 'r').read())['people']
|
|
data_ldmk_pose = np.array(dict_json[0]["pose_keypoints"]).reshape(-1, 3)
|
|
data_ldmk_face = np.array(dict_json[0]["face_keypoints"]).reshape(-1, 3)
|
|
# select & concate pose
|
|
data_ldmk_pose = data_ldmk_pose[[2, 5, 0, 16, 17], :2] # COCO-18 body pose
|
|
data_ldmk_face = data_ldmk_face[:, :2] # 70 landmarks pose
|
|
data_ldmk = np.concatenate([data_ldmk_pose, data_ldmk_face], axis=0) # (75, 2)
|
|
# print(idata_ldmk.shape)
|
|
|
|
return data_ldmk
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# head, ssim_ref_3740
|
|
fname = "head_black"
|
|
height = -8
|
|
|
|
path_root = Path("/mfs/lsen/DATA/Actor4/bg_black")
|
|
path_video = path_root.joinpath(fname, "video")
|
|
path_ldmks = path_root.joinpath(fname, "openpose")
|
|
path_video.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
path_peaks = path_video.joinpath("reference_peaks_by_mean_lmd2.txt")
|
|
vpeaks = np.loadtxt(path_peaks, dtype=int)
|
|
print("vpeaks = ", vpeaks)
|
|
|
|
vpath_ldmks = sorted(path_ldmks.glob("frame-*_keypoints.json"), reverse=False)
|
|
|
|
|
|
total_values = []
|
|
# 对每个renference帧
|
|
for idx in vpeaks:
|
|
print(f"processing... {idx:05d}-th peaks ")
|
|
ipath_lmd = path_video.joinpath(f"lmd_ref_{idx}.npy")
|
|
idata_lmd = - np.load(ipath_lmd)
|
|
|
|
# 找到peaks点
|
|
peaks, _ = find_peaks(idata_lmd, height=height)
|
|
peaks_filter = filter_peaks(idata_lmd, peaks, nwind=50)
|
|
|
|
# 求总的值
|
|
idata_filter = idata_lmd[peaks_filter]
|
|
imean_lmd = np.mean(idata_filter)
|
|
print(f"find peaks = {len(peaks)}, after filter = {len(peaks_filter)}, mean of filter ldm = {imean_lmd}")
|
|
total_values.append(imean_lmd)
|
|
|
|
|
|
# 找到最小的帧
|
|
print("total_values = ", total_values)
|
|
idx_max = np.argmax(total_values)
|
|
idx_reference = vpeaks[idx_max]
|
|
|
|
print(f"min ldm = {total_values[idx_max]}, corresponding frame = {idx_reference}") |