-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_segmentation.py
More file actions
198 lines (139 loc) · 5.86 KB
/
run_segmentation.py
File metadata and controls
198 lines (139 loc) · 5.86 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""Load Modules"""
import os
import cv2
from glob import glob
import numpy as np
import random
import tensorflow as tf
from imageio import imread, imsave
import tensorflow.contrib.slim as slim
from datetime import datetime
from tqdm import tqdm
from dh_segment.io import PAGE
from dh_segment.inference import LoadedModel
from dh_segment.post_processing import boxes_detection, binarization
import xml.etree.ElementTree as ET
import argparse
parser = argparse.ArgumentParser(description='Run dhSegment and generate Zone.xml that follows PAGE XML-schema (https://www.primaresearch.org/tools/PAGELibraries)')
parser.add_argument('-i', '--imagepath', type=str, required=True,
help='a path to the root directory of Zone xml files')
parser.add_argument('-s', '--savepath', type=str, required=True,
help='a path to the root directory of save files')
parser.add_argument('-t', '--threshold', type=int, default=0.005,
help='a threshold for ignoring small zones [0,1] (default: 0.005)')
args = parser.parse_args()
"""Parse Args"""
IMAGE_DIR = args.imagepath
SAVE_DIR = args.savepath
CONNECTIVITY = 4
SM_ZONE_RATIO = args.threshold
LOG_DIR = './log'
log_filename = datetime.now().strftime('dhSegment_%H_%M_%d_%m_%Y.log')
os.environ["CUDA_VISIBLE_DEVICES"]="0"
BG_ID = 0
TEXT_ID = 1
FIGURE_ID = 2
LINE_ID = 3
TABLE_ID = 4
"""Prepare Dirs"""
try:
os.makedirs(LOG_DIR)
os.makedirs(SAVE_DIR)
except FileExistsError:
# directory already exist
pass
"""Start session"""
session = tf.InteractiveSession()
"""Load model"""
model_dir = './dhSegment/pretrained_models/ENP_500_model_v3/export/1564890842/'
m = LoadedModel(model_dir, predict_mode='filename')
"""
1. Preparation
Input batch
"""
image_list = glob(os.path.join(IMAGE_DIR,'**/*.jpg'),recursive=True)
with open(os.path.join(LOG_DIR,log_filename),'w') as fl:
fl.write("{} file(s) are found under:\n{}\n".format(len(image_list),IMAGE_DIR))
for _idx,input_path in enumerate(tqdm(image_list)):
try:
fl.write("\n***[{}/{}]***\n".format(_idx+1,len(image_list)))
# Create the file structure
data_PcGts = ET.Element('PcGts')
data_PcGts.set('xmlns','http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15')
data_PcGts.set('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance')
data_PcGts.set('xsi:schemaLocation','http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15 http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15/pagecontent.xsd')
data_meta = ET.SubElement(data_PcGts, 'Metadata')
data_page = ET.SubElement(data_PcGts,'Page')
# Read image
img = cv2.imread(input_path)
# Parse filename
basename = os.path.basename(input_path)
basename_wo_ext = os.path.splitext(basename)[0]
fl.write("input_path\t\t: {}\n".format(input_path))
fl.write("basename\t\t: {}\n".format(basename))
fl.write("basename (w/o ext)\t: {}\n".format(basename_wo_ext))
"""
2. Main
Run prediction
"""
# Run prediction
prediction_outputs = m.predict(input_path)
pred_labels = np.copy(prediction_outputs['labels'][0]).astype(np.uint8)
"""
2. Main
Get basic attributes
"""
oriH,oriW = np.shape(img)[:2]
newH,newW = np.shape(pred_labels)
data_page.set("HEIGHT",str(oriH))
data_page.set("WIDTH",str(oriW))
"""
3. Postprocessing
Generate binary mask for each class
"""
mask_texts = np.copy(pred_labels)
mask_texts[mask_texts != TEXT_ID] = 0
"""
3. Postprocessing
Generate polygones for each class
"""
txt_num_labels, txt_labels, txt_stats, txt_centroids = cv2.connectedComponentsWithStats(mask_texts, CONNECTIVITY, cv2.CV_32S)
"""
4. Postprocessing (TextRegion; Rectangle)
"""
factor_h = oriH/newH
factor_w = oriW/newW
# Get rectangle region
cnt_remove = 0
THRESHOLD_SM_ZONE = (newH*newW)*SM_ZONE_RATIO
region_idx = 0
for bb_idx in range(1,txt_num_labels):
if txt_stats[bb_idx][4] < THRESHOLD_SM_ZONE:
cnt_remove+=1
continue
# Resize predicted coordinate
left,top,width,height = txt_stats[bb_idx][:4]
left = int(left*factor_w)
width = int(width*factor_w)
top = int(top*factor_h)
height = int(height*factor_h)
p1 = (left,top)
p2 = (left+width,top+height)
region_idx +=1
# Inject coordinates
data_textBlock = ET.SubElement(data_page, 'TextBlock')
data_textBlock.set("ID",str(region_idx))
data_textBlock.set("HEIGHT",str(height))
data_textBlock.set("WIDTH",str(width))
data_textBlock.set("HPOS",str(left))
data_textBlock.set("VPOS",str(top))
# Finalize file structure in xml format
data_page_xml = ET.tostring(data_PcGts)
# Save xml
save_xml_filename = basename_wo_ext + '_dhSegment' + '.xml'
with open(os.path.join(SAVE_DIR, save_xml_filename), "wb") as data_page_xml_file:
data_page_xml_file.write(data_page_xml)
fl.write("Total {} textRegion(s) are found.\n...{} region(s) are removed from the original finding.\n".format(txt_num_labels-cnt_remove,cnt_remove))
except Exception as e:
fl.write("unexpected error occurred:\n{}".format(e))
pass