DataExcel to XML

This commit is contained in:
Daniel Saavedra
2020-01-26 17:49:04 -03:00
parent 1e09ec4f57
commit 64a4712ec1
8 changed files with 3245 additions and 0 deletions

1
.gitignore vendored
View File

@@ -4,6 +4,7 @@ panel_jpg/
result_ssd7_panel_1/
result_ssd7_panel_2/
Train&Test_A/
Train&Test_B/
result_ssd7_panel/
result_ssd7_panel_cell/
Thermal/

File diff suppressed because one or more lines are too long

1
.jpg.xml Normal file
View File

@@ -0,0 +1 @@
<annotation><folder>Train_B</folder><filename>Mision 1_DJI_0069.jpg</filename><path>Train_B/images/Mision 1_DJI_0069.jpg</path><source><database>Unknown</database></source><size><width>512</width><height>640</height><depth>1</depth></size><segmented>0</segmented><object><name>4</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>399</xmin><ymin>242</ymin><xmax>415</xmax><ymax>272</ymax></bndbox></object><object><name>4</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>258</xmin><ymin>341</ymin><xmax>274</xmax><ymax>371</ymax></bndbox></object></annotation>

214
DataFlit2xml.py Normal file
View File

@@ -0,0 +1,214 @@
#!/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']
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(path_Flir.split('/')[-2:])
path_save_anns = output_path + 'anns/' + '_'.join(path_Flir.split('/')[-2:])
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)

1295
Panel_Detector_Fault.ipynb Normal file

File diff suppressed because it is too large Load Diff

28
config_7_fault.json Normal file
View File

@@ -0,0 +1,28 @@
{
"model" : {
"backend": "ssd7",
"input": 400,
"labels": ["1","2","3","4","5","6","7","8"]
},
"train": {
"train_image_folder": "Train&Test_B/images",
"train_annot_folder": "Train&Test_B/anns",
"train_image_set_filename": "Train&Test_B/train.txt",
"train_times": 1,
"batch_size": 8,
"learning_rate": 1e-4,
"nb_epochs": 10,
"warmup_epochs": 3,
"saved_weights_name": "experimento_ssd7_fault.h5",
"debug": true
},
"test": {
"test_image_folder": "Train&Test_B/images",
"test_annot_folder": "Train&Test_B/anns",
"test_image_set_filename": "Train&Test_B/test.txt"
}
}

BIN
experimento_ssd7_fault.h5 Normal file

Binary file not shown.

26
log.csv
View File

@@ -419,3 +419,29 @@ epoch,loss,val_loss
97,3.7434568858146666,4.392282009124756
98,4.092953279018402,4.400970935821533
99,3.898336341381073,4.445141792297363
0,15.208782874850701,12.099904885932581
1,12.517779222319398,9.29780756537594
2,11.099253389172087,8.594528625260539
3,9.910101813108183,7.770827549607007
4,8.87201379220697,7.3329147011486455
5,8.233459735573982,7.4922582142388645
6,7.841278509440156,6.3696390550528
7,7.244439907315411,6.6484536057087915
8,7.456777523633532,5.680388607195954
9,6.6837966272673635,6.748422978529289
10,6.9996571444257905,5.011203175160422
11,6.991896384341974,10.520304850677945
12,6.599298155247258,13.11472865716735
13,6.449381576576814,7.365987898698494
14,6.726311950683594,6.321325010328151
15,6.528120457371579,12.707196548803529
16,6.336373497814926,10.22905507728235
17,6.783499336242675,12.416464705965412
18,6.243661289892826,11.046684393242224
19,6.450892756256876,8.678086650905325
20,6.494656276702881,5.327648618328038
21,6.6301689813584845,6.921413478566639
22,6.063628935631905,4.657180942706208
23,5.98993371963501,9.678441431984973
24,6.50997715479211,7.558368946189311
25,6.291930182936228,4.799717326662433
1 epoch loss val_loss
419 97 3.7434568858146666 4.392282009124756
420 98 4.092953279018402 4.400970935821533
421 99 3.898336341381073 4.445141792297363
422 0 15.208782874850701 12.099904885932581
423 1 12.517779222319398 9.29780756537594
424 2 11.099253389172087 8.594528625260539
425 3 9.910101813108183 7.770827549607007
426 4 8.87201379220697 7.3329147011486455
427 5 8.233459735573982 7.4922582142388645
428 6 7.841278509440156 6.3696390550528
429 7 7.244439907315411 6.6484536057087915
430 8 7.456777523633532 5.680388607195954
431 9 6.6837966272673635 6.748422978529289
432 10 6.9996571444257905 5.011203175160422
433 11 6.991896384341974 10.520304850677945
434 12 6.599298155247258 13.11472865716735
435 13 6.449381576576814 7.365987898698494
436 14 6.726311950683594 6.321325010328151
437 15 6.528120457371579 12.707196548803529
438 16 6.336373497814926 10.22905507728235
439 17 6.783499336242675 12.416464705965412
440 18 6.243661289892826 11.046684393242224
441 19 6.450892756256876 8.678086650905325
442 20 6.494656276702881 5.327648618328038
443 21 6.6301689813584845 6.921413478566639
444 22 6.063628935631905 4.657180942706208
445 23 5.98993371963501 9.678441431984973
446 24 6.50997715479211 7.558368946189311
447 25 6.291930182936228 4.799717326662433