OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """ | 3 """ |
4 Copyright 2013 Google Inc. | 4 Copyright 2013 Google Inc. |
5 | 5 |
6 Use of this source code is governed by a BSD-style license that can be | 6 Use of this source code is governed by a BSD-style license that can be |
7 found in the LICENSE file. | 7 found in the LICENSE file. |
8 | 8 |
9 Calulate differences between image pairs, and store them in a database. | 9 Calulate differences between image pairs, and store them in a database. |
10 """ | 10 """ |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 RGBDIFFS_SUBDIR = 'diffs' | 44 RGBDIFFS_SUBDIR = 'diffs' |
45 WHITEDIFFS_SUBDIR = 'whitediffs' | 45 WHITEDIFFS_SUBDIR = 'whitediffs' |
46 | 46 |
47 # Keys used within DiffRecord dictionary representations. | 47 # Keys used within DiffRecord dictionary representations. |
48 # NOTE: Keep these in sync with static/constants.js | 48 # NOTE: Keep these in sync with static/constants.js |
49 KEY__DIFFERENCES__MAX_DIFF_PER_CHANNEL = 'maxDiffPerChannel' | 49 KEY__DIFFERENCES__MAX_DIFF_PER_CHANNEL = 'maxDiffPerChannel' |
50 KEY__DIFFERENCES__NUM_DIFF_PIXELS = 'numDifferingPixels' | 50 KEY__DIFFERENCES__NUM_DIFF_PIXELS = 'numDifferingPixels' |
51 KEY__DIFFERENCES__PERCENT_DIFF_PIXELS = 'percentDifferingPixels' | 51 KEY__DIFFERENCES__PERCENT_DIFF_PIXELS = 'percentDifferingPixels' |
52 KEY__DIFFERENCES__PERCEPTUAL_DIFF = 'perceptualDifference' | 52 KEY__DIFFERENCES__PERCEPTUAL_DIFF = 'perceptualDifference' |
| 53 KEY__DIFFERENCES__DIFF_URL = 'diffUrl' |
| 54 KEY__DIFFERENCES__WHITE_DIFF_URL = 'whiteDiffUrl' |
53 | 55 |
54 # Special values within ImageDiffDB._diff_dict | 56 # Special values within ImageDiffDB._diff_dict |
55 _DIFFRECORD_FAILED = 'failed' | 57 _DIFFRECORD_FAILED = 'failed' |
56 _DIFFRECORD_PENDING = 'pending' | 58 _DIFFRECORD_PENDING = 'pending' |
57 | 59 |
58 # How often to report tasks_queue size | 60 # How often to report tasks_queue size |
59 QUEUE_LOGGING_GRANULARITY = 1000 | 61 QUEUE_LOGGING_GRANULARITY = 1000 |
60 | 62 |
61 # Temporary variable to keep track of how many times we download | 63 # Temporary variable to keep track of how many times we download |
62 # the same file in multiple threads. | 64 # the same file in multiple threads. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 153 |
152 # Get information out of the skpdiff_summary_file. | 154 # Get information out of the skpdiff_summary_file. |
153 with contextlib.closing(open(skpdiff_summary_file)) as fp: | 155 with contextlib.closing(open(skpdiff_summary_file)) as fp: |
154 data = json.load(fp) | 156 data = json.load(fp) |
155 | 157 |
156 # For now, we can assume there is only one record in the output summary, | 158 # For now, we can assume there is only one record in the output summary, |
157 # since we passed skpdiff only one pair of images. | 159 # since we passed skpdiff only one pair of images. |
158 record = data['records'][0] | 160 record = data['records'][0] |
159 self._width = record['width'] | 161 self._width = record['width'] |
160 self._height = record['height'] | 162 self._height = record['height'] |
| 163 self._diffUrl = os.path.split(record['rgbDiffPath'])[1] |
| 164 self._whiteDiffUrl = os.path.split(record['whiteDiffPath'])[1] |
| 165 |
161 # TODO: make max_diff_per_channel a tuple instead of a list, because the | 166 # TODO: make max_diff_per_channel a tuple instead of a list, because the |
162 # structure is meaningful (first element is red, second is green, etc.) | 167 # structure is meaningful (first element is red, second is green, etc.) |
163 # See http://stackoverflow.com/a/626871 | 168 # See http://stackoverflow.com/a/626871 |
164 self._max_diff_per_channel = [ | 169 self._max_diff_per_channel = [ |
165 record['maxRedDiff'], record['maxGreenDiff'], record['maxBlueDiff']] | 170 record['maxRedDiff'], record['maxGreenDiff'], record['maxBlueDiff']] |
166 per_differ_stats = record['diffs'] | 171 per_differ_stats = record['diffs'] |
167 for stats in per_differ_stats: | 172 for stats in per_differ_stats: |
168 differ_name = stats['differName'] | 173 differ_name = stats['differName'] |
169 if differ_name == 'different_pixels': | 174 if differ_name == 'different_pixels': |
170 self._num_pixels_differing = stats['pointsOfInterest'] | 175 self._num_pixels_differing = stats['pointsOfInterest'] |
171 elif differ_name == 'perceptual': | 176 elif differ_name == 'perceptual': |
172 perceptual_similarity = stats['result'] | 177 perceptual_similarity = stats['result'] |
173 | 178 |
174 # skpdiff returns the perceptual similarity; convert it to get the | 179 # skpdiff returns the perceptual similarity; convert it to get the |
175 # perceptual difference percentage. | 180 # perceptual difference percentage. |
176 # skpdiff outputs -1 if the images are different sizes. Treat any | 181 # skpdiff outputs -1 if the images are different sizes. Treat any |
177 # output that does not lie in [0, 1] as having 0% perceptual | 182 # output that does not lie in [0, 1] as having 0% perceptual |
178 # similarity. | 183 # similarity. |
179 if not 0 <= perceptual_similarity <= 1: | 184 if not 0 <= perceptual_similarity <= 1: |
180 perceptual_similarity = 0 | 185 perceptual_similarity = 0 |
181 self._perceptual_difference = 100 - (perceptual_similarity * 100) | 186 self._perceptual_difference = 100 - (perceptual_similarity * 100) |
| 187 except: |
| 188 print "expected:", expected_image_file |
| 189 print "actual :", actual_image_file |
| 190 raise |
182 finally: | 191 finally: |
183 shutil.rmtree(skpdiff_output_dir) | 192 shutil.rmtree(skpdiff_output_dir) |
184 | 193 |
185 # TODO(epoger): Use properties instead of getters throughout. | 194 # TODO(epoger): Use properties instead of getters throughout. |
186 # See http://stackoverflow.com/a/6618176 | 195 # See http://stackoverflow.com/a/6618176 |
187 def get_num_pixels_differing(self): | 196 def get_num_pixels_differing(self): |
188 """Returns the absolute number of pixels that differ.""" | 197 """Returns the absolute number of pixels that differ.""" |
189 return self._num_pixels_differing | 198 return self._num_pixels_differing |
190 | 199 |
191 def get_percent_pixels_differing(self): | 200 def get_percent_pixels_differing(self): |
(...skipping 13 matching lines...) Expand all Loading... |
205 | 214 |
206 def as_dict(self): | 215 def as_dict(self): |
207 """Returns a dictionary representation of this DiffRecord, as needed when | 216 """Returns a dictionary representation of this DiffRecord, as needed when |
208 constructing the JSON representation.""" | 217 constructing the JSON representation.""" |
209 return { | 218 return { |
210 KEY__DIFFERENCES__NUM_DIFF_PIXELS: self._num_pixels_differing, | 219 KEY__DIFFERENCES__NUM_DIFF_PIXELS: self._num_pixels_differing, |
211 KEY__DIFFERENCES__PERCENT_DIFF_PIXELS: | 220 KEY__DIFFERENCES__PERCENT_DIFF_PIXELS: |
212 self.get_percent_pixels_differing(), | 221 self.get_percent_pixels_differing(), |
213 KEY__DIFFERENCES__MAX_DIFF_PER_CHANNEL: self._max_diff_per_channel, | 222 KEY__DIFFERENCES__MAX_DIFF_PER_CHANNEL: self._max_diff_per_channel, |
214 KEY__DIFFERENCES__PERCEPTUAL_DIFF: self._perceptual_difference, | 223 KEY__DIFFERENCES__PERCEPTUAL_DIFF: self._perceptual_difference, |
| 224 KEY__DIFFERENCES__DIFF_URL: self._diffUrl, |
| 225 KEY__DIFFERENCES__WHITE_DIFF_URL: self._whiteDiffUrl, |
215 } | 226 } |
216 | 227 |
217 | 228 |
| 229 |
218 class ImageDiffDB(object): | 230 class ImageDiffDB(object): |
219 """ Calculates differences between image pairs, maintaining a database of | 231 """ Calculates differences between image pairs, maintaining a database of |
220 them for download.""" | 232 them for download.""" |
221 | 233 |
222 def __init__(self, storage_root, gs=None, | 234 def __init__(self, storage_root, gs=None, |
223 num_worker_threads=DEFAULT_NUM_WORKER_THREADS): | 235 num_worker_threads=DEFAULT_NUM_WORKER_THREADS): |
224 """ | 236 """ |
225 Args: | 237 Args: |
226 storage_root: string; root path within the DB will store all of its stuff | 238 storage_root: string; root path within the DB will store all of its stuff |
227 gs: instance of GSUtils object we can use to download images | 239 gs: instance of GSUtils object we can use to download images |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 | 472 |
461 Args: | 473 Args: |
462 locator: string, or something that can be represented as a string. | 474 locator: string, or something that can be represented as a string. |
463 If None or '', it is returned without modification, because empty | 475 If None or '', it is returned without modification, because empty |
464 locators have a particular meaning ("there is no image for this") | 476 locators have a particular meaning ("there is no image for this") |
465 """ | 477 """ |
466 if locator: | 478 if locator: |
467 return DISALLOWED_FILEPATH_CHAR_REGEX.sub('_', str(locator)) | 479 return DISALLOWED_FILEPATH_CHAR_REGEX.sub('_', str(locator)) |
468 else: | 480 else: |
469 return locator | 481 return locator |
OLD | NEW |