Files
audio2motion/scripts/BG_Actor4/body1-landmarks.ipynb
T

1.4 MiB

explore landmarks-based similarity

In [1]:

from pathlib import Path
import numpy as np
# Importing packages
import matplotlib.pyplot as plt
%matplotlib inline

from jupyterthemes import jtplot
jtplot.style(grid=False)



# head, ssim_ref_3740
fname = "body1_black" 
# idx_reference = 3739

path_root = Path("/mfs/lsen/DATA/Actor4/bg_black")
path_frame = path_root.joinpath(fname, "frames")
path_video = path_root.joinpath(fname, "video") 
path_ldmks = path_root.joinpath(fname, "openpose")
path_video.mkdir(parents=True, exist_ok=True)

path_lmd = path_video.joinpath(f"lmd_ref_mean.npy")

data_lmd = np.load(path_lmd)
print(data_lmd.shape)

(6228,)
In [2]:
# plot 
def plot_peaks(x, peaks, height):
    plt.figure(figsize=(30,10))
    # plot 
    plt.plot(x)
    plt.plot(peaks, x[peaks], "o", color="r", label="peaks")
    plt.plot(np.full(x.shape, height), "--", color="gray", label="height")
    # plt.ylim(0.5, 1.05)
    plt.grid(True)
    plt.xticks(peaks,rotation=45)
    plt.show()
    

# 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
In [4]:

x = - data_lmd
height = -10

# Plot a simple line chart without any feature
plt.figure(figsize=(30,10))
plt.plot(range(x.shape[0]), x)
plt.plot(range(x.shape[0]), np.full(x.shape, height))
plt.ylim(-50, 1)

plt.show()
In [12]:
from scipy.signal import find_peaks

x = - data_lmd

height = -8
peaks, _ = find_peaks(x, height=(height, 1))

# 3500 + 239 = 3740

print("find peaks = ", len(peaks))
plt.figure(figsize=(30,10))

plt.plot(x)
plt.plot(peaks, x[peaks], "o", color="r", label="peaks")
plt.plot(np.full(x.shape, height), "--", color="gray", label="height")
# plt.ylim(0.5, 1.05)
plt.grid(True)
plt.xticks(peaks,rotation=45)

plt.show()
find peaks =  282
In [17]:
# filter some range 
peaks_filter = filter_peaks(peaks, nwind=50)

# plot
print("filter peaks = ", len(peaks_filter))
plt.figure(figsize=(30,10))

# save peaks 
path_peaks_save = path_video.joinpath("reference_peaks_by_mean_lmd.txt")
np.savetxt(path_peaks_save, peaks_filter, fmt="%d")
print("filter peaks = ", len(peaks_filter), f"save to > {path_peaks_save}")


# plot 
plt.plot(x)
plt.plot(peaks_filter, x[peaks_filter], "o", color="r", label="peaks")
plt.plot(np.full(x.shape, height), "--", color="gray", label="height")
# plt.ylim(0.5, 1.05)
plt.grid(True)
plt.xticks(peaks_filter,rotation=45)

plt.show()


# save filter peaks
filter peaks =  24
filter peaks =  24 save to > /mfs/lsen/DATA/Actor4/bg_black/head_black/video/reference_peaks_by_mean_lmd.txt
In [31]:
# second round 
path_lmd2 = path_video.joinpath(f"lmd_ref_mean2.npy")
data_lmd2 = np.load(path_lmd2)
print(data_lmd2.shape)

x = - data_lmd2

height = -8
peaks, _ = find_peaks(x, height=height)

# filter some range 
peaks_filter = filter_peaks(peaks, nwind=50)

# save peaks 
path_peaks_save = path_video.joinpath("reference_peaks_by_mean_lmd2.txt")
np.savetxt(path_peaks_save, peaks_filter, fmt="%d")
print("filter peaks = ", len(peaks_filter), f"save to > {path_peaks_save}")

# get max lmd
# print(f" reference frame (max idx) = {np.argmax(x)}")
# get max lmd
x_filter = x[peaks_filter]
print(f" reference frame (max idx) = {peaks_filter[np.argmax(x_filter)]}, min={peaks_filter[np.argmin(x_filter)]}")


plot_peaks(x, peaks_filter, height)
(6482,)
filter peaks =  25 save to > /mfs/lsen/DATA/Actor4/bg_black/head_black/video/reference_peaks_by_mean_lmd2.txt
 reference frame (max idx) = 5895, min=5824
In [30]:
# last round 
# second round 
idx_reference = 5859
path_lmd3 = path_video.joinpath(f"lmd_ref_{idx_reference}.npy")
data_lmd3 = np.load(path_lmd3)
print(data_lmd3.shape)

x = - data_lmd3

height = -8
peaks, _ = find_peaks(x, height=height)

# filter some range 
peaks_filter = filter_peaks(peaks, nwind=50)

# save peaks 
path_peaks_save = path_video.joinpath("reference_peaks_by_mean_lmd2.txt")
np.savetxt(path_peaks_save, peaks_filter, fmt="%d")
print("filter peaks = ", len(peaks_filter), f"save to > {path_peaks_save}")


# get max lmd
x_filter = x[peaks_filter]
print(f" reference frame (max idx) = {peaks_filter[np.argmax(x_filter)]}, min={peaks_filter[np.argmin(x_filter)]}")


plot_peaks(x, peaks_filter, height)
(6482,)
filter peaks =  13 save to > /mfs/lsen/DATA/Actor4/bg_black/head_black/video/reference_peaks_by_mean_lmd2.txt
 reference frame (max idx) = 5859, min=5427
In [40]:
# last round 
idx_reference = 136
path_lmd3 = path_video.joinpath(f"lmd_ref_{idx_reference}.npy")
x = - np.load(path_lmd3)
print(x.shape)


height = -10
peaks, _ = find_peaks(x, height=height)
peaks_filter = filter_peaks(x, peaks, nwind=50)
print(f"find peaks = {len(peaks)}, after filter = {len(peaks_filter)}")

plot_peaks(x, peaks_filter, height)
(6482,)
find peaks = 386, after filter = 31
In [39]:
# last round 
idx_reference = 3149
path_lmd3 = path_video.joinpath(f"lmd_ref_{idx_reference}.npy")
x = - np.load(path_lmd3)
print(x.shape)


height = -10
peaks, _ = find_peaks(x, height=height)
peaks_filter = filter_peaks(x, peaks, nwind=50)
print(f"find peaks = {len(peaks)}, after filter = {len(peaks_filter)}")

plot_peaks(x, peaks_filter, height)
(6482,)
find peaks = 358, after filter = 30
In [47]:
# last round 
idx_reference = 4967
path_lmd3 = path_video.joinpath(f"lmd_ref_{idx_reference}.npy")
x = - np.load(path_lmd3)
print(x.shape)


height = -10
peaks, _ = find_peaks(x, height=height)
peaks_filter = filter_peaks(x, peaks, nwind=50)
print(f"find peaks = {len(peaks)}, after filter = {len(peaks_filter)}")

plot_peaks(x, peaks_filter, height)
(6482,)
find peaks = 371, after filter = 26
In [48]:
# 精细校准
list_remove = [1583, 2778, 3224, 4722, 4862, 4914, 5404, 5821, 6086]
for irm in list_remove:
    peaks_filter.remove(irm)


# save 
path_peaks_save = path_video.joinpath("reference_peaks_landmarks.txt")

# save peaks 
peaks_save = [idx_reference] + peaks_filter
np.savetxt(path_peaks_save, peaks_save, fmt="%d")

print(f"save peaks (len={len(peaks_save)}) in files > {path_peaks_save}")
print(f"peaks = {peaks_save}")


# plot
plot_peaks(x, peaks_filter, height)

save peaks_filter(len=17) in files > /mfs/lsen/DATA/Actor4/bg_black/head_black/video/reference_peaks_landmarks.txt
peaks = [4967, 136, 1137, 1532, 1726, 2227, 2726, 3156, 3370, 3755, 3926, 4967, 5273, 5346, 5766, 5893, 6169, 6461]
In [50]:
import tempfile
import os

def concate_video(path_frames, idx_beg, idx_end, path_save):

    # write to a input.txt 
    file_tmp = tempfile.NamedTemporaryFile(suffix='.txt')

    files = [ str(path_frames.joinpath(f"frame-{i:06d}.jpg")) for i in range(idx_beg, idx_end)]
    with open(file_tmp.name, 'w') as f:
        for ifiles in files:
            f.write(f"file {ifiles} \n")

    # with open(file_tmp.name, 'r') as f:
    #     read = f.readlines()
    #     print(read)

    # concate to a video 
    cmd = f"ffmpeg -y -r 50  -f concat -safe 0 -i {str(file_tmp.name)} -c:v libx264 -pix_fmt yuv420p -loglevel error  {str(path_save)}"
    print(cmd)
    os.system(cmd)
    print(f"frame=[{idx_beg},{idx_end}] duration={(idx_end - idx_beg)/50}s. save video to {path_save}")
In [52]:
# cut video
ipath_video_save = path_video.joinpath(f"video_split_ref_{idx_reference}")
ipath_video_save.mkdir(parents=True, exist_ok=True)


for i in range(len(peaks_filter)-1):
    idx_beg = peaks_filter[i]
    idx_end = peaks_filter[i+1]
    
    nsize = idx_end - idx_beg + 1
    
    duration     = nsize/50
    duration_pad = nsize//50 + 1
    nframe_pad = duration_pad * 50 - nsize
    
    print(f"frame=[{idx_beg},{idx_end}] nframes = {nsize}, duration={duration}, duration_pad = {duration_pad}, need padding {nframe_pad / 2}")

    # make 32 padding 
    # resampling
    ipath_save = ipath_video_save.joinpath(f"split_{i:02d}.mp4")
    concate_video(path_frame, idx_beg, idx_end, ipath_save)
frame=[136,1137] nframes = 1002, duration=20.04, duration_pad = 21, need padding 24.0
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmp0_7_bo8k.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_00.mp4
frame=[136,1137] duration=20.02s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_00.mp4
frame=[1137,1532] nframes = 396, duration=7.92, duration_pad = 8, need padding 2.0
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpxmn81ap8.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_01.mp4
frame=[1137,1532] duration=7.9s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_01.mp4
frame=[1532,1726] nframes = 195, duration=3.9, duration_pad = 4, need padding 2.5
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpgin9w9az.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_02.mp4
frame=[1532,1726] duration=3.88s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_02.mp4
frame=[1726,2227] nframes = 502, duration=10.04, duration_pad = 11, need padding 24.0
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpvssk43la.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_03.mp4
frame=[1726,2227] duration=10.02s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_03.mp4
frame=[2227,2726] nframes = 500, duration=10.0, duration_pad = 11, need padding 25.0
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpfk0ywry5.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_04.mp4
frame=[2227,2726] duration=9.98s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_04.mp4
frame=[2726,3156] nframes = 431, duration=8.62, duration_pad = 9, need padding 9.5
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmp8853rv8b.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_05.mp4
frame=[2726,3156] duration=8.6s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_05.mp4
frame=[3156,3370] nframes = 215, duration=4.3, duration_pad = 5, need padding 17.5
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpwyp_4_i4.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_06.mp4
frame=[3156,3370] duration=4.28s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_06.mp4
frame=[3370,3755] nframes = 386, duration=7.72, duration_pad = 8, need padding 7.0
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpzxyqdv0v.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_07.mp4
frame=[3370,3755] duration=7.7s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_07.mp4
frame=[3755,3926] nframes = 172, duration=3.44, duration_pad = 4, need padding 14.0
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpfxs15jv3.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_08.mp4
frame=[3755,3926] duration=3.42s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_08.mp4
frame=[3926,4967] nframes = 1042, duration=20.84, duration_pad = 21, need padding 4.0
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpwv73flub.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_09.mp4
frame=[3926,4967] duration=20.82s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_09.mp4
frame=[4967,5273] nframes = 307, duration=6.14, duration_pad = 7, need padding 21.5
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpcwzqo6tu.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_10.mp4
frame=[4967,5273] duration=6.12s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_10.mp4
frame=[5273,5346] nframes = 74, duration=1.48, duration_pad = 2, need padding 13.0
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmp1132hkjy.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_11.mp4
frame=[5273,5346] duration=1.46s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_11.mp4
frame=[5346,5766] nframes = 421, duration=8.42, duration_pad = 9, need padding 14.5
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpdqaeghur.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_12.mp4
frame=[5346,5766] duration=8.4s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_12.mp4
frame=[5766,5893] nframes = 128, duration=2.56, duration_pad = 3, need padding 11.0
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpntllgzlv.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_13.mp4
frame=[5766,5893] duration=2.54s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_13.mp4
frame=[5893,6169] nframes = 277, duration=5.54, duration_pad = 6, need padding 11.5
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmp_1u5kth9.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_14.mp4
frame=[5893,6169] duration=5.52s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_14.mp4
frame=[6169,6461] nframes = 293, duration=5.86, duration_pad = 6, need padding 3.5
ffmpeg -y -r 50  -f concat -safe 0 -i /tmp/tmpazg3kobi.txt -c:v libx264 -pix_fmt yuv420p -loglevel error  /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_15.mp4
frame=[6169,6461] duration=5.84s. save video to /mfs/lsen/DATA/Actor4/bg_black/head_black/video/video_split_ref_4967/split_15.mp4
In [ ]: