示例#1
0
    def __init__(self, src_root, dst_root, grayscale, size):
        '''
        Args:
            src_root: Source root directory
            dst_root: Target root directory.   This will always be created.
            resize: tuple of the new x and y size.
        '''
        self.tree_util = TreeUtil(src_root, dst_root)

        converter = ImageCounter(src_root, src_root)
        converter.process()
        img_cnt = converter.get_count()

        self._size = size
        self._grayscale = grayscale

        self._color_layers = 1
        if not self._grayscale:
            self._color_layers = 3

        self._img = np.zeros([img_cnt, size[0] * size[1] * self._color_layers],
                             dtype=np.uint8)
        self._label = np.zeros([img_cnt, 3], dtype=np.float32)
        self._idx = 0
        self._filename_manager = FilenameManager()
示例#2
0
class ColorSizeConverter(object):
    def __init__(self, src_root, dst_root, grayscale=False, resize=None):
        '''
        Args:
            src_root: Source root directory
            dst_root: Target root directory.   This will always be created.
            resize: tuple of the new x and y size.
        '''
        self.tree_util = TreeUtil(src_root, dst_root)
        self._resize = resize
        self._grayscale = grayscale

    def process(self):
        '''
        Convert Color files to Gray Scale.
        '''
        def converter(src_path, dst_path, filename):
            if self._grayscale:
                # Convert from RGBA to grayscale
                img = Image.open(os.path.join(src_path, filename)).convert('L')
            else:
                # Convert from RGBA to RGB
                img = Image.open(os.path.join(src_path,
                                              filename)).convert('RGB')

            if self._resize:
                img.thumbnail(self._resize, Image.ANTIALIAS)

            img.save(os.path.join(dst_path, filename))
            print('Converting: {}, {}'.format(os.path.join(src_path, filename),
                                              os.path.join(dst_path,
                                                           filename)))

        self.tree_util.apply_files(converter, '*.png')
示例#3
0
class ImageToNumpy(object):
    def __init__(self, src_root, dst_root, grayscale, size):
        '''
        Args:
            src_root: Source root directory
            dst_root: Target root directory.   This will always be created.
            resize: tuple of the new x and y size.
        '''
        self.tree_util = TreeUtil(src_root, dst_root)

        converter = ImageCounter(src_root, src_root)
        converter.process()
        img_cnt = converter.get_count()

        self._size = size
        self._grayscale = grayscale

        self._color_layers = 1
        if not self._grayscale:
            self._color_layers = 3

        self._img = np.zeros([img_cnt, size[0] * size[1] * self._color_layers],
                             dtype=np.uint8)
        self._label = np.zeros([img_cnt, 3], dtype=np.float32)
        self._idx = 0
        self._filename_manager = FilenameManager()

    def process(self):
        '''
        Get image, convert to numpy array, store in list.
        '''
        def loader(src_path, dest_path, filename):
            img = Image.open(os.path.join(src_path, filename))
            img_np = np.array(img)

            img_np = img_np.reshape(
                self._size[0] * self._size[1] * self._color_layers, )
            self._img[self._idx] = img_np.tolist()

            theta, radius, alpha = self._filename_manager.filename_to_labels(
                filename)
            label = [theta, radius, alpha]

            for i in range(len(label)):
                self._label[self._idx][i] = label[i]

            self._idx += 1
            print('Storing: {}, {}'.format(os.path.join(src_path, filename),
                                           os.path.join(dest_path, filename)))

        self.tree_util.apply_files(loader, '*.png')

    def get_data(self):
        if self._grayscale:
            return self._label, self._img.reshape(len(self._img),
                                                  self._size[0], self._size[1])
        else:
            return self._label, self._img.reshape(len(self._img),
                                                  self._size[0], self._size[1],
                                                  self._color_layers)
示例#4
0
 def __init__(self, src_root, dst_root):
     '''
     Args:
         src_root: Source root directory
         dst_root: Target root directory.   This will always be created.
     '''
     self.tree_util = TreeUtil(src_root, dst_root)
     self._count = 0
示例#5
0
 def __init__(self, src_root, dst_root, grayscale=False, resize=None):
     '''
     Args:
         src_root: Source root directory
         dst_root: Target root directory.   This will always be created.
         resize: tuple of the new x and y size.
     '''
     self.tree_util = TreeUtil(src_root, dst_root)
     self._resize = resize
     self._grayscale = grayscale
示例#6
0
 def __init__(self, src_path, pattern, theta_step, span_step, span_count,
              fmt):
     self._filenames = []
     self._src_path = src_path
     self._dst_path = '.'
     self._pattern = pattern
     self._theta_step = theta_step
     self._span_step = span_step
     self._span_count = span_count
     self._fmt = fmt
     self._tree_util = TreeUtil(self._src_path, self._dst_path)
示例#7
0
class ImageCounter(object):
    def __init__(self, src_root, dst_root):
        '''
        Args:
            src_root: Source root directory
            dst_root: Target root directory.   This will always be created.
        '''
        self.tree_util = TreeUtil(src_root, dst_root)
        self._count = 0

    def get_count(self):
        return self._count

    def process(self):
        '''
        Count the images.
        '''
        def converter(src_path, dst_path, filename):
            self._count += 1
            print('Counting: {}, {}'.format(os.path.join(src_path, filename),
                                            os.path.join(dst_path, filename)))

        self.tree_util.apply_files(converter, '*.png')
示例#8
0
class ThetaSorter(object):
    def __init__(self, src_path, pattern, theta_step, span_step, span_count,
                 fmt):
        self._filenames = []
        self._src_path = src_path
        self._dst_path = '.'
        self._pattern = pattern
        self._theta_step = theta_step
        self._span_step = span_step
        self._span_count = span_count
        self._fmt = fmt
        self._tree_util = TreeUtil(self._src_path, self._dst_path)

    def generate_theta_list(self, theta):
        '''
        Generate a list of the angles included in a range.

        Input:
            theta: angle to build the list around.
            step: size of the step.
            count: number of steps.
            self._fmt: for the items in the list.

        Return:
            list of angles.
        '''
        values = sorted([
            theta + self._span_step * x
            for x in range(-self._span_count, self._span_count + 1)
        ])
        normalized_values = [0.0] * len(values)
        for i, value in enumerate(values):
            if value < 0:
                normalized_values[i] = 360 + value
            elif value >= 360:
                normalized_values[i] = value - 360
            else:
                normalized_values[i] = value
        string_values = [
            self._fmt.format(value) for value in normalized_values
        ]
        return string_values

    def filename_theta_parser(filename):
        '''
        parse the filename for theta.
        '''
        return filename.split('_')[0]

    def collect_filenames(self):
        '''
        Generator of filenames that fit into each of the labeled groups.

        Return:
            tuple of (theta, files) where files is a tuple of (src_path, filename)
        '''
        for theta in range(0, 360, self._theta_step):
            print('Processing Theta: ', theta)
            values = self.generate_theta_list(theta)
            collector = LabelCollector(values,
                                       ThetaSorter.filename_theta_parser)
            self._tree_util.apply_files(collector.get_collector_funct(),
                                        self._pattern,
                                        copy_dir=False)
            yield (theta, collector.get_filenames())