Files
audio2motion/scripts/BG_Actor4/body1.ipynb
T
2022-04-25 22:24:03 +08:00

403 KiB

Exploar the SSIM

In [2]:

from pathlib import Path
import numpy as np

fname = "body1"  
path_root = Path("/mfs/lsen/DATA/Actor4/bg")
path_ssim = path_root.joinpath(fname, "video", "ssim.npy")
In [3]:
data_ssim = np.load(path_ssim)
print(len(data_ssim))
6228
In [4]:
# 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 [5]:
from scipy.signal import find_peaks

x = data_ssim[::1]

height = 0.90
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 =  94
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.show()
filter peaks =  20
In [7]:
# 精细校准
# filter some range 
peaks = peaks_filter.copy()
peaks_filter = [ idx for idx in peaks if x[idx] > 0.91]

# 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 =  11
In [26]:
# 精细校准
list_remove = [1653, 3469, 5243]
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 =  8
In [27]:
# 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_root = Path("/mfs/lsen/DATA/Actor4/bg")
path_ssim = path_root.joinpath(fname, "video", "ssim.npy")
path_frames = path_root.joinpath(fname, "frames")

for i in range(len(peaks_filter)-1):
    ipath_save = path_root.joinpath(fname, "video", f"split_{i:02d}.mp4")
    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


    # concate_video(path_frames, peaks_filter[i], peaks_filter[i+1], ipath_save)
    


frame=[679,1180] nframes = 502, duration=10.04, duration_pad = 11, need padding 24.0
frame=[1180,1600] nframes = 421, duration=8.42, duration_pad = 9, need padding 14.5
frame=[1600,1875] nframes = 276, duration=5.52, duration_pad = 6, need padding 12.0
frame=[1875,3397] nframes = 1523, duration=30.46, duration_pad = 31, need padding 13.5
frame=[3397,3530] nframes = 134, duration=2.68, duration_pad = 3, need padding 8.0
frame=[3530,4025] nframes = 496, duration=9.92, duration_pad = 10, need padding 2.0
frame=[4025,5187] nframes = 1163, duration=23.26, duration_pad = 24, need padding 18.5
In [ ]: