视频运动分计算
细分项 | 注意与前提 |
|---|---|
费用 | 调用算子前,您需先了解使用算子时的模型调用费用,详情请参见大模型调用计费。 |
鉴权(API Key) | 调用算子前,您需要先生成算子调用的API Key,并建议将API Key配置为环境变量,便于更安全地调用算子,详情请参见获取 API Key 并配置。 |
BaseURL | 调用算子前,您需要先根据您当前使用的LAS服务所在地域,了解算子调用的BaseURL,用于配置算子调用路径参数取值。 |
输入列名 | 说明 |
|---|---|
video_paths | 视频文件路径列(支持本地、TOS、HTTP等)。与 video_binaries 二选一。 |
video_binaries | 视频二进制数据列。与 video_paths 二选一。 |
video_formats | 视频格式字符串列(如 "mp4", "avi"),配合 video_binaries 使用。 |
包含运动分计算结果的结构化数组(Struct),每个元素包含以下字段:
如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
optical_flow_algorithm | str | "farneback" | 稠密光流算法类型。 |
sample_ratio | float | 0.125 | 视频采样帧数比例,用于平衡计算效率与精度。 |
mag_threshold | float | 0.01 | 运动幅值阈值,过滤微小噪声位移(像素)。 |
flow_threshold | float | 6 | 光流异常值阈值。 |
smooth_window | int | 5 | 平滑窗口大小。 |
downsample_ratio | float | 1.0 | 帧降采样比例(0.0-1.0),1.0为原始分辨率。 |
high_motion_threshold | float | 0.02 | 高动态视频判断阈值。 |
batch_size | int | 10 | 批处理大小。 |
model_path | str | "/opt/las/models" | 模型文件存储路径(用于MEMFOF)。 |
algorithm_params | dict | None | 算法特定参数字典。 |
rank | int | 0 | 指定使用的GPU设备编号(多卡环境有效)。 |
num_workers | int | 4 | 多线程工作进程数。 |
use_cuda | bool | true | 是否启用CUDA加速。 |
max_gpu_memory | float | 0.8 | GPU最大内存使用率限制(0.0-1.0)。 |
min_frame_size | int | 32 | 最小帧尺寸(宽/高),低于此值会被缩放。 |
下面的代码展示了如何使用 Daft(适用于分布式)运行算子对视频进行运动分计算。
from __future__ import annotations import os import daft from daft import col from daft.las.functions.udf import las_udf from daft.las.functions.video import VideoMotionScore if __name__ == "__main__": if os.getenv("DAFT_RUNNER", "native") == "ray": import logging import ray def configure_logging(): logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S.%s".format(), ) logging.getLogger("tracing.span").setLevel(logging.WARNING) logging.getLogger("daft_io.stats").setLevel(logging.WARNING) logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING) ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging}) daft.set_runner_ray() daft.set_execution_config(actor_udf_ready_timeout=600) daft.set_execution_config(min_cpu_per_task=0) tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com") samples = { "video_path": [f"https://{tos_dir_url}/public/shared_video_dataset/singer.mp4"], } ds = daft.from_pydict(samples) constructor_kwargs = { "optical_flow_algorithm": "dis-fast", "sample_ratio": 0.03, "use_cuda": True, "num_workers": 1, "batch_size": 10, "downsample_ratio": 1.0, "model_path": "/opt/las/models", "mag_threshold": 1.5, "flow_threshold": 6, } ds = ds.with_column( "motion_results", las_udf(VideoMotionScore, construct_args=constructor_kwargs)(col("video_path")), ) ds = ds.select( col("video_path").alias("视频路径"), col("motion_results")["used_algorithm"].alias("使用算法"), col("motion_results")["standardized_score"].alias("标准化运动分"), col("motion_results")["mean_score"].alias("平均运动幅值"), col("motion_results")["median_score"].alias("运动幅值中位数"), col("motion_results")["dynamic_mean_score"].alias("尺寸自适应均值"), col("motion_results")["high_percentile_score"].alias("原始95分位"), col("motion_results")["dynamic_high_percentile_score"].alias("尺寸自适应95分位"), col("motion_results")["motion_pattern"].alias("运动等级"), col("motion_results")["video_resolution"].alias("视频分辨率"), col("motion_results")["total_frames"].alias("总帧数"), col("motion_results")["status"].alias("处理状态"), col("motion_results")["avg_frame_process_time"].alias("单帧平均处理时间(s)"), col("motion_results")["sample_frames_count"].alias("采样帧数"), ) ds.show() # ╭────────────────────────────────┬──────────┬──────────────┬──────────────┬────────────────┬────────────┬─────────────┬────────┬──────────┬─────────────────────┬──────────╮ # │ 视频路径 ┆ 使用算法 ┆ 标准化运动分 ┆ 平均运动幅值 ┆ 运动幅值中位数 ┆ … ┆ 视频分辨率 ┆ 总帧数 ┆ 处理状态 ┆ 单帧平均处理时间(s) ┆ 采样帧数 │ # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ # │ String ┆ String ┆ Float32 ┆ Float32 ┆ Float32 ┆ (4 hidden) ┆ List[Int32] ┆ Int64 ┆ String ┆ Float64 ┆ Int64 │ # ╞════════════════════════════════╪══════════╪══════════════╪══════════════╪════════════════╪════════════╪═════════════╪════════╪══════════╪═════════════════════╪══════════╡ # │ https://las-ai-qa-online.tos-… ┆ dis-fast ┆ 0.57431227 ┆ 36.396885 ┆ 35.626892 ┆ … ┆ [640, 360] ┆ 665 ┆ success ┆ 0.015 ┆ 21 │ # ╰────────────────────────────────┴──────────┴──────────────┴──────────────┴────────────────┴────────────┴─────────────┴────────┴──────────┴─────────────────────┴──────────╯