|
25 | 25 | from datetime import datetime |
26 | 26 |
|
27 | 27 | import numpy as np |
28 | | -from PIL import Image |
| 28 | +from PIL import Image, ImageOps |
29 | 29 |
|
30 | 30 | try: |
31 | 31 | from pycoral.utils.dataset import read_label_file |
@@ -1145,58 +1145,37 @@ def _resize_and_chop_tiles(self, |
1145 | 1145 |
|
1146 | 1146 | logging.debug("Resizing to {} x {} for tiling".format(resamp_x, resamp_y)) |
1147 | 1147 |
|
1148 | | - # From: https://uploadcare.com/blog/fast-import-of-pillow-images-to-numpy-opencv-arrays/ |
1149 | | - def to_numpy(im, shape, typestr): |
1150 | | - # unpack data |
1151 | | - e = Image._getencoder(im.mode, 'raw', im.mode) |
1152 | | - e.setimage(im.im) |
1153 | | - |
1154 | | - # NumPy buffer for the result |
1155 | | - data = np.empty(shape, dtype=typestr) |
1156 | | - mem = data.data.cast('B', (data.data.nbytes,)) |
1157 | | - |
1158 | | - bufsize, s, offset = 65536, 0, 0 |
1159 | | - while not s: |
1160 | | - l, s, d = e.encode(bufsize) |
1161 | | - mem[offset:offset + len(d)] = d |
1162 | | - offset += len(d) |
1163 | | - if s < 0: |
1164 | | - raise RuntimeError("encoder error %d in tobytes" % s) |
1165 | | - return data |
1166 | | - |
1167 | 1148 | # Chop & resize image piece |
1168 | 1149 | if image.mode != 'RGB': |
1169 | 1150 | image = image.convert('RGB') |
1170 | 1151 | image.thumbnail((resamp_x, resamp_y), Image.LANCZOS) |
1171 | | - |
| 1152 | + |
| 1153 | + # Rescale the input from uint8 |
| 1154 | + input_zero = self.input_details['quantization'][1] |
| 1155 | + input_scale = 1.0 / (255.0 * self.input_details['quantization'][0]) |
| 1156 | + |
1172 | 1157 | # It'd be useful to print this once at the beginning of the run |
1173 | 1158 | key = "{} {}".format(*image.size) |
1174 | 1159 | if key not in self.printed_shape_map: |
1175 | 1160 | logging.info( |
1176 | 1161 | "Mapping {} image to {}x{} tiles".format(image.size, tiles_x, tiles_y)) |
1177 | 1162 | self.printed_shape_map[key] = True |
1178 | 1163 |
|
1179 | | - input_zero = self.input_details['quantization'][1] |
1180 | | - input_scale = 1.0 / (256.0 * self.input_details['quantization'][0]) |
1181 | | - |
1182 | 1164 | # Do chunking |
1183 | 1165 | tiles = [] |
1184 | 1166 | for x_off in range(0, resamp_x - options.tile_overlap, m_width - options.tile_overlap): |
1185 | 1167 | for y_off in range(0, resamp_y - options.tile_overlap, m_height - options.tile_overlap): |
1186 | | - cropped_arr = to_numpy(image.crop((x_off, |
1187 | | - y_off, |
1188 | | - x_off + m_width, |
1189 | | - y_off + m_height)), self.input_details['shape'][1:], np.uint8) |
| 1168 | + image_chunk = ImageOps.autocontrast(image.crop((x_off, |
| 1169 | + y_off, |
| 1170 | + x_off + m_width, |
| 1171 | + y_off + m_height)), 1) |
| 1172 | + # Normalize to whatever the input is |
| 1173 | + cropped_arr = np.asarray(image_chunk, np.float32) * input_scale + input_zero |
1190 | 1174 |
|
1191 | 1175 | logging.debug("Resampled image tile {} at offset {}, {}".format(cropped_arr.shape, x_off, y_off)) |
1192 | 1176 | resamp_info = (x_off, y_off, i_width/image.width, i_height/image.height) |
1193 | 1177 |
|
1194 | | - # Normalize from 8-bit image to whatever the input is |
1195 | | - if self.input_details['dtype'] == np.int8: |
1196 | | - cropped_arr = cropped_arr * input_scale + input_zero |
1197 | | - tiles.append((cropped_arr.astype(np.int8), resamp_info)) |
1198 | | - else: |
1199 | | - tiles.append((cropped_arr, resamp_info)) |
| 1178 | + tiles.append((cropped_arr.astype(self.input_details['dtype']), resamp_info)) |
1200 | 1179 |
|
1201 | 1180 | return tiles |
1202 | 1181 |
|
|
0 commit comments