调整了选图逻辑

This commit is contained in:
jinye_huang 2025-05-16 17:29:20 +08:00
parent e457c0295e
commit 13eebff18b

View File

@ -202,19 +202,32 @@ class PosterNotesCreator:
logger.info(f"开始为主题 {topic_index} 变体 {variant_index} 选择额外配图")
# 获取候选图像 - 我们需要至少4*num_additional_images张图片
num_source_images_needed = min(4 * num_additional_images, 12) # 限制最多12张源图
# num_source_images_needed = min(4 * num_additional_images, 12) # 限制最多12张源图 <-- 旧逻辑
total_images_needed = 4 * num_additional_images # 计算总共需要的唯一图片数量
candidate_images = self.get_candidate_images(
poster_metadata_path,
source_image_dir,
num_source_images_needed
# num_source_images_needed <-- 旧逻辑
total_images_needed # 请求总共需要的图片数量
)
if not candidate_images:
logger.warning("没有找到合适的候选图像")
return []
if len(candidate_images) < 4:
# 检查是否有足够的图片来生成请求数量的配图
if len(candidate_images) < total_images_needed:
adjusted_num_images = len(candidate_images) // 4
logger.warning(
f"可用图像数量 ({len(candidate_images)}) 不足以生成 {num_additional_images} 张不重复的2x2配图 "
f"(需要 {total_images_needed} 张)。将只生成 {adjusted_num_images} 张配图。"
)
num_additional_images = adjusted_num_images
if num_additional_images == 0:
logger.warning("可用图像数量少于4张无法创建任何2x2拼图。")
return []
elif len(candidate_images) < 4: # 即使调整后检查是否仍少于4张
logger.warning(f"可用图像数量({len(candidate_images)})少于4张无法创建2x2拼图")
return []
@ -232,24 +245,35 @@ class PosterNotesCreator:
with concurrent.futures.ProcessPoolExecutor(max_workers=min(4, num_additional_images)) as executor:
# 创建任务
future_to_image_set = {}
start_index = 0 # 用于追踪从candidate_images中取图的起始位置
for i in range(num_additional_images):
# 为每个输出选择4张不同的图片
selected_indices = []
# 确保我们有足够的图片可选择
available_indices = list(range(len(candidate_images)))
# # 为每个输出选择4张不同的图片 <-- 旧逻辑,改为切
# selected_indices = []
# # 确保我们有足够的图片可选择
# available_indices = list(range(len(candidate_images)))
# 如果图片不够,我们可能需要重复使用一些图片
if len(available_indices) < 4:
selected_indices = available_indices * (4 // len(available_indices) + 1)
selected_indices = selected_indices[:4]
else:
# 随机选择4个不同的索引
selected_indices = random.sample(available_indices, 4)
# # 如果图片不够,我们可能需要重复使用一些图片
# if len(available_indices) < 4:
# selected_indices = available_indices * (4 // len(available_indices) + 1)
# selected_indices = selected_indices[:4]
# else:
# # 随机选择4个不同的索引
# selected_indices = random.sample(available_indices, 4)
# 获取对应的图片文件名
selected_images = [candidate_images[idx] for idx in selected_indices]
# # 获取对应的图片文件名
# selected_images = [candidate_images[idx] for idx in selected_indices]
# --- 新逻辑:从打乱后的列表中顺序切片获取不重复的图像 ---
end_index = start_index + 4
if end_index > len(candidate_images): # 双重检查,理论上不应发生
logger.error(f"内部错误:尝试获取的图像索引超出范围 ({start_index}-{end_index}),可用图像: {len(candidate_images)}")
break
selected_images = candidate_images[start_index:end_index]
start_index = end_index # 更新下一个起始索引
# --- 结束新逻辑 ---
# 为每个拼图创建单独的种子
image_seed = seed + i