-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.py
More file actions
134 lines (102 loc) · 4.63 KB
/
Copy pathLoader.py
File metadata and controls
134 lines (102 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
loader.py
This extends mask-rcnn Dataset. Handles DataSet loading.
Author kasim <se.kasim.ebrahim@gmail.com>
"""
import os
import json
import skimage
import hashlib
import numpy as np
import sys
LIBS = 'libs/'
if LIBS not in sys.path:
sys.path.append(LIBS)
ROOT_DIR = os.path.abspath(".")
from mrcnn import utils
class SegmentationDataSet(utils.Dataset):
def load_doc_seg(self, dir, type):
#Data has to be validation or training.
assert type in["train", "validation"]
dataset_dir = os.path.join(dir, type)
segments = ["title", "subtitle", "paragraph", "footnotes",
"header", "footer", "page", "signature"]
for i in range(len(segments)):
self.add_class("doc_seg", i, segments[i])
annotations = list(json.load(open(os.path.join(dataset_dir, "labels.json"))).
values())
for annotation in annotations:
bounding_boxs = [box['shape_attributes'] for box in annotation['regions'].values()]
classes = [region['region_attributes'] for region in annotation['regions'].values()]
image = skimage.io.imread(os.path.join(dataset_dir, annotation['filename']))
height, width = image.shape[:2]
self.add_image(
"doc_seg",
image_id=annotation['filename'],
path=os.path.join(dataset_dir, annotation['filename']),
height=height,
width=width,
bounding_boxs=bounding_boxs,
classes=classes,
class_label="type"
)
def hash(self, string):
return int(hashlib.md5(string.encode('utf-8')).hexdigest(), 16) % (10 ** 8)
def load_mask(self, image_id):
image_info = self.image_info[image_id]
assert image_info['source'] == 'doc_seg'
image_mask = np.zeros(
[image_info['height'], image_info['width'], len(image_info['bounding_boxs'])],
dtype=np.uint8
)
for i, box in enumerate(image_info['bounding_boxs']):
start = (box['y'], box['x'])
end = (start[0]+box['height']-1, start[1]+box['width']-1)
rr, cc = skimage.draw.rectangle(start=start, end=end,
shape=(image_info["height"], image_info["width"]))
image_mask[rr, cc, i] = 1
class_ids = np.array([self.class_names.index(c["type"]) for c in image_info['classes']])
return image_mask.astype(np.bool), class_ids.astype(np.int32)
class PubLayNetDataSet(utils.Dataset):
def load_doc_seg(self, dir, type):
#Data has to be validation or training.
assert type in["train", "validation"]
dataset_dir = os.path.join(dir, type)
segments = ["text", "title", "list", "table",
"figure"]
for i in range(len(segments)):
self.add_class("pul_lay_seg", i, segments[i])
annotations = list(json.load(open(os.path.join(dataset_dir, "labels.json"))).
values())
for annotation in annotations:
bounding_boxs = [box['shape_attributes'] for box in annotation['regions'].values()]
classes = [region['region_attributes'] for region in annotation['regions'].values()]
image = skimage.io.imread(os.path.join(dataset_dir, annotation['filename']))
height, width = image.shape[:2]
self.add_image(
"pul_lay_seg",
image_id=annotation['filename'],
path=os.path.join(dataset_dir, annotation['filename']),
height=height,
width=width,
bounding_boxs=bounding_boxs,
classes=classes,
class_label="type"
)
def hash(self, string):
return int(hashlib.md5(string.encode('utf-8')).hexdigest(), 16) % (10 ** 8)
def load_mask(self, image_id):
image_info = self.image_info[image_id]
assert image_info['source'] == 'pul_lay_seg'
image_mask = np.zeros(
[image_info['height'], image_info['width'], len(image_info['bounding_boxs'])],
dtype=np.uint8
)
for i, box in enumerate(image_info['bounding_boxs']):
start = (box['y'], box['x'])
end = (start[0]+box['height']-1, start[1]+box['width']-1)
rr, cc = skimage.draw.rectangle(start=start, end=end,
shape=(image_info["height"], image_info["width"]))
image_mask[rr, cc, i] = 1
class_ids = np.array([self.class_names.index(c["type"]) for c in image_info['classes']])
return image_mask.astype(np.bool), class_ids.astype(np.int32)