Files
audio2motion/scripts/BG_Actor6/body2.ipynb
T
2022-06-24 15:08:13 +08:00

340 KiB

Exploar the SSIM

In [10]:

from pathlib import Path
import numpy as np

fname = "body2_black"  
path_root = Path("/mfs/lsen/DATA/Actor4/bg_black")
path_ssim = path_root.joinpath(fname, "video", "ssim_ref_4790.npy")
data_ssim = np.load(path_ssim)
print(len(data_ssim))
6124
In [2]:
# Importing packages
import matplotlib.pyplot as plt
%matplotlib inline

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

data_tmp = data_ssim[::5]


# Define x and y values
x = range(data_tmp.shape[0])

# Plot a simple line chart without any feature
plt.figure(figsize=(30,10))
plt.plot(x, data_tmp)
plt.plot(x, np.full(data_tmp.shape, 0.92))
plt.ylim(0.5, 1)

# plt.legend(["rec_loss", "vel_loss1", "vel_loss2"])
# plt.plot(x, data_loss[:,2])
plt.show()

In [4]:
from scipy.signal import find_peaks

x = data_ssim[::1]

height = 0.88
peaks, _ = find_peaks(x, height=height)


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.show()
find peaks =  115
In [6]:
# filter some range 

peaks_filter = []
nwind = 1 * 50
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


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

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()
filter peaks =  25
In [7]:
# 精细校准
list_remove = [3484, 4702, 4849, 5502, 5560]
for irm in list_remove:
    peaks_filter.remove(irm)

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

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()



filter peaks =  20
In [12]:
# cut video
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}")


# save 
path_frames = path_root.joinpath(fname, "frames")
path_peaks_save = path_root.joinpath(fname, "video", "reference_peaks.txt")

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

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


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 = path_root.joinpath(fname, "video", f"split_{i:02d}.mp4")
    # concate_video(path_frames, idx_beg, idx_end, ipath_save)
    


save peaks_filter(len=20) in files > /mfs/lsen/DATA/Actor4/bg_black/body2_black/video/reference_peaks.txt
peaks = [158, 1270, 1493, 1618, 1967, 2140, 2346, 2690, 2949, 3107, 3395, 3986, 4473, 4790, 4943, 5158, 5382, 5752, 5958, 6112]
frame=[158,1270] nframes = 1113, duration=22.26, duration_pad = 23, need padding 18.5
frame=[1270,1493] nframes = 224, duration=4.48, duration_pad = 5, need padding 13.0
frame=[1493,1618] nframes = 126, duration=2.52, duration_pad = 3, need padding 12.0
frame=[1618,1967] nframes = 350, duration=7.0, duration_pad = 8, need padding 25.0
frame=[1967,2140] nframes = 174, duration=3.48, duration_pad = 4, need padding 13.0
frame=[2140,2346] nframes = 207, duration=4.14, duration_pad = 5, need padding 21.5
frame=[2346,2690] nframes = 345, duration=6.9, duration_pad = 7, need padding 2.5
frame=[2690,2949] nframes = 260, duration=5.2, duration_pad = 6, need padding 20.0
frame=[2949,3107] nframes = 159, duration=3.18, duration_pad = 4, need padding 20.5
frame=[3107,3395] nframes = 289, duration=5.78, duration_pad = 6, need padding 5.5
frame=[3395,3986] nframes = 592, duration=11.84, duration_pad = 12, need padding 4.0
frame=[3986,4473] nframes = 488, duration=9.76, duration_pad = 10, need padding 6.0
frame=[4473,4790] nframes = 318, duration=6.36, duration_pad = 7, need padding 16.0
frame=[4790,4943] nframes = 154, duration=3.08, duration_pad = 4, need padding 23.0
frame=[4943,5158] nframes = 216, duration=4.32, duration_pad = 5, need padding 17.0
frame=[5158,5382] nframes = 225, duration=4.5, duration_pad = 5, need padding 12.5
frame=[5382,5752] nframes = 371, duration=7.42, duration_pad = 8, need padding 14.5
frame=[5752,5958] nframes = 207, duration=4.14, duration_pad = 5, need padding 21.5
frame=[5958,6112] nframes = 155, duration=3.1, duration_pad = 4, need padding 22.5
In [ ]: