def detect_colors_folder(folder_path, output_file_path):
img_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith(('.jpg', '.jpeg', '.png'))]
# 打开文件准备写入结果
with open(output_file_path, 'w') as f_output:
for img_path in img_paths:
# 提取文件名
file_name = ntpath.basename(img_path)
# 进行颜色识别并获取统计结果
color_counts = detect_colors(img_path)
# 计算总人数
total_people = sum(color_counts.values())
# 格式化每种颜色的人数字符串
color_distribution = ', '.join([f"{color} {count}人" for color, count in color_counts.items()])
# 将结果写入文件
result_line = f"图片‘{file_name}’中人群总数{total_people}人;{color_distribution}\n"
f_output.write(result_line)
# 在控制台打印结果
print(result_line, end='')
目标检测函数:detect函数用于加载模型、进行目标检测,并可以选择保存检测结果的图像和文本文件。
目标检测流程:对每张图像进行预处理,然后通过模型进行推理,应用非极大值抑制(NMS)算法来去除重叠的检测框,最后可以选择显示或保存结果。
颜色检测函数:detect_colors函数用于对单张图片进行颜色检测,并统计不同颜色的人数。
可以加Q 3562487425交流