问题描述
我有一个标志。我现在正在做的是为所有这些函数设置概率权重,并且徽标一次只出现在其中一个,具体取决于其概率值。这是我的代码:
augmentation_func = [(random_rotation,[brand_logo]),(blurring,(compression,(portion_covering,[brand_logo,brand_logo_width,brand_logo_height]),(perspective_transform,[logo_image,(overlaying,[logo_path]),(logo_background,[logo_path])]
(augs,args),= random.choices(augmentation_func,weights=[10,5,15,20,15])
new_logo = random_resize(augs(*args),gameplay)
然而,这还不够。标志应该能够通过多种功能。例如。徽标可能会通过 random_rotation()
、perspective_transformation()
和 overlaying()
。它是否会通过一个函数应该取决于它的概率权重。这是我的想法:
解决方法
每个操作发生的机会是否独立于其他操作?那么 random.choice()
不是正确的解决方案,因为它只选择了一项。相反,对于每个项目,以其相应的概率选择它。例如:
if random.randrange(0,100) < 10:
# Crop the image (in place) at a 10% chance.
# The chance of cropping is independent
# of the chances of any other operation.
brand_logo = cropping(brand_logo)
if random.randrange(0,100) < 15:
# Rotate the image (in place) at a 15% chance.
# The chance of rotating is independent
# of the chances of any other operation.
brand_logo = rotating(brand_logo)