-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilIO.py
198 lines (132 loc) · 5.59 KB
/
utilIO.py
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
196
197
198
import sys, os, re
import shutil
import cv2
import numpy as np
from os.path import dirname
import copy
def createParentDirectory(path_file):
pathdir = dirname(path_file)
mkdirp(pathdir)
def mkdirp(directory):
if not os.path.isdir(directory):
os.makedirs(directory)
def deleteFolder(directory):
if os.path.isdir(directory):
shutil.rmtree(directory, ignore_errors=True)
def listFilesRecursive(path_dir):
try:
listOfFile = os.listdir(path_dir)
except Exception:
pathdir_exec = os.path.dirname(os.path.abspath(__file__))
path_dir = pathdir_exec + "/" + path_dir
listOfFile = os.listdir(path_dir)
list_files = list()
for entry in listOfFile:
fullPath = os.path.join(path_dir, entry)
if os.path.isdir(fullPath):
list_files = list_files + listFilesRecursive(fullPath)
else:
list_files.append(fullPath)
list_files.sort()
return list_files
def match_SRC_GT_Images(list_src_images, list_gt_images):
list_matched_data = []
for idx_image in range(len(list_src_images)):
src_image = list_src_images[idx_image]
gt_image = list_gt_images[idx_image]
src_basename = os.path.basename(src_image).split(".")[0]
gt_basename = os.path.basename(gt_image).split(".")[0]
print('*'*80)
print("Image %d:" % (idx_image))
print("\t%s" % (src_image))
print("\t%s" % (gt_image))
assert(src_basename == gt_basename)
list_matched_data.append( (src_image, gt_image))
print("SRC and GT images are match.")
return list_matched_data
def load_gt_image(path_file, regions_mask=None):
file_img = cv2.imread(path_file, cv2.IMREAD_UNCHANGED,) # 4-channel
if file_img is None :
raise Exception(
'It is not possible to load the image\n'
"Path: " + str(path_file)
)
return file_img
def load_src_image(path_file, mode=cv2.IMREAD_COLOR):
file_img = cv2.imread(path_file, mode)
if file_img is None :
raise Exception(
'It is not possible to load the image\n'
"Path: " + str(path_file)
)
return file_img
def saveImage (image, path_file):
assert 'numpy.ndarray' in str(type(image))
assert type(path_file) == str
path_dir = dirname(path_file)
if not os.path.exists(path_dir):
os.makedirs(path_dir, 493)
cv2.imwrite(path_file, image)
def appendString(content_string, path_file, close_file = True):
assert type(content_string) == str
assert type(path_file) == str
path_dir = dirname(path_file)
if not os.path.exists(path_dir):
os.makedirs(path_dir, 493)
f = open(path_file,"a")
f.write(content_string + "\n")
if close_file == True:
f.close()
def writeString(content_string, path_file, close_file = True):
assert type(content_string) == str
assert type(path_file) == str
path_dir = dirname(path_file)
if not os.path.exists(path_dir):
os.makedirs(path_dir, 493)
f = open(path_file,"w")
f.write(content_string + "\n")
if close_file == True:
f.close()
def readStringFile(path_file):
assert type(path_file) == str
f = open(path_file)
content = f.read()
f.close()
assert type(content) == str
return content
def __remove_attribute_namespace(config, key):
try:
delattr(config, key)
except:
pass
def getPathModel(config):
config_copy = copy.deepcopy(config)
__remove_attribute_namespace(config_copy, 'test')
__remove_attribute_namespace(config_copy, 'db_test_src')
__remove_attribute_namespace(config_copy, 'db_test_gt')
__remove_attribute_namespace(config_copy, 'gpu')
__remove_attribute_namespace(config_copy, 'verbose')
__remove_attribute_namespace(config_copy, 'res')
__remove_attribute_namespace(config_copy, 'save')
__remove_attribute_namespace(config_copy, 'aug_test')
__remove_attribute_namespace(config_copy, 'n_aug')
__remove_attribute_namespace(config_copy, 'drop_test')
__remove_attribute_namespace(config_copy, 'm')
if config.no_mask is None or config.no_mask == False:
__remove_attribute_namespace(config_copy, 'no_mask')
str_config = str(config_copy).replace("Namespace", "modelCNN_").replace("(", "").replace(")", "").replace("=", "_").replace("'", "").replace(",","").replace(" ", "__").replace("[", "_").replace("]","_").replace("]","_").replace("/", "_")
str_config = "models/modelCNN/"+str_config + ".h5"
str_config = str_config.replace("datasets","dbs").replace("training", "train").replace("db_train","dtr").replace("pages_train", "pt")
return str_config
'''
ltrain = ["training/img-p/ej1.png", "training/img-p/ej2.png", "training/img-p/ej3.png",
"training/pixel-p/ej1.png", "training/pixel-p/ej2.png", "training/pixel-p/ej3.png" ]
lval = ["validation/img-p/ej1.png", "validation/img-p/ej2.png", "validation/img-p/ej3.png",
"validation/pixel-p/ej1.png", "validation/pixel-p/ej2.png", "validation/pixel-p/ej3.png" ]
list_training_data_images = [item for item in ltrain if "/img-" in item]
list_validation_data_images = [item for item in lval if "/img-" in item]
str_list_training_data_images = "\n".join(list_training_data_images)
str_list_validation_data_images = "\n".join(list_validation_data_images)
writeString(str_list_training_data_images, "prueba/prueba_train.txt", close_file = True)
writeString(str_list_validation_data_images, "prueba/prueba_val.txt", close_file = True)
'''