Mercurial > ift6266
comparison data_generation/transformations/add_background_image.py @ 167:1f5937e9e530
More moves - transformations into data_generation, added "deep" folder
author | Dumitru Erhan <dumitru.erhan@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 14:15:38 -0500 |
parents | transformations/add_background_image.py@8aadb0f59a64 |
children |
comparison
equal
deleted
inserted
replaced
166:17ae5a1a4dd1 | 167:1f5937e9e530 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: iso-8859-1 -*- | |
3 | |
4 ''' | |
5 Implementation of random background adding to a specific image | |
6 | |
7 Author: Guillaume Sicard | |
8 ''' | |
9 | |
10 import sys, os, random | |
11 import cPickle | |
12 import Image, numpy | |
13 | |
14 class AddBackground(): | |
15 def __init__(self, threshold = 128, complexity = 1): | |
16 self.h = 32 | |
17 self.w = 32 | |
18 self.threshold = 1; | |
19 try: #in order to load locally if it is available | |
20 self.bg_image_file = '/Tmp/image_net/' | |
21 f=open(self.bg_image_file+'filelist.pkl') | |
22 except: | |
23 self.bg_image_file = '/data/lisa/data/ift6266h10/image_net/' | |
24 f=open(self.bg_image_file+'filelist.pkl') | |
25 self.image_files = cPickle.load(f) | |
26 f.close() | |
27 self.regenerate_parameters(complexity) | |
28 | |
29 def get_current_parameters(self): | |
30 return [self.contrast] | |
31 # get threshold value | |
32 def get_settings_names(self): | |
33 return ['contrast'] | |
34 | |
35 # no need, except for testmod.py | |
36 def regenerate_parameters(self, complexity): | |
37 self.contrast = 1-numpy.random.rand()*complexity | |
38 return [self.contrast] | |
39 | |
40 # load an image | |
41 def load_image(self,filename): | |
42 image = Image.open(filename).convert('L') | |
43 image = numpy.asarray(image) | |
44 image = (image / 255.0).astype(numpy.float32) | |
45 return image | |
46 | |
47 # save an image | |
48 def save_image(self,array, filename): | |
49 image = (array * 255.0).astype('int') | |
50 image = Image.fromarray(image) | |
51 if (filename != ''): | |
52 image.save(filename) | |
53 else: | |
54 image.show() | |
55 | |
56 # make a random 32x32 crop of an image | |
57 def rand_crop(self,image): | |
58 i_w, i_h = image.shape | |
59 x, y = random.randint(0, i_w - self.w), random.randint(0, i_h - self.h) | |
60 return image[x:x + self.w, y:y + self.h] | |
61 | |
62 # select a random background image from "bg_image_file" and crops it | |
63 def rand_bg_image(self,maximage): | |
64 i = random.randint(0, len(self.image_files) - 1) | |
65 | |
66 image = self.load_image(self.bg_image_file + self.image_files[i]) | |
67 self.bg_image = self.rand_crop(image) | |
68 maxbg = self.bg_image.max() | |
69 self.bg_image = self.bg_image / maxbg * ( max(maximage - self.contrast,0.0) ) | |
70 | |
71 # set "bg_image" as background to "image", based on a pixels threshold | |
72 def set_bg(self,image): | |
73 tensor = numpy.asarray([self.bg_image,image],dtype='float32') | |
74 return tensor.max(0) | |
75 | |
76 # transform an image file and return an array | |
77 def transform_image_from_file(self, filename): | |
78 self.rand_bg_image() | |
79 image = self.load_image(filename) | |
80 image = self.set_bg(image) | |
81 return image | |
82 | |
83 # standard array to array transform | |
84 def transform_image(self, image): | |
85 self.rand_bg_image(image.max()) | |
86 image = self.set_bg(image) | |
87 return image | |
88 | |
89 # test method | |
90 def test(self,filename): | |
91 import time | |
92 | |
93 sys.stdout.write('Starting addBackground test : loading image') | |
94 sys.stdout.flush() | |
95 | |
96 image = self.load_image(filename) | |
97 | |
98 t = 0 | |
99 n = 500 | |
100 for i in range(n): | |
101 t0 = time.time() | |
102 image2 = self.transform_image(image) | |
103 t = ( i * t + (time.time() - t0) ) / (i + 1) | |
104 sys.stdout.write('.') | |
105 sys.stdout.flush() | |
106 | |
107 print "Done!\nAverage time : " + str(1000 * t) + " ms" | |
108 | |
109 if __name__ == '__main__': | |
110 | |
111 myAddBackground = AddBackground() | |
112 myAddBackground.test('./images/0-LiberationSans-Italic.ttf.jpg') |