-
Notifications
You must be signed in to change notification settings - Fork 19
/
DataFlit2xml.py
224 lines (159 loc) · 6.45 KB
/
DataFlit2xml.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 25 14:12:34 2020
@author: dlsaavedra
"""
import argparse
import os
import numpy as np
import errno
import flirimageextractor
import matplotlib.pyplot as plt
import pandas
import matplotlib.patches as patches
import xml.etree.cElementTree as ET
def mkdir(filename):
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
argparser = argparse.ArgumentParser(
description = 'Data flirt excel to train estructure data')
argparser.add_argument(
'-i',
'--input',
help='path data excel')
argparser.add_argument(
'-T',
'--input_thermal',
help='path thermal images')
# Example 'Thermal/'
argparser.add_argument(
'-o',
'--output',
help='folder save Train data')
#Examplo 'Train_B/'
def _main_(args):
input_path = args.input
output_path = args.output
thermal_path = args.input_thermal
mkdir(output_path)
mkdir(output_path + 'images/')
mkdir(output_path + 'anns/')
Excel = pandas.read_excel(input_path, sheet_name= 'Lista_Archivos_Fotos', header = 1)
for index_path in range(len(Excel.Archivo)):
if not pandas.notna(Excel.Archivo[index_path]):
continue
path_Flir = Excel.loc[index_path]['Archivo']
cod_falla = int(Excel.loc[index_path]['Cód. Falla'])
sev = Excel.loc[index_path]['Severidad']
#if cod_falla != 4:
# continue
## Junta las mismas fotos con distintos label EJ :
# DJI_0021B ---> DJI_0021
aux = path_Flir.split('/')[-2:]
if len(aux[1].split('.')[0]) > 8:
aux[1] = aux[1].split('.')[0][:8] + '.' + aux[1].split('.')[1]
path_Flir_aux = thermal_path + '/'.join(path_Flir.split('/')[-2:])
if not os.path.isfile(path_Flir_aux):
print ('No existe la imagen', path_Flir_aux)
continue
flir = flirimageextractor.FlirImageExtractor()
try:
flir.process_image(path_Flir_aux)
I = flirimageextractor.FlirImageExtractor.get_thermal_np(flir)
w, h = I.shape
except:
print('No se puede leer la imagen Flir', path_Flir_aux)
continue
dic_data = flir.get_metadata(path_Flir_aux)
meas = [s for s in dic_data.keys() if "Meas" in s]
q_bbox = len(meas)//3 # cada bbox tiene 3 parametros
param_bbox = []
for num_bbox in range(1, q_bbox + 1):
# Se guarda los parametros de los boundibox (xmin, ymin, width, height) width = xmax- xmin
param_bbox.append(list(map(int, dic_data['Meas' + str(num_bbox) + 'Params'].split(' '))))
##### Save Image and create XML annotations type of fault
path_save_img = output_path + 'images/' + '_'.join(aux)
path_save_anns = output_path + 'anns/' + '_'.join(aux)
path_save_anns = path_save_anns[:-4] + '.xml'
if not os.path.isfile(path_save_img):
plt.imsave(path_save_img , I, cmap = 'gray')
#si el archivo ya existe se agregan mas anotaciones
if os.path.isfile(path_save_anns):
et = ET.parse(path_save_anns)
root = et.getroot()
for box in param_bbox:
obj = ET.SubElement(root, "object")
ET.SubElement(obj, "name").text = str(cod_falla)
ET.SubElement(obj, "pose").text = 'Unspecified'
ET.SubElement(obj, "truncated").text = str(0)
ET.SubElement(obj, "difficult").text = str(0)
bx = ET.SubElement(obj, "bndbox")
ET.SubElement(bx, "xmin").text = str(box[0])
ET.SubElement(bx, "ymin").text = str(box[1])
ET.SubElement(bx, "xmax").text = str(box[0] + box[2])
ET.SubElement(bx, "ymax").text = str(box[1] + box[3])
tree = ET.ElementTree(root)
tree.write(path_save_anns)
## Si no existe se crea desde cero
else:
root = ET.Element("annotation")
ET.SubElement(root, "folder").text = output_path[:-1]
ET.SubElement(root, "filename").text = '_'.join(path_Flir.split('/')[-2:])
ET.SubElement(root, "path").text = path_save_img
source = ET.SubElement(root, "source")
ET.SubElement(source, "database").text = 'Unknown'
size = ET.SubElement(root, "size")
ET.SubElement(size, "width").text = str(w)
ET.SubElement(size, "height").text = str(h)
ET.SubElement(size, "depth").text = str(1)
ET.SubElement(root, "segmented").text = '0'
for box in param_bbox:
obj = ET.SubElement(root, "object")
ET.SubElement(obj, "name").text = str(cod_falla)
ET.SubElement(obj, "pose").text = 'Unspecified'
ET.SubElement(obj, "truncated").text = str(0)
ET.SubElement(obj, "difficult").text = str(0)
bx = ET.SubElement(obj, "bndbox")
ET.SubElement(bx, "xmin").text = str(box[0])
ET.SubElement(bx, "ymin").text = str(box[1])
ET.SubElement(bx, "xmax").text = str(box[0] + box[2])
ET.SubElement(bx, "ymax").text = str(box[1] + box[3])
tree = ET.ElementTree(root)
tree.write(path_save_anns)
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(input_path):
for file in f:
if '.jpg' in file:
files.append(os.path.join(r, file))
for f in files:
flir = flirimageextractor.FlirImageExtractor()
print(f)
try:
flir.process_image(f)
I = flirimageextractor.FlirImageExtractor.get_thermal_np(flir)
except:
I = plt.imread(f)
#flir.save_images()
#flir.plot()
#img = img.astype(np.int8)
W = np.where(np.isnan(I))
if np.shape(W)[1] > 0:
#xmax = np.max(np.amax(W,axis=0))
ymax = np.max(np.amin(W,axis=1))
img = I[:ymax,:]
else:
img = I
list_string = f.split('/')
list_string[-3]+= '_jpg'
f_aux = '/'.join(list_string)
mkdir(f_aux)
plt.imsave(f_aux, img, cmap = 'gray')
if __name__ == '__main__':
args = argparser.parse_args()
_main_(args)