[Bug] Incomplete handling of frames without faces causing frame count mismatch and index errors

Author: suntpCreated Mar 5, 2026Updated Mar 5, 2026

问题描述 / Problem Description

中文: 在处理视频时,如果某些帧检测不到人脸,当前实现只是简单地跳过,这会导致帧数不匹配和后续处理中的索引越界错误。

English: When processing videos, if certain frames fail to detect faces, the current implementation simply skips them, which causes frame count mismatches and subsequent index out-of-bounds errors.

问题位置 / Location

文件 / File: src/utils/cropper.py
行号 / Line: 222

问题代码 / Problematic Code

python
# TODO: support skipping frame with NO FACE
if len(src_face) == 0:
    log(f"No face detected in the frame #{idx}")
    continue  # 只是跳过,但没有正确处理 / Just skip, but not handled properly

影响 / Impact

中文:

  1. 当视频中某些帧检测不到人脸时,会导致帧数不匹配
  2. 可能引发后续处理的索引越界错误
  3. 长视频中容易出现此问题,导致处理失败

English:

  1. When certain frames in a video fail to detect faces, it causes frame count mismatches
  2. May trigger index out-of-bounds errors in subsequent processing
  3. This problem frequently occurs in long videos, causing processing failures

建议修复方案 / Suggested Solutions

方案1: 使用前一帧的关键点 / Solution 1: Use Previous Frame's Landmarks

python
if len(src_face) == 0:
    log(f"No face detected in the frame #{idx}")
    # 使用前一帧的关键点进行插值 / Interpolate using previous frame's landmarks
    if trajectory.lmk_lst:
        trajectory.lmk_lst.append(trajectory.lmk_lst[-1])
        trajectory.end = idx
        continue
    else:
        raise FaceDetectionError(f"No face in first frame #{idx}")

方案2: 抛出明确的异常 / Solution 2: Throw Explicit Exception

python
class FaceDetectionError(Exception):
    """人脸检测失败异常 / Face detection failure exception"""
    pass

if len(src_face) == 0:
    raise FaceDetectionError(f"No face detected in frame #{idx}")

方案3: 提供配置选项 / Solution 3: Provide Configuration Options

python
# 在配置中添加 / Add to configuration
class CropConfig:
    # ...
    skip_frames_without_face: bool = False
    max_consecutive_skip_frames: int = 5

# 处理逻辑 / Processing logic
if len(src_face) == 0:
    if crop_cfg.skip_frames_without_face:
        # 跳过逻辑 / Skip logic
    else:
        # 插值逻辑 / Interpolation logic

优先级 / Priority

P0 - 建议立即修复 / Recommend immediate fix

相关信息 / Related Information

  • 发现时间 / Discovered: 2026-03-05
  • 分析方法 / Analysis Method: 代码审查 / Code Review
  • 相关 TODO: TODO: support skipping frame with NO FACE

Source: KlingAIResearch/LivePortrait