Produce images read from a list of files as (h, w, c) arrays.
| 45 | |
| 46 | |
| 47 | class ImageFromFile(RNGDataFlow): |
| 48 | """ Produce images read from a list of files as (h, w, c) arrays. """ |
| 49 | def __init__(self, files, channel=3, resize=None, shuffle=False): |
| 50 | """ |
| 51 | Args: |
| 52 | files (list): list of file paths. |
| 53 | channel (int): 1 or 3. Will convert grayscale to RGB images if channel==3. |
| 54 | Will produce (h, w, 1) array if channel==1. |
| 55 | resize (tuple): int or (h, w) tuple. If given, resize the image. |
| 56 | """ |
| 57 | assert len(files), "No image files given to ImageFromFile!" |
| 58 | self.files = files |
| 59 | self.channel = int(channel) |
| 60 | assert self.channel in [1, 3], self.channel |
| 61 | self.imread_mode = cv2.IMREAD_GRAYSCALE if self.channel == 1 else cv2.IMREAD_COLOR |
| 62 | if resize is not None: |
| 63 | resize = shape2d(resize) |
| 64 | self.resize = resize |
| 65 | self.shuffle = shuffle |
| 66 | |
| 67 | def __len__(self): |
| 68 | return len(self.files) |
| 69 | |
| 70 | def __iter__(self): |
| 71 | if self.shuffle: |
| 72 | self.rng.shuffle(self.files) |
| 73 | for f in self.files: |
| 74 | im = cv2.imread(f, self.imread_mode) |
| 75 | assert im is not None, f |
| 76 | if self.channel == 3: |
| 77 | im = im[:, :, ::-1] |
| 78 | if self.resize is not None: |
| 79 | im = cv2.resize(im, tuple(self.resize[::-1])) |
| 80 | if self.channel == 1: |
| 81 | im = im[:, :, np.newaxis] |
| 82 | yield [im] |
| 83 | |
| 84 | |
| 85 | class AugmentImageComponent(MapDataComponent): |
no outgoing calls
no test coverage detected