| 7 | |
| 8 | |
| 9 | class VaceImageProcessor(object): |
| 10 | |
| 11 | def __init__(self, downsample=None, seq_len=None): |
| 12 | self.downsample = downsample |
| 13 | self.seq_len = seq_len |
| 14 | |
| 15 | def _pillow_convert(self, image, cvt_type='RGB'): |
| 16 | if image.mode != cvt_type: |
| 17 | if image.mode == 'P': |
| 18 | image = image.convert(f'{cvt_type}A') |
| 19 | if image.mode == f'{cvt_type}A': |
| 20 | bg = Image.new( |
| 21 | cvt_type, |
| 22 | size=(image.width, image.height), |
| 23 | color=(255, 255, 255)) |
| 24 | bg.paste(image, (0, 0), mask=image) |
| 25 | image = bg |
| 26 | else: |
| 27 | image = image.convert(cvt_type) |
| 28 | return image |
| 29 | |
| 30 | def _load_image(self, img_path): |
| 31 | if img_path is None or img_path == '': |
| 32 | return None |
| 33 | img = Image.open(img_path) |
| 34 | img = self._pillow_convert(img) |
| 35 | return img |
| 36 | |
| 37 | def _resize_crop(self, img, oh, ow, normalize=True): |
| 38 | """ |
| 39 | Resize, center crop, convert to tensor, and normalize. |
| 40 | """ |
| 41 | # resize and crop |
| 42 | iw, ih = img.size |
| 43 | if iw != ow or ih != oh: |
| 44 | # resize |
| 45 | scale = max(ow / iw, oh / ih) |
| 46 | img = img.resize((round(scale * iw), round(scale * ih)), |
| 47 | resample=Image.Resampling.LANCZOS) |
| 48 | assert img.width >= ow and img.height >= oh |
| 49 | |
| 50 | # center crop |
| 51 | x1 = (img.width - ow) // 2 |
| 52 | y1 = (img.height - oh) // 2 |
| 53 | img = img.crop((x1, y1, x1 + ow, y1 + oh)) |
| 54 | |
| 55 | # normalize |
| 56 | if normalize: |
| 57 | img = TF.to_tensor(img).sub_(0.5).div_(0.5).unsqueeze(1) |
| 58 | return img |
| 59 | |
| 60 | def _image_preprocess(self, img, oh, ow, normalize=True, **kwargs): |
| 61 | return self._resize_crop(img, oh, ow, normalize) |
| 62 | |
| 63 | def load_image(self, data_key, **kwargs): |
| 64 | return self.load_image_batch(data_key, **kwargs) |
| 65 | |
| 66 | def load_image_pair(self, data_key, data_key2, **kwargs): |
nothing calls this directly
no outgoing calls
no test coverage detected