Coverage for src/deepdraw/data/drionsdb/__init__.py: 52%
42 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-11-30 15:00 +0100
« prev ^ index » next coverage.py v7.3.1, created at 2023-11-30 15:00 +0100
1# SPDX-FileCopyrightText: Copyright © 2023 Idiap Research Institute <contact@idiap.ch>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
5"""DRIONS-DB (training set) for Optic Disc Segmentation.
7The dataset originates from data collected from 55 patients with glaucoma
8(23.1%) and eye hypertension (76.9%), and random selected from an eye fundus
9image base belonging to the Ophthalmology Service at Miguel Servet Hospital,
10Saragossa (Spain). It contains 110 eye fundus images with a resolution of 600
11x 400. Two sets of ground-truth optic disc annotations are available. The first
12set is commonly used for training and testing. The second set acts as a "human"
13baseline.
15* Reference: [DRIONSDB-2008]_
16* Original resolution (height x width): 400 x 600
17* Configuration resolution: 416 x 608 (after padding)
18* Split reference: [MANINIS-2016]_
19* Protocols ``expert1`` (baseline) and ``expert2`` (human comparison):
21 * Training samples: 60
22 * Test samples: 50
23"""
25import csv
26import os
28import PIL.Image
29import PIL.ImageDraw
30import pkg_resources
32from ...data.dataset import JSONDataset
33from ...utils.rc import load_rc
34from ..loader import load_pil_1, load_pil_rgb, make_delayed
36_protocols = [
37 pkg_resources.resource_filename(__name__, "expert1.json"),
38 pkg_resources.resource_filename(__name__, "expert2.json"),
39]
41_root_path = load_rc().get("datadir.drionsdb", os.path.realpath(os.curdir))
43_pkg_path = pkg_resources.resource_filename(__name__, "masks")
46def _txt_to_pil_1(fname, size):
47 """Converts DRIONS-DB annotations to image format."""
48 with open(fname) as f:
49 rows = csv.reader(f, delimiter=",", quoting=csv.QUOTE_NONNUMERIC)
50 data = list(map(tuple, rows))
52 retval = PIL.Image.new("1", size)
53 draw = PIL.ImageDraw.ImageDraw(retval)
54 draw.polygon(data, fill="white")
55 del draw
56 return retval
59def _pad_right(img):
60 """Pads image on the right by one pixel, respects mode."""
61 retval = PIL.Image.new(img.mode, (img.size[0] + 1, img.size[1]), "black")
62 retval.paste(img, (0, 0) + img.size) # top-left pasting
63 return retval
66def _raw_data_loader(sample):
67 data = load_pil_rgb(os.path.join(_root_path, sample["data"]))
68 label = _txt_to_pil_1(os.path.join(_root_path, sample["label"]), data.size)
69 mask = load_pil_1(os.path.join(_pkg_path, sample["mask"]))
70 return dict(data=data, label=label, mask=mask)
73def _sample_101_loader(sample):
74 # pads the image on the right side to account for a difference in
75 # resolution to other images in the dataset
76 retval = _raw_data_loader(sample)
77 retval["data"] = _pad_right(retval["data"])
78 retval["label"] = _pad_right(retval["label"])
79 retval["mask"] = _pad_right(retval["mask"])
80 return retval
83def _loader(context, sample):
84 if sample["data"].endswith("_101.jpg"):
85 return make_delayed(sample, _sample_101_loader)
86 return make_delayed(sample, _raw_data_loader)
89dataset = JSONDataset(
90 protocols=_protocols, fieldnames=("data", "label", "mask"), loader=_loader
91)
92"""DRIONSDB dataset object."""