python - 将参数添加到多标题
问题描述
我已经使用 OpenCV 创建了一个用于图像旋转的管道,以及另一个将多线程应用到管道的函数。
管道:
def rotation_pipeline(img, rotation):
# Rotation options
rotation_options = {
'90':cv2.ROTATE_90_CLOCKWISE,
'180':cv2.ROTATE_180,
'270':cv2.ROTATE_90_COUNTERCLOCKWISE
}
# Rotate single image
img_name = img.split('/')[-1]
read_image = cv2.imread(img)
rotated_image = cv2.rotate(read_image, rotation_options[rotation])
cv2.imwrite(f'../processed_data/jpeg/augmented/{rotation}_degree_{img_name}', rotated_image)
print(f'{img_name} rotated {rotation} degrees.')
以及多线程功能:
# Rotations
def rotate_images(rotation):
'''
Options for `rotation` are '90', '180', and '270'.
'''
# Read in metadata
PATH = '../data/jpeg/train/'
df = pd.read_csv('../data/train.csv')
augmentation_df = df[df['target'] == 1]
# Obtain image names
melanoma_names = augmentation_df['image_name'].values
image_paths = PATH + melanoma_names + '.jpg'
# Create new metadata
augmentation_df['image_name'] = augmentation_df['image_name'].map(lambda x: f'{rotation}_degree_'+x)
# Multithread rotations
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(rotation_pipeline, image_paths, ('rotation',rotation))
# Add metadata to csv
if 'augmented_metadata.csv' in os.listdir('../processed_data/'):
augmented_csv = pd.read_csv('../processed_data/augmented_metadata.csv')
augmentation_df = pd.concat([augmented_csv, augmentation_df], axis=0)
augmentation_df.to_csv('../processed_data/augmented_metadata.csv')
该代码旨在包含在我的预处理管道中,如下所示:
rotate_images('90')
rotate_images('180')
rotate_images('270')
我已经在笔记本中测试了代码,使用rotate_melanoma_images('90')
. 但这仅运行一个图像。我检查了输出目录并确认只有一个图像被旋转。
根据文档,
对于多个可迭代对象,迭代器会在最短的可迭代对象耗尽时停止。
我假设只运行一个项目的原因是因为只有一个 arg,因此“最短的可迭代对象已用尽”。话虽如此,似乎有一种方法可以将 args 添加到concurrent.futures.ThreadPoolExecutor
用于列表中每个项目的函数中。不过,我无法在网上找到任何解释如何做到这一点的东西——有人知道这是如何工作的吗?
解决方案
推荐阅读
- wordpress - 使 WooCommerce 缩略图图像大于主要产品图像
- python-3.x - 需要帮助解决 python 中的 selenium webdriver
- monads - 无法声明 MonadPlus 接口受 Monad 约束
- php - 使用标签的可见文本替换 qualifying 的 href 值
- c++ - 如何安全地重载 std::unique_ptr 的自定义删除器?
- javascript - 输入框包含某些值时的浮动标签
- javascript - 使用 throw 错误时,passport.authenticate() 中出现未处理的承诺拒绝错误
- join - 为什么在 pyspark 中加入两个临时视图后删除列不起作用,但它适用于数据框连接?
- python - 插入排序算法:哪些代码在其结构上是高效的,怎么来的?
- python - Python:将输出行中的第二个值分配给变量