python - 从时间点列表中随机选择时间帧作为元组
问题描述
我有几个时间点取自具有最大时间长度 ( T
) 的视频。这些点存储在列表列表中,如下所示:
time_pt_nested_list =
[[0.0, 6.131, 32.892, 43.424, 46.969, 108.493, 142.69, 197.025, 205.793, 244.582, 248.913, 251.518, 258.798, 264.021, 330.02, 428.965],
[11.066, 35.73, 64.784, 151.31, 289.03, 306.285, 328.7, 408.274, 413.64],
[48.447, 229.74, 293.19, 333.343, 404.194, 418.575],
[66.37, 242.16, 356.96, 424.967],
[78.711, 358.789, 403.346],
[84.454, 373.593, 422.384],
[102.734, 394.58],
[158.534],
[210.112],
[247.61],
[340.02],
[365.146],
[372.153]]
上面的每个列表都与一些概率相关联;我想根据其概率从每个列表中随机选择点以形成n
连续时间跨度的元组,例如:
[(0,t1),(t1,t2),(t2,t3),...,(tn,T)]
其中n
由用户指定。所有返回的元组应该只包含上面嵌套列表中的浮点数。我想将最高概率分配给它们进行采样并出现在返回的元组中,第二个列表的概率略低,等等。这些概率的确切细节并不重要,但如果用户可以输入一个就好了控制概率在idx
增加时衰减的速度的参数。
返回的元组是应该完全覆盖整个视频并且不应该重叠的时间帧。0
并且T
可能不一定出现在time_pt_nested_list
(但它们可能)。有没有很好的方法来实现这一点?我将不胜感激任何有见地的建议。
例如,如果用户输入 6 作为子剪辑的数量,那么这将是一个示例输出:
[(0.0, 32.892), (32.892, 64.784), (64.784, 229.74), (229.74, 306.285), (306.285, 418.575), (418.575, 437.47)]
元组中出现的所有数字都出现在 中time_pt_nested_list
,除了0.0
和437.47
。(0.0
这里确实出现了,但在其他情况下可能不会出现)这里437.47
也是给出的视频长度,可能不会出现在列表中。
解决方案
这比看起来要简单。您实际上只需n
要从子列表中采样点,每个点都具有与行相关的样本概率。无论获得什么样本,都可以按时间排序来构建您的元组。
import numpy as np
# user params
n = 6
prob_falloff_param = 0.2
lin_list = sorted([(idx, el) for idx, row in enumerate(time_pt_nested_list) for
el in row], key=lambda x: x[1])
# endpoints required, excluded from random selection process
t0 = lin_list.pop(0)[1]
T = lin_list.pop(-1)[1]
arr = np.array(lin_list)
# define row weights, alpha is parameter
weights = np.exp(-prob_falloff_param*arr[:,0]**2)
norm_weights = weights/np.sum(weights)
# choose (weighted) random points, create tuple list:
random_points = sorted(np.random.choice(arr[:,1], size=(n-1), replace=False))
time_arr = [t0, *random_points, T]
output = list(zip(time_arr, time_arr[1:]))
示例输出:
# n = 6
[(0.0, 78.711),
(78.711, 84.454),
(84.454, 158.534),
(158.534, 210.112),
(210.112, 372.153),
(372.153, 428.965)]
# n = 12
[(0.0, 6.131),
(6.131, 43.424),
(43.424, 64.784),
(64.784, 84.454),
(84.454, 102.734),
(102.734, 210.112),
(210.112, 229.74),
(229.74, 244.582),
(244.582, 264.021),
(264.021, 372.153),
(372.153, 424.967),
(424.967, 428.965)]
推荐阅读
- c# - 通过 Outlook 发送电子邮件
- generics - 以元组结构的类型作为参数调用泛型函数是什么意思?
- angular - 在 Angular 7 注册表单错误处理中出现错误,例如缺少电子邮件@
- node.js - NodeJS 12.x:AWS Greengrass 中未找到错误
- sqlite - 如何在 Ubuntu 18.04 上安装或升级到 sqlite 3.33.0?
- apostrophe-cms - 如何强制撇号 cms 中的页面树?
- python - 如何绘制在 Y 轴上显示总人口百分比的分布曲线
- swift - ForEach onDelete 不适用于 LazyVStack
- c# - 使用 Winforms 使用 Combobox 绑定空值
- python - 从烧瓶函数内部返回值