| 104 | |
| 105 | |
| 106 | def preprocess(img, bbox_labels, mode, settings): |
| 107 | img_width, img_height = img.size |
| 108 | sampled_labels = bbox_labels |
| 109 | if mode == 'train': |
| 110 | if settings._apply_distort: |
| 111 | img = image_util.distort_image(img, settings) |
| 112 | if settings._apply_expand: |
| 113 | img, bbox_labels, img_width, img_height = image_util.expand_image( |
| 114 | img, bbox_labels, img_width, img_height, settings) |
| 115 | # sampling |
| 116 | batch_sampler = [] |
| 117 | # hard-code here |
| 118 | batch_sampler.append( |
| 119 | image_util.sampler(1, 1, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0)) |
| 120 | batch_sampler.append( |
| 121 | image_util.sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.1, 0.0)) |
| 122 | batch_sampler.append( |
| 123 | image_util.sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.3, 0.0)) |
| 124 | batch_sampler.append( |
| 125 | image_util.sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.5, 0.0)) |
| 126 | batch_sampler.append( |
| 127 | image_util.sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.7, 0.0)) |
| 128 | batch_sampler.append( |
| 129 | image_util.sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.9, 0.0)) |
| 130 | batch_sampler.append( |
| 131 | image_util.sampler(1, 50, 0.3, 1.0, 0.5, 2.0, 0.0, 1.0)) |
| 132 | sampled_bbox = image_util.generate_batch_samples(batch_sampler, |
| 133 | bbox_labels) |
| 134 | |
| 135 | img = np.array(img) |
| 136 | if len(sampled_bbox) > 0: |
| 137 | idx = int(np.random.uniform(0, len(sampled_bbox))) |
| 138 | img, sampled_labels = image_util.crop_image( |
| 139 | img, bbox_labels, sampled_bbox[idx], img_width, img_height) |
| 140 | |
| 141 | img = Image.fromarray(img) |
| 142 | img = img.resize((settings.resize_w, settings.resize_h), Image.ANTIALIAS) |
| 143 | img = np.array(img) |
| 144 | |
| 145 | if mode == 'train': |
| 146 | mirror = int(np.random.uniform(0, 2)) |
| 147 | if mirror == 1: |
| 148 | img = img[:, ::-1, :] |
| 149 | for i in six.moves.xrange(len(sampled_labels)): |
| 150 | tmp = sampled_labels[i][1] |
| 151 | sampled_labels[i][1] = 1 - sampled_labels[i][3] |
| 152 | sampled_labels[i][3] = 1 - tmp |
| 153 | # HWC to CHW |
| 154 | if len(img.shape) == 3: |
| 155 | img = np.swapaxes(img, 1, 2) |
| 156 | img = np.swapaxes(img, 1, 0) |
| 157 | # RBG to BGR |
| 158 | img = img[[2, 1, 0], :, :] |
| 159 | img = img.astype('float32') |
| 160 | img -= settings.img_mean |
| 161 | img = img * 0.007843 |
| 162 | return img, sampled_labels |
| 163 | |