[docs]@OPERATORS.register_module(OP_NAME)@LOADED_IMAGES.register_module(OP_NAME)classImageBlurMapper(Mapper):"""Mapper to blur images."""
[docs]def__init__(self,p:float=0.2,blur_type:str="gaussian",radius:float=2,save_dir:str=None,*args,**kwargs):""" Initialization method. :param p: Probability of the image being blurred. :param blur_type: Type of blur kernel, including ['mean', 'box', 'gaussian']. :param radius: Radius of blur kernel. :param save_dir: The directory where generated image files will be stored. If not specified, outputs will be saved in the same directory as their corresponding input files. This path can alternatively be defined by setting the `DJ_PRODUCED_DATA_DIR` environment variable. :param args: extra args :param kwargs: extra args """super().__init__(*args,**kwargs)self._init_parameters=self.remove_extra_parameters(locals())self._init_parameters.pop("save_dir",None)ifblur_typenotin["mean","box","gaussian"]:raiseValueError(f"Blur_type [{blur_type}] is not supported. "f'Can only be one of ["mean", "box", "gaussian"]. ')ifradius<0:raiseValueError("Radius must be >= 0. ")self.p=pfromPILimportImageFilterifblur_type=="mean":self.blur=ImageFilter.BLURelifblur_type=="box":self.blur=ImageFilter.BoxBlur(radius)else:self.blur=ImageFilter.GaussianBlur(radius)self.save_dir=save_dir
[docs]defprocess_single(self,sample,context=False):# there is no image in this sampleifself.image_keynotinsampleornotsample[self.image_key]:sample[Fields.source_file]=[]returnsampleifFields.source_filenotinsampleornotsample[Fields.source_file]:sample[Fields.source_file]=sample[self.image_key]# load imagesloaded_image_keys=sample[self.image_key]sample,images=load_data_with_context(sample,context,loaded_image_keys,load_image,mm_bytes_key=self.image_bytes_key)processed={}forimage_keyinloaded_image_keys:ifimage_keyinprocessed:continueifself.p<np.random.rand():processed[image_key]=image_keyelse:blured_image_key=transfer_filename(image_key,OP_NAME,self.save_dir,**self._init_parameters)ifblured_image_key!=image_key:# the image_key is a valid local path, we can update itifnotos.path.exists(blured_image_key)orblured_image_keynotinimages:blured_image=images[image_key].convert("RGB").filter(self.blur)images[blured_image_key]=blured_imageblured_image.save(blured_image_key)ifcontext:# update contextsample[Fields.context][blured_image_key]=blured_imageprocessed[image_key]=blured_image_keyelse:blured_image=images[image_key].convert("RGB").filter(self.blur)images[image_key]=blured_imageprocessed[image_key]=image_keyifcontext:# update contextsample[Fields.context][image_key]=blured_image# when the file is modified, its source file needs to be updated.fori,valueinenumerate(loaded_image_keys):ifsample[Fields.source_file][i]!=value:ifprocessed[value]!=value:sample[Fields.source_file][i]=valueifself.image_bytes_keyinsampleandi<len(sample[self.image_bytes_key]):sample[self.image_bytes_key][i]=images[processed[value]].tobytes()sample[self.image_key]=[processed[key]forkeyinloaded_image_keys]returnsample