Skip to content

Commit

Permalink
fix(video_processor): cpu提取关键帧失败; 修复 KMeans 聚类失败问题
Browse files Browse the repository at this point in the history
- 增加对空镜头帧的检查
- 添加异常捕获,当 KMeans 聚类失败时使用备选方案
- 备选方案:选择镜头中间的帧作为关键帧
-优化代码结构,提高鲁棒性
  • Loading branch information
linyqh committed Nov 26, 2024
1 parent 9c58102 commit 593b427
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/utils/video_processor_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,21 @@ def extract_keyframes(self, frames: List[np.ndarray], shot_boundaries: List[int]
end = shot_boundaries[i]
shot_frames = frames[start:end]

if not shot_frames:
continue

# 将每一帧转换为灰度图并展平为一维数组
frame_features = np.array([cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).flatten()
for frame in shot_frames])
kmeans = KMeans(n_clusters=1, random_state=0).fit(frame_features)
center_idx = np.argmin(np.sum((frame_features - kmeans.cluster_centers_[0]) ** 2, axis=1))

try:
# 尝试使用 KMeans
kmeans = KMeans(n_clusters=1, random_state=0).fit(frame_features)
center_idx = np.argmin(np.sum((frame_features - kmeans.cluster_centers_[0]) ** 2, axis=1))
except Exception as e:
logger.warning(f"KMeans 聚类失败,使用备选方案: {str(e)}")
# 备选方案:选择镜头中间的帧作为关键帧
center_idx = len(shot_frames) // 2

keyframes.append(shot_frames[center_idx])
keyframe_indices.append(start + center_idx)
Expand Down

0 comments on commit 593b427

Please sign in to comment.