Exemple #1
0
def smart_load(location, use_cache=False):
    """
    Load a file, with the method based on the extension.  See smart_save doc for the list of extensions.
    :param location: Identifies file location.
        If it's formatted as a url, it's downloaded.
        If it begins with a "/", it's assumed to be a local path.
        Otherwise, it is assumed to be referenced relative to the data directory.
    :param use_cache: If True, and the location is a url, make a local cache of the file for future use (note: if the
        file at this url changes, the cached file will not).
    :return: An object, whose type depends on the extension.  Generally a numpy array for data or an object for pickles.
    """
    assert isinstance(
        location, str), 'Location must be a string!  We got: %s' % (location, )
    with smart_file(location, use_cache=use_cache) as local_path:
        ext = os.path.splitext(local_path)[1].lower()
        if ext == '.pkl':
            with open(local_path) as f:
                obj = pickle.load(f)
        elif ext == '.gif':
            frames = readGif(local_path)
            if frames[0].shape[2] == 3 and all(
                    f.shape[2] for f in frames[1:]):  # Wierd case:
                obj = np.array([frames[0]] + [f[:, :, :3] for f in frames[1:]])
            else:
                obj = np.array(readGif(local_path))
        elif ext in _IMAGE_EXTENSIONS:
            from PIL import Image
            obj = _load_image(local_path)
        elif ext in ('.mpg', '.mp4', '.mpeg'):
            obj = _load_video(local_path)
        else:
            raise Exception(
                "No method exists yet to load '{}' files.  Add it!  (While trying to load file {})"
                .format(ext, location))
    return obj
Exemple #2
0
def smart_load(location, use_cache = False):
    """
    Load a file, with the method based on the extension.  See smart_save doc for the list of extensions.
    :param location: Identifies file location.
        If it's formatted as a url, it's downloaded.
        If it begins with a "/", it's assumed to be a local path.
        Otherwise, it is assumed to be referenced relative to the data directory.
    :param use_cache: If True, and the location is a url, make a local cache of the file for future use (note: if the
        file at this url changes, the cached file will not).
    :return: An object, whose type depends on the extension.  Generally a numpy array for data or an object for pickles.
    """
    assert isinstance(location, str), 'Location must be a string!  We got: %s' % (location, )
    with smart_file(location, use_cache=use_cache) as local_path:
        ext = os.path.splitext(local_path)[1].lower()
        if ext=='.pkl':
            with open(local_path) as f:
                obj = pickle.load(f)
        elif ext=='.gif':
            frames = readGif(local_path)
            if frames[0].shape[2]==3 and all(f.shape[2] for f in frames[1:]):  # Wierd case:
                obj = np.array([frames[0]]+[f[:, :, :3] for f in frames[1:]])
            else:
                obj = np.array(readGif(local_path))
        elif ext in _IMAGE_EXTENSIONS:
            from PIL import Image
            obj = _load_image(local_path)
        elif ext in ('.mpg', '.mp4', '.mpeg'):
            obj = _load_video(local_path)
        else:
            raise Exception("No method exists yet to load '{}' files.  Add it!  (While trying to load file {})".format(ext, location))
    return obj