1#!/usr/bin/env python
2# coding=utf-8
3
4
5"""Data loading code"""
6
7
8import functools
9import os
10
11import PIL.Image
12
13from .sample import DelayedSample
14
15
16def load_pil_rgb(path):
17 """Loads a sample data
18
19 Parameters
20 ----------
21
22 path : str
23 The full path leading to the image to be loaded
24
25
26 Returns
27 -------
28
29 image : PIL.Image.Image
30 A PIL image in RGB mode
31
32 """
33
34 return PIL.Image.open(path).convert("RGB")
35
36
37def load_pil_1(path):
38 """Loads a sample binary label or mask
39
40 Parameters
41 ----------
42
43 path : str
44 The full path leading to the image to be loaded
45
46
47 Returns
48 -------
49
50 image : PIL.Image.Image
51 A PIL image in mode "1"
52
53 """
54
55 return PIL.Image.open(path).convert(mode="1", dither=None)
56
57
58def make_delayed(sample, loader, key=None):
59 """Returns a delayed-loading Sample object
60
61 Parameters
62 ----------
63
64 sample : dict
65 A dictionary that maps field names to sample data values (e.g. paths)
66
67 loader : object
68 A function that inputs ``sample`` dictionaries and returns the loaded
69 data.
70
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.
75
76
77 Returns
78 -------
79
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.
83
84 """
85
86 return DelayedSample(
87 functools.partial(loader, sample),
88 key=key or os.path.splitext(sample["data"])[0],
89 )