fix(video_processor): cpu提取关键帧失败; 修复 KMeans 聚类失败问题

- 增加对空镜头帧的检查
- 添加异常捕获,当 KMeans 聚类失败时使用备选方案
- 备选方案:选择镜头中间的帧作为关键帧
-优化代码结构,提高鲁棒性
This commit is contained in:
linyqh 2024-11-23 18:54:13 +08:00 committed by linyq
parent 9c58102558
commit 593b427061

View File

@ -88,10 +88,21 @@ class VideoProcessor:
end = shot_boundaries[i] end = shot_boundaries[i]
shot_frames = frames[start:end] shot_frames = frames[start:end]
if not shot_frames:
continue
# 将每一帧转换为灰度图并展平为一维数组
frame_features = np.array([cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).flatten() frame_features = np.array([cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).flatten()
for frame in shot_frames]) for frame in shot_frames])
try:
# 尝试使用 KMeans
kmeans = KMeans(n_clusters=1, random_state=0).fit(frame_features) 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)) 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]) keyframes.append(shot_frames[center_idx])
keyframe_indices.append(start + center_idx) keyframe_indices.append(start + center_idx)