Video motion score calculation
Details | Caution and prerequisites |
|---|---|
Costs | Before calling an operator, you need to understand the model invocation costs associated with using the operator. For details, see Large model invocation billing. |
Authentication (API Key) | Before calling an operator, you need to generate an API Key for operator invocation. It is recommended to configure the API Key as an environment variable to ensure safer operator calls. For details, see Obtain and configure API Key. |
BaseURL | Before calling an operator, you need to determine the BaseURL for operator invocation based on the region where your current LAS service is deployed. This is used to configure the path parameter values for operator calls. |
Input column names | Note |
|---|---|
video_paths | Video file path column (supports local, TOS, HTTP, and other sources). Choose either video_paths or video_binaries. |
video_binaries | Video binary data column. Choose either video_paths or video_binaries. |
video_formats | Video format string column (such as "mp4", "avi"), used together with video_binaries. |
Structured array (Struct) containing the results of motion score calculation. Each element includes the following fields:
If a parameter does not have a default value, it is required.
Parameter name | Type | Default value | Description |
|---|---|---|---|
optical_flow_algorithm | str | "farneback" | Type of dense optical flow algorithm. |
sample_ratio | float | 0.125 | Video frame sampling ratio, used to balance computational efficiency and accuracy. |
mag_threshold | float | 0.01 | Motion magnitude threshold, filters out minor noise displacements (pixels). |
flow_threshold | float | 6 | Optical flow outlier threshold. |
smooth_window | int | 5 | Smoothing window size. |
downsample_ratio | float | 1.0 | Frame downsampling ratio (0.0-1.0), 1.0 means original resolution. |
high_motion_threshold | float | 0.02 | High-motion video determination threshold. |
batch_size | int | 10 | Batch size. |
model_path | str | "/opt/las/models" | Model file storage path (used for MEMFOF). |
algorithm_params | dict | None | Algorithm-specific parameter dictionary. |
rank | int | 0 | Specifies the GPU device number to use (effective in multi-GPU environments). |
num_workers | int | 4 | Number of multithreaded worker processes. |
use_cuda | bool | true | Enable CUDA acceleration. |
max_gpu_memory | float | 0.8 | Maximum GPU memory usage limit (0.0-1.0). |
min_frame_size | int | 32 | Minimum frame size (width/height); frames smaller than this will be scaled up. |
The following code demonstrates how to use Daft (for distributed environments) to run the operator for video motion score calculation.
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() # ╭────────────────────────────────┬──────────┬──────────────┬──────────────┬────────────────┬────────────┬─────────────┬────────┬──────────┬─────────────────────┬──────────╮ # │ Video path ┆ Algorithm used ┆ Normalized motion score ┆ Average motion magnitude ┆ Median motion magnitude ┆ … ┆ Video resolution ┆ Total frames ┆ Processing status ┆ Average processing time per frame (s) ┆ Sampled frames │ # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ # │ 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 │ # ╰────────────────────────────────┴──────────┴──────────────┴──────────────┴────────────────┴────────────┴─────────────┴────────┴──────────┴─────────────────────┴──────────╯