Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# coding=utf-8
5"""Data loading code"""
8import functools
9import os
11import PIL.Image
13from .sample import DelayedSample
16def load_pil_rgb(path):
17 """Loads a sample data
19 Parameters
20 ----------
22 path : str
23 The full path leading to the image to be loaded
26 Returns
27 -------
29 image : PIL.Image.Image
30 A PIL image in RGB mode
32 """
34 return PIL.Image.open(path).convert("RGB")
37def load_pil_1(path):
38 """Loads a sample binary label or mask
40 Parameters
41 ----------
43 path : str
44 The full path leading to the image to be loaded
47 Returns
48 -------
50 image : PIL.Image.Image
51 A PIL image in mode "1"
53 """
55 return PIL.Image.open(path).convert(mode="1", dither=None)
58def make_delayed(sample, loader, key=None):
59 """Returns a delayed-loading Sample object
61 Parameters
62 ----------
64 sample : dict
65 A dictionary that maps field names to sample data values (e.g. paths)
67 loader : object
68 A function that inputs ``sample`` dictionaries and returns the loaded
69 data.
71 key : str
72 A unique key identifier for this sample. If not provided, assumes
73 ``sample`` is a dictionary with a ``data`` entry and uses its path as
74 key.
77 Returns
78 -------
80 sample : bob.ip.binseg.data.sample.DelayedSample
81 In which ``key`` is as provided and ``data`` can be accessed to trigger
82 sample loading.
84 """
86 return DelayedSample(
87 functools.partial(loader, sample),
88 key=key or os.path.splitext(sample["data"])[0],
89 )