def build(self): import imageio pixels = imageio.imread(self.filepath) pixels = channels_to_front(pixels) transparent_types = {'.png'} filepath = Path(self.filepath) if pixels.shape[0] == 4 and filepath.suffix in transparent_types: # If normalise is False, then we return the alpha as an extra # channel, which can be useful if the alpha channel has semantic # meanings! if self.normalise: p = normalize_pixels_range(pixels[:3]) return MaskedImage(p, mask=pixels[-1].astype(np.bool), copy=False) else: return Image(pixels, copy=False) # Assumed not to have an Alpha channel if self.normalise: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False)
def _pil_to_numpy(pil_image, normalise, convert=None): p = pil_image.convert(convert) if convert else pil_image p = channels_to_front(p) if normalise: return normalize_pixels_range(p) else: return p
def vector_128_dsift(x, dtype=np.float32): r""" Computes a SIFT feature vector from a square patch (or image). Patch **must** be square and the output vector will *always* be a ``(128,)`` vector. Please see :func:`dsift` for more information. Parameters ---------- x : :map:`Image` or subclass or ``(C, Y, Y)`` `ndarray` Either the image object itself or an array with the pixels. The first dimension is interpreted as channels. Must be square i.e. ``height == width``. dtype : ``np.dtype``, optional The dtype of the returned vector. Raises ------ ValueError Only square images are supported. """ if not isinstance(x, np.ndarray): x = x.pixels if x.shape[-1] != x.shape[-2]: raise ValueError('This feature only works with square images ' 'i.e. width == height') patch_shape = x.shape[-1] n_bins = 4 c_size = patch_shape // n_bins x = normalize_pixels_range(x, error_on_unknown_type=False) return dsift(x, window_step_horizontal=patch_shape, window_step_vertical=patch_shape, num_bins_horizontal=n_bins, num_bins_vertical=n_bins, cell_size_horizontal=c_size, cell_size_vertical=c_size, num_or_bins=8, fast=True).astype(dtype)
def _pil_to_numpy(pil_image, normalize, convert=None): p = pil_image.convert(convert) if convert else pil_image p = channels_to_front(p) if normalize: return normalize_pixels_range(p) else: return p
def _pil_to_numpy(pil_image, normalize, convert=None): p = pil_image.convert(convert) if convert else pil_image p = np.asarray(p) if normalize: return normalize_pixels_range(p) else: return p
def import_mesh(path): if path.suffix == '.pkl' or path.suffix == '.gz': mesh = import_pickle(path) else: mesh = m3io.import_mesh(path) if hasattr(mesh, "texture") and mesh.texture.pixels.dtype != np.float64: mesh.texture.pixels = normalize_pixels_range(mesh.texture.pixels) return mesh
def _imageio_to_menpo(imio_reader, normalise, index): pixels = imio_reader.get_data(index) pixels = channels_to_front(pixels) if normalise: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False)
def imageio_importer(filepath, asset=None, normalise=True, **kwargs): r""" Imports images using the imageio library - which is actually fairly similar to our importing logic - but contains the necessary plugins to import lots of interesting image types like RAW images. Parameters ---------- filepath : `Path` Absolute filepath of the image. asset : `object`, optional An optional asset that may help with loading. This is unused for this implementation. normalise : `bool`, optional If ``True``, normalise between 0.0 and 1.0 and convert to float. If ``False`` just return whatever imageio imports. \**kwargs : `dict`, optional Any other keyword arguments. Returns ------- image : :map:`Image` or subclass The imported image. """ import imageio pixels = imageio.imread(str(filepath)) pixels = channels_to_front(pixels) transparent_types = {'.png'} if pixels.shape[0] == 4 and filepath.suffix in transparent_types: # If normalise is False, then we return the alpha as an extra # channel, which can be useful if the alpha channel has semantic # meanings! if normalise: p = normalize_pixels_range(pixels[:3]) return MaskedImage(p, mask=pixels[-1].astype(np.bool), copy=False) else: return Image(pixels, copy=False) # Assumed not to have an Alpha channel if normalise: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False)
def imageio_importer(filepath, asset=None, normalize=True, **kwargs): r""" Imports images using the imageio library - which is actually fairly similar to our importing logic - but contains the necessary plugins to import lots of interesting image types like RAW images. Parameters ---------- filepath : `Path` Absolute filepath of the image. asset : `object`, optional An optional asset that may help with loading. This is unused for this implementation. normalize : `bool`, optional If ``True``, normalize between 0.0 and 1.0 and convert to float. If ``False`` just return whatever imageio imports. \**kwargs : `dict`, optional Any other keyword arguments. Returns ------- image : :map:`Image` or subclass The imported image. """ import imageio pixels = imageio.imread(str(filepath)) pixels = channels_to_front(pixels) transparent_types = {'.png'} if pixels.shape[0] == 4 and filepath.suffix in transparent_types: # If normalize is False, then we return the alpha as an extra # channel, which can be useful if the alpha channel has semantic # meanings! if normalize: p = normalize_pixels_range(pixels[:3]) return MaskedImage(p, mask=pixels[-1].astype(np.bool), copy=False) else: return Image(pixels, copy=False) # Assumed not to have an Alpha channel if normalize: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False)
def imageio_to_menpo(imio_reader, index): pixels = imio_reader.get_data(index) pixels = channels_to_front(pixels) if pixels.shape[0] == 4: # If normalize is False, then we return the alpha as an extra # channel, which can be useful if the alpha channel has semantic # meanings! if normalize: p = normalize_pixels_range(pixels[:3]) return MaskedImage(p, mask=pixels[-1].astype(bool), copy=False) else: return Image(pixels, copy=False) # Assumed not to have an Alpha channel if normalize: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False)
def imageio_to_menpo(imio_reader, index): pixels = imio_reader.get_data(index) pixels = channels_to_front(pixels) if pixels.shape[0] == 4: # If normalise is False, then we return the alpha as an extra # channel, which can be useful if the alpha channel has semantic # meanings! if normalise: p = normalize_pixels_range(pixels[:3]) return MaskedImage(p, mask=pixels[-1].astype(np.bool), copy=False) else: return Image(pixels, copy=False) # Assumed not to have an Alpha channel if normalise: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False)
def _read_one_frame(self): r""" Reads one frame from the opened ``self._pipe`` and converts it to a numpy array Returns ------- image : :map:`Image` Image of shape ``(self.height, self.width, 3)`` """ raw_data = self._pipe.stdout.read(self.height * self.width * 3) frame = np.frombuffer(raw_data, dtype=np.uint8) frame = frame.reshape((self.height, self.width, 3)) self._pipe.stdout.flush() self.index += 1 if self.normalize: frame = normalize_pixels_range(frame) return frame
def _read_one_frame(self): r""" Reads one frame from the opened ``self._pipe`` and converts it to a numpy array Returns ------- image : :map:`Image` Image of shape ``(self.height, self.width, 3)`` """ raw_data = self._pipe.stdout.read(self.height*self.width*3) frame = np.fromstring(raw_data, dtype=np.uint8) frame = frame.reshape((self.height, self.width, 3)) self._pipe.stdout.flush() self.index += 1 if self.normalize: frame = normalize_pixels_range(frame) return frame
def import_mesh(path, hasTexture=False, landmark_type='ibug68'): if path.suffix == '.pkl' or path.suffix == '.gz': mesh = import_pickle(path) else: mesh = m3io.import_mesh(path) if hasTexture: if mesh.texture.pixels.dtype != np.float64: mesh.texture.pixels = normalize_pixels_range(mesh.texture.pixels) else: landmark = [] count = 0 with open(str(path) + '.landmark') as pp_file: pp_file = csv.reader(pp_file, delimiter=' ') for row in pp_file: count = count + 1 if landmark_type == 'ibug100': if count >= 1 and count <= 100: landmark.append( [float(row[0]), float(row[1]), float(row[2])]) if landmark_type == 'ibug68': if count < 69: landmark.append( [float(row[0]), float(row[1]), float(row[2])]) if landmark_type == 'ibugEar': if count >= 78 and count <= 88: landmark.append( [float(row[0]), float(row[1]), float(row[2])]) if count >= 90 and count <= 100: landmark.append( [float(row[0]), float(row[1]), float(row[2])]) mesh.landmarks[landmark_type] = PointCloud(np.array(landmark)) return mesh