605 KiB
605 KiB
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 = "4R3A0204"
# idx_reference = 3739
# path_root = Path("/mfs/lsen/DATA/Actor6/bg/processing")
path_root = Path("/mfs/lsen/DATA/Actor6/bg/processing2_0810")
path_frame = path_root.joinpath(fname, "frames")
path_video = path_root.joinpath(fname, "video")
path_ldmks = path_root.joinpath(fname, "openpose")
path_lmd = path_video.joinpath(f"lmd_ref_mean.npy")
# path_lmd = "../../data/Actor6/openpose_mean_outfit2.npy"
data_lmd = np.load(path_lmd)
print(data_lmd.shape)(8321,)
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.ylim(-100, 0.1)
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 [5]:
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(-1000, 1)
plt.show()
In [8]:
from scipy.signal import find_peaks
x = - data_lmd
height = -250
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(-1000, 0.1)
plt.grid(True)
plt.xticks(peaks,rotation=45)
plt.show()
find peaks = 149
In [10]:
# filter some range
peaks_filter = filter_peaks(x, 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(-1000, 0.1)
plt.grid(True)
plt.xticks(peaks_filter,rotation=45)
plt.show()
# save filter peaks
filter peaks = 44 filter peaks = 44 save to > /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/reference_peaks_by_mean_lmd.txt
In [ ]:
In [11]:
# 精细校准
list_remove = [2435, 8156 ]
for irm in list_remove:
peaks_filter.remove(irm)
# save
path_peaks_save = path_video.joinpath("reference_peaks_landmarks.txt")
# save peaks
peaks_save = peaks_filter
np.savetxt(path_peaks_save, peaks_filter, 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 (len=42) in files > /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/reference_peaks_landmarks.txt peaks = [157, 285, 590, 736, 883, 1098, 1246, 1397, 1530, 1759, 1919, 2138, 2228, 2381, 2522, 2842, 3279, 3409, 3543, 3679, 3850, 3971, 4146, 4303, 4522, 4976, 5166, 5297, 5496, 5859, 6059, 6194, 6379, 6580, 6729, 6974, 7183, 7362, 7535, 7764, 7913, 8103]
In [12]:
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 [13]:
# cut video
ipath_video_save = path_video.joinpath(f"video_split_ref_mean_small")
ipath_video_save.mkdir(parents=True, exist_ok=True)
# ipath_video_log = path_video.joinpath(f"video_split_ref_mean2.manifest")
# dict_manifest = {}
for i in range(0, len(peaks_filter)-1):
# if i > 15: break
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}_{idx_beg:05d}_{idx_end:05d}.mp4")
concate_video(path_frame, idx_beg, idx_end, ipath_save)
frame=[157,285] nframes = 129, duration=2.58, duration_pad = 3, need padding 10.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpnomm6il_.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_00_00157_00285.mp4 frame=[157,285] duration=2.56s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_00_00157_00285.mp4 frame=[285,590] nframes = 306, duration=6.12, duration_pad = 7, need padding 22.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpxjhi8ec1.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_01_00285_00590.mp4 frame=[285,590] duration=6.1s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_01_00285_00590.mp4 frame=[590,736] nframes = 147, duration=2.94, duration_pad = 3, need padding 1.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmp0jgaxrqs.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_02_00590_00736.mp4 frame=[590,736] duration=2.92s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_02_00590_00736.mp4 frame=[736,883] nframes = 148, duration=2.96, duration_pad = 3, need padding 1.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpfhss_y30.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_03_00736_00883.mp4 frame=[736,883] duration=2.94s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_03_00736_00883.mp4 frame=[883,1098] nframes = 216, duration=4.32, duration_pad = 5, need padding 17.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpgody1byw.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_04_00883_01098.mp4 frame=[883,1098] duration=4.3s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_04_00883_01098.mp4 frame=[1098,1246] nframes = 149, duration=2.98, duration_pad = 3, need padding 0.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpzizz913b.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_05_01098_01246.mp4 frame=[1098,1246] duration=2.96s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_05_01098_01246.mp4 frame=[1246,1397] nframes = 152, duration=3.04, duration_pad = 4, need padding 24.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmponn7uefl.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_06_01246_01397.mp4 frame=[1246,1397] duration=3.02s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_06_01246_01397.mp4 frame=[1397,1530] nframes = 134, duration=2.68, duration_pad = 3, need padding 8.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpvjh_luvv.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_07_01397_01530.mp4 frame=[1397,1530] duration=2.66s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_07_01397_01530.mp4 frame=[1530,1759] nframes = 230, duration=4.6, duration_pad = 5, need padding 10.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmprkr4ysw4.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_08_01530_01759.mp4 frame=[1530,1759] duration=4.58s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_08_01530_01759.mp4 frame=[1759,1919] nframes = 161, duration=3.22, duration_pad = 4, need padding 19.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpzzce7ht_.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_09_01759_01919.mp4 frame=[1759,1919] duration=3.2s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_09_01759_01919.mp4 frame=[1919,2138] nframes = 220, duration=4.4, duration_pad = 5, need padding 15.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmp70f1x61j.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_10_01919_02138.mp4 frame=[1919,2138] duration=4.38s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_10_01919_02138.mp4 frame=[2138,2228] nframes = 91, duration=1.82, duration_pad = 2, need padding 4.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpg3qadx3u.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_11_02138_02228.mp4 frame=[2138,2228] duration=1.8s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_11_02138_02228.mp4 frame=[2228,2381] nframes = 154, duration=3.08, duration_pad = 4, need padding 23.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmp71bn9nq0.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_12_02228_02381.mp4 frame=[2228,2381] duration=3.06s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_12_02228_02381.mp4 frame=[2381,2522] nframes = 142, duration=2.84, duration_pad = 3, need padding 4.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmplbcha4z0.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_13_02381_02522.mp4 frame=[2381,2522] duration=2.82s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_13_02381_02522.mp4 frame=[2522,2842] nframes = 321, duration=6.42, duration_pad = 7, need padding 14.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpmvm93k1r.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_14_02522_02842.mp4 frame=[2522,2842] duration=6.4s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_14_02522_02842.mp4 frame=[2842,3279] nframes = 438, duration=8.76, duration_pad = 9, need padding 6.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpw6sqwnt5.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_15_02842_03279.mp4 frame=[2842,3279] duration=8.74s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_15_02842_03279.mp4 frame=[3279,3409] nframes = 131, duration=2.62, duration_pad = 3, need padding 9.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpdv15gq0x.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_16_03279_03409.mp4 frame=[3279,3409] duration=2.6s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_16_03279_03409.mp4 frame=[3409,3543] nframes = 135, duration=2.7, duration_pad = 3, need padding 7.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpe4ueke88.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_17_03409_03543.mp4 frame=[3409,3543] duration=2.68s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_17_03409_03543.mp4 frame=[3543,3679] nframes = 137, duration=2.74, duration_pad = 3, need padding 6.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmp1l4vodss.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_18_03543_03679.mp4 frame=[3543,3679] duration=2.72s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_18_03543_03679.mp4 frame=[3679,3850] nframes = 172, duration=3.44, duration_pad = 4, need padding 14.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpnp7z0w2u.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_19_03679_03850.mp4 frame=[3679,3850] duration=3.42s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_19_03679_03850.mp4 frame=[3850,3971] nframes = 122, duration=2.44, duration_pad = 3, need padding 14.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpwklhc67e.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_20_03850_03971.mp4 frame=[3850,3971] duration=2.42s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_20_03850_03971.mp4 frame=[3971,4146] nframes = 176, duration=3.52, duration_pad = 4, need padding 12.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpmctb1iqn.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_21_03971_04146.mp4 frame=[3971,4146] duration=3.5s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_21_03971_04146.mp4 frame=[4146,4303] nframes = 158, duration=3.16, duration_pad = 4, need padding 21.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpcxu4j_5j.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_22_04146_04303.mp4 frame=[4146,4303] duration=3.14s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_22_04146_04303.mp4 frame=[4303,4522] nframes = 220, duration=4.4, duration_pad = 5, need padding 15.0 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpei_nghei.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_23_04303_04522.mp4 frame=[4303,4522] duration=4.38s. save video to /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_23_04303_04522.mp4 frame=[4522,4976] nframes = 455, duration=9.1, duration_pad = 10, need padding 22.5 ffmpeg -y -r 50 -f concat -safe 0 -i /tmp/tmpbl68ly1j.txt -c:v libx264 -pix_fmt yuv420p -loglevel error /mfs/lsen/DATA/Actor6/bg/processing2_0810/4R3A0204/video/video_split_ref_mean_small/split_24_04522_04976.mp4
In [ ]: