comparison transformations/add_background_image.py @ 65:ab70fbca513c

Change path to AddBackground and add a image_file attribute (not to do a listdir at each image)
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Tue, 09 Feb 2010 18:31:24 -0500
parents 4d4248f7e2fb
children ee6a788557b6
comparison
equal deleted inserted replaced
63:7949f46b03e0 65:ab70fbca513c
13 class AddBackground(): 13 class AddBackground():
14 def __init__(self, threshold = 128): 14 def __init__(self, threshold = 128):
15 self.h = 32 15 self.h = 32
16 self.w = 32 16 self.w = 32
17 self.threshold = threshold; 17 self.threshold = threshold;
18 self.bg_image_dir = './images/' 18 self.bg_image_dir = '/data/lisa/data/ift6266h10/image_net/'
19 self.pattern = '*.jpg' 19 self.pattern = '*.JPEG'
20 self.image_files = fnmatch.filter(os.listdir(self.bg_image_dir),self.pattern)
20 21
21 # get threshold value 22 # get threshold value
22 def get_settings_names(self): 23 def get_settings_names(self):
23 return [str(self.threshold)] 24 return [str(self.threshold)]
24 25
49 x, y = random.randint(0, i_w - self.w), random.randint(0, i_h - self.h) 50 x, y = random.randint(0, i_w - self.w), random.randint(0, i_h - self.h)
50 return image[x:x + self.w, y:y + self.h] 51 return image[x:x + self.w, y:y + self.h]
51 52
52 # select a random background image from "bg_image_dir" and crops it 53 # select a random background image from "bg_image_dir" and crops it
53 def rand_bg_image(self): 54 def rand_bg_image(self):
54 files = os.listdir(self.bg_image_dir) 55 i = random.randint(0, len(self.image_files) - 1)
55 image_files = fnmatch.filter(files, self.pattern)
56 i = random.randint(0, len(image_files) - 1)
57 56
58 image = self.load_image(self.bg_image_dir + image_files[i]) 57 image = self.load_image(self.bg_image_dir + self.image_files[i])
59 self.bg_image = self.rand_crop(image) 58 self.bg_image = self.rand_crop(image)
60 59
61 # set "bg_image" as background to "image", based on a pixels threshold 60 # set "bg_image" as background to "image", based on a pixels threshold
62 def set_bg(self,image): 61 def set_bg(self,image):
63 b = (image < self.threshold / 255.0).astype(numpy.float32) 62 b = (image < self.threshold / 255.0).astype(numpy.float32)