| 132 | return imgori |
| 133 | |
| 134 | def resize_norm_img(self, data, gen_ratio, padding=True): |
| 135 | img = data['image'] |
| 136 | h = img.shape[0] |
| 137 | w = img.shape[1] |
| 138 | if self.padding_rand and random.random() < 0.5: |
| 139 | padding = not padding |
| 140 | imgW, imgH = self.base_shape[gen_ratio - 1] if gen_ratio <= 4 else [ |
| 141 | self.base_h * gen_ratio, self.base_h |
| 142 | ] |
| 143 | use_ratio = imgW // imgH |
| 144 | if use_ratio >= (w // h) + 2: |
| 145 | self.error += 1 |
| 146 | return None |
| 147 | if not padding: |
| 148 | resized_image = cv2.resize(img, (imgW, imgH), |
| 149 | interpolation=cv2.INTER_LINEAR) |
| 150 | resized_w = imgW |
| 151 | else: |
| 152 | ratio = w / float(h) |
| 153 | if math.ceil(imgH * ratio) > imgW: |
| 154 | resized_w = imgW |
| 155 | else: |
| 156 | resized_w = int( |
| 157 | math.ceil(imgH * ratio * (random.random() + 0.5))) |
| 158 | resized_w = min(imgW, resized_w) |
| 159 | |
| 160 | resized_image = cv2.resize(img, (resized_w, imgH)) |
| 161 | resized_image = resized_image.astype('float32') |
| 162 | resized_image = resized_image.transpose((2, 0, 1)) / 255 |
| 163 | resized_image -= 0.5 |
| 164 | resized_image /= 0.5 |
| 165 | padding_im = np.zeros((3, imgH, imgW), dtype=np.float32) |
| 166 | if self.padding_doub and random.random() < 0.5: |
| 167 | padding_im[:, :, -resized_w:] = resized_image |
| 168 | else: |
| 169 | padding_im[:, :, :resized_w] = resized_image |
| 170 | valid_ratio = min(1.0, float(resized_w / imgW)) |
| 171 | data['image'] = padding_im |
| 172 | data['valid_ratio'] = valid_ratio |
| 173 | data['real_ratio'] = round(w / h) |
| 174 | return data |
| 175 | |
| 176 | def get_lmdb_sample_info(self, txn, index): |
| 177 | label_key = 'label-%09d'.encode() % index |