423 KiB
423 KiB
In [16]:
from pathlib import Path
import numpy as np
fname = "body1_black"
path_root = Path("/mfs/lsen/DATA/Actor4/bg_black")
path_ssim = path_root.joinpath(fname, "video", "ssim_ref_3529.npy")
data_ssim = np.load(path_ssim)
print(len(data_ssim))6228
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 [11]:
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.grid(True)
plt.xticks(peaks,rotation=45)
plt.show()
find peaks = 172
In [12]:
# 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 = 31
In [13]:
# 精细校准
peaks_filter_old2 = peaks_filter.copy()
list_remove = [666, 884, 1652, 1708, 1762, 1933, 3469 ]
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 = 24
In [17]:
# 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, [3529] + 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=24) in files > /mfs/lsen/DATA/Actor4/bg_black/body1_black/video/reference_peaks.txt peaks = [548, 718, 832, 1180, 1282, 1367, 1600, 1878, 2003, 2139, 2345, 2463, 2522, 3398, 3529, 4025, 4145, 4335, 4760, 5157, 5246, 5300, 5865, 6211] frame=[548,718] nframes = 171, duration=3.42, duration_pad = 4, need padding 14.5 frame=[718,832] nframes = 115, duration=2.3, duration_pad = 3, need padding 17.5 frame=[832,1180] nframes = 349, duration=6.98, duration_pad = 7, need padding 0.5 frame=[1180,1282] nframes = 103, duration=2.06, duration_pad = 3, need padding 23.5 frame=[1282,1367] nframes = 86, duration=1.72, duration_pad = 2, need padding 7.0 frame=[1367,1600] nframes = 234, duration=4.68, duration_pad = 5, need padding 8.0 frame=[1600,1878] nframes = 279, duration=5.58, duration_pad = 6, need padding 10.5 frame=[1878,2003] nframes = 126, duration=2.52, duration_pad = 3, need padding 12.0 frame=[2003,2139] nframes = 137, duration=2.74, duration_pad = 3, need padding 6.5 frame=[2139,2345] nframes = 207, duration=4.14, duration_pad = 5, need padding 21.5 frame=[2345,2463] nframes = 119, duration=2.38, duration_pad = 3, need padding 15.5 frame=[2463,2522] nframes = 60, duration=1.2, duration_pad = 2, need padding 20.0 frame=[2522,3398] nframes = 877, duration=17.54, duration_pad = 18, need padding 11.5 frame=[3398,3529] nframes = 132, duration=2.64, duration_pad = 3, need padding 9.0 frame=[3529,4025] nframes = 497, duration=9.94, duration_pad = 10, need padding 1.5 frame=[4025,4145] nframes = 121, duration=2.42, duration_pad = 3, need padding 14.5 frame=[4145,4335] nframes = 191, duration=3.82, duration_pad = 4, need padding 4.5 frame=[4335,4760] nframes = 426, duration=8.52, duration_pad = 9, need padding 12.0 frame=[4760,5157] nframes = 398, duration=7.96, duration_pad = 8, need padding 1.0 frame=[5157,5246] nframes = 90, duration=1.8, duration_pad = 2, need padding 5.0 frame=[5246,5300] nframes = 55, duration=1.1, duration_pad = 2, need padding 22.5 frame=[5300,5865] nframes = 566, duration=11.32, duration_pad = 12, need padding 17.0 frame=[5865,6211] nframes = 347, duration=6.94, duration_pad = 7, need padding 1.5
In [ ]: