318 KiB
318 KiB
In [2]:
from pathlib import Path
import numpy as np
# head, ssim_ref_3740
fname = "head_black"
path_root = Path("/mfs/lsen/DATA/Actor4/bg_black")
path_ssim = path_root.joinpath(fname, "video", "ssim_ref_3739.npy")
data_ssim = np.load(path_ssim)
print(len(data_ssim))
6482
In [3]:
# Importing packages
import matplotlib.pyplot as plt
%matplotlib inline
# from jupyterthemes import jtplot
# jtplot.style(grid=False)
data_tmp = data_ssim[::10]
# 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
height = 0.93
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 = 505
In [5]:
# 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
# last value
if cur_idx - peaks_filter[-1] > nwind:
peaks_filter.append(prev_idx)
# peaks_filter.append(prev_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 = 50
In [6]:
# 精细校准
peaks_filter_old2 = peaks_filter.copy()
list_remove = [620, 1027, 1573, 2287, 2420, 2564, 2809, 2942, 3158, 3214, 3322, 3458, 3791, 3857, 3985, 4622, 4832, 5091, 5157, 5389, 5539, 5593, 5826, 5878, 5972, 6047]
# list_remove = [1570, 2412, 2562, 2941, 3213, 5091, 5878, 6047, 6313]
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 [7]:
# 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, [3739] + 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/head_black/video/reference_peaks.txt peaks = [164, 1136, 1521, 1743, 1877, 1979, 2218, 2756, 3100, 3401, 3739, 3923, 4090, 4447, 4565, 4719, 4983, 5273, 5338, 5463, 5764, 6165, 6314, 6470] frame=[164,1136] nframes = 973, duration=19.46, duration_pad = 20, need padding 13.5 frame=[1136,1521] nframes = 386, duration=7.72, duration_pad = 8, need padding 7.0 frame=[1521,1743] nframes = 223, duration=4.46, duration_pad = 5, need padding 13.5 frame=[1743,1877] nframes = 135, duration=2.7, duration_pad = 3, need padding 7.5 frame=[1877,1979] nframes = 103, duration=2.06, duration_pad = 3, need padding 23.5 frame=[1979,2218] nframes = 240, duration=4.8, duration_pad = 5, need padding 5.0 frame=[2218,2756] nframes = 539, duration=10.78, duration_pad = 11, need padding 5.5 frame=[2756,3100] nframes = 345, duration=6.9, duration_pad = 7, need padding 2.5 frame=[3100,3401] nframes = 302, duration=6.04, duration_pad = 7, need padding 24.0 frame=[3401,3739] nframes = 339, duration=6.78, duration_pad = 7, need padding 5.5 frame=[3739,3923] nframes = 185, duration=3.7, duration_pad = 4, need padding 7.5 frame=[3923,4090] nframes = 168, duration=3.36, duration_pad = 4, need padding 16.0 frame=[4090,4447] nframes = 358, duration=7.16, duration_pad = 8, need padding 21.0 frame=[4447,4565] nframes = 119, duration=2.38, duration_pad = 3, need padding 15.5 frame=[4565,4719] nframes = 155, duration=3.1, duration_pad = 4, need padding 22.5 frame=[4719,4983] nframes = 265, duration=5.3, duration_pad = 6, need padding 17.5 frame=[4983,5273] nframes = 291, duration=5.82, duration_pad = 6, need padding 4.5 frame=[5273,5338] nframes = 66, duration=1.32, duration_pad = 2, need padding 17.0 frame=[5338,5463] nframes = 126, duration=2.52, duration_pad = 3, need padding 12.0 frame=[5463,5764] nframes = 302, duration=6.04, duration_pad = 7, need padding 24.0 frame=[5764,6165] nframes = 402, duration=8.04, duration_pad = 9, need padding 24.0 frame=[6165,6314] nframes = 150, duration=3.0, duration_pad = 4, need padding 25.0 frame=[6314,6470] nframes = 157, duration=3.14, duration_pad = 4, need padding 21.5
In [15]:
processing the peak 164, range=[164,1136] nframes = 973, duration=19.46, duration_pad = 20, need padding 13.5 nframe_pad_head= 13, nframe_pad_tail=14 len=13 = [2, 5, 7, 10, 12, 14, 17, 19, 21, 24, 26, 29, 32] len=14 = [2, 4, 7, 9, 11, 13, 16, 18, 20, 22, 24, 27, 29, 32] total have 1000 files, duration = 20.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpuow12p19.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor4/bg_black/head_black/video/split_00_interplote.mp4
In [ ]: