-
Notifications
You must be signed in to change notification settings - Fork 15
/
load_mat_into_csv_xml.py
618 lines (490 loc) · 22.4 KB
/
load_mat_into_csv_xml.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 24 11:23:05 2018
@author: Tilemachos Bontzorlos
This is a sample code to experiment and transform the Singapore Maritime
Dataset (SMD) .mat object detection files into a CSV and VOC XML format for
further processing.
Dataset available here: https://sites.google.com/site/dilipprasad/home/singapore-maritime-dataset
If this dataset is used please cite it as:
D. K. Prasad, D. Rajan, L. Rachmawati, E. Rajabaly, and C. Quek,
"Video Processing from Electro-optical Sensors for Object Detection and
Tracking in Maritime Environment: A Survey," IEEE Transactions on Intelligent
Transportation Systems (IEEE), 2017.
USAGE:
python load_mat_into_csv_xml.py -i <path of unzipped SMD dataset> -o <folder to output/save csv and xml files> -f <path of the train/test folders that contain the frames>
example:
python load_mat_into_csv_xml.py -i /home/tbontz2s/singapore_dataset -o /home/tbontz2s/git/tensorflow/workspace/training_demo/images -f /home/tbontz2s/git/tensorflow/workspace/training_demo/images
"""
from scipy.io import loadmat
import os
from os import listdir
from os.path import isfile, join
import pandas as pd
import argparse
GT_FILES_PATHS_LIST = ["NIR/ObjectGT", "VIS_Onshore/ObjectGT", "VIS_Onboard/ObjectGT"]
class Frame:
"""
This is a class to save the data for each video frame
"""
csv_list = []
csv_list_initialized = False
classes_dict = {
1:"Ferry",
2:"Buoy",
3:"Vessel/ship",
4:"Speed boat",
5:"Boat",
6:"Kayak",
7:"Sail boat",
8:"Swimming person",
9:"Flying bird/plane",
10:"Other"
}
def __init__(self, frame, image_name, bb, objects, motion, distance, image_path='', xml_path=''):
"""
Parameters
----------
frame : the frame number of the video. (string or int)
image_name : the name of the image (for identification). (string)
bb : bounding box coordinates of the objects. This is an array.
Each line is the bb of an object and corresponds to
[x_min,y_min,width,height]. See the dataset webpage for more
info.
objects : the type of objects. (array)
motion : of the objects are moving or not. (array)
distance : distance of each objects. (array)
image_path : path of the file (without the filename). (string)
xml_path : path to save the generated xml file. (string)
"""
self.frame = frame
self.image_name = image_name
self.bb = bb
self.objects = objects
self.motion = motion
self.distance = distance
self.image_path = image_path
self.xml_path = xml_path
self.csv_list_initialized = False
self.xml_initialized = False
def convert_frame_to_csv(self, integer_bb=False):
"""
Tranform the frame data into a list of cvs entries. Each entry is of
the form:
('filename',
'width',
'height',
'class',
'xmin',
'ymin',
'xmax',
'ymax')
This form is suitable for generating tensorflow records.
Parameters
----------
integer_bb : should the bounding box coordinates be integers? (boolean)
Default is False.
"""
self.csv_list = []
number_of_objects = len(self.objects) # get the total number of objects
# objects is a list in a list. To avoid problems with " len([[]]) -> 1 " that sanity chack should be used.
if len(self.objects[0]) > 0:
for i in range(number_of_objects):
# avoid possible bad entries / there is one in MVI_1613_VIS_frame0.jpg
if (int(self.objects[i][0])) != 0:
if integer_bb:
entry = (self.image_name,
int(self.bb[i,2]),
int(self.bb[i,3]),
self.objects[i][0],
int(self.bb[i,0]),
int(self.bb[i,1]),
int(self.bb[i,0] + self.bb[i,2]),
int(self.bb[i,1] + self.bb[i,3])
)
else:
entry = (self.image_name,
self.bb[i,2],
self.bb[i,3],
self.objects[i][0],
self.bb[i,0],
self.bb[i,1],
self.bb[i,0] + self.bb[i,2],
self.bb[i,1] + self.bb[i,3]
)
self.csv_list.append(entry)
self.csv_list_initialized = True
def convert_frame_to_VOC_xml(self, integer_bb=False):
"""
Converts the frame data into the VOC XML format.
Code based on:
https://dataturks.com/help/ibbx_dataturks_to_pascal_voc_format.php
Parameters
----------
integer_bb : should the bounding box coordinates be integers? (boolean)
Default is False.
Returns
-------
xml : the corresponding VOC XML representation of the frame data. (string)
"""
folder_name = self.image_path.split('/')[-1]
file_path = os.path.join(self.image_path, self.image_name)
xml = ''
xml = "<annotation>\n<folder>" + folder_name + "</folder>\n"
xml = xml + "<filename>" + self.image_name +"</filename>\n"
xml = xml + "<path>" + file_path +"</path>\n"
xml = xml + "<source>\n\t<database>Unknown</database>\n</source>\n"
xml = xml + "<size>\n"
xml = xml + "\t<width>" + str(1920) + "</width>\n"
xml = xml + "\t<height>" + str(1080) + "</height>\n"
xml = xml + "\t<depth>"+str(3)+"</depth>\n"
xml = xml + "</size>\n"
xml = xml + "<segmented>Unspecified</segmented>\n"
number_of_objects = len(self.objects) # get the total number of objects
# objects is a list in a list. To avoid problems with " len([[]]) -> 1 " that sanity chack should be used.
if len(self.objects[0]) > 0:
for i in range(number_of_objects):
# avoid possible bad entries / there is one in MVI_1613_VIS_frame0.jpg
if (int(self.objects[i][0])) != 0:
xml = xml + self._get_xml_for_bbx(self.objects[i][0], self.bb[i,:], integer_bb)
xml = xml + "</annotation>"
self.xml = xml
def _get_xml_for_bbx(self, label, bb_data, integer_bb=False):
"""
Creates the VOC XML representation for an object in an image.
Code based on:
https://dataturks.com/help/ibbx_dataturks_to_pascal_voc_format.php
Parameters
----------
label : the integer label corresponding to the object class. (int)
bb_data : the bounding box data in the list format:
[xmin, ymin, width, height]
integer_bb : should the bounding box coordinates be integers? (boolean)
Default is False.
Returns
-------
xml : the corresponding object entry in xml value. (string)
"""
xmin = bb_data[0]
xmax = bb_data[0] + bb_data[2]
ymin = bb_data[1]
ymax = bb_data[1] + bb_data[3]
if integer_bb:
xmin = int(xmin)
xmax = int(xmax)
ymin = int(ymin)
ymax = int(ymax)
xml = "<object>\n"
xml = xml + "\t<name>" + str(self._convert_class_int_to_string(label)) + "</name>\n"
xml = xml + "\t<pose>Unspecified</pose>\n"
xml = xml + "\t<truncated>Unspecified</truncated>\n"
xml = xml + "\t<difficult>Unspecified</difficult>\n"
xml = xml + "\t<occluded>Unspecified</occluded>\n"
xml = xml + "\t<bndbox>\n"
xml = xml + "\t\t<xmin>" + str(xmin) + "</xmin>\n"
xml = xml + "\t\t<xmax>" + str(xmax) + "</xmax>\n"
xml = xml + "\t\t<ymin>" + str(ymin) + "</ymin>\n"
xml = xml + "\t\t<ymax>" + str(ymax) + "</ymax>\n"
xml = xml + "\t</bndbox>\n"
xml = xml + "</object>\n"
return xml
def _convert_class_int_to_string(self, class_int):
"""
TODO: write
"""
return self.classes_dict[class_int]
def get_frame_as_csv(self):
if not self.csv_list_initialized:
self.convert_frame_to_csv() # create list with float bb
return self.csv_list
def save_frame_as_xml(self):
"""
Saves the frame in the VOC xml represenation in the specified path set
in the constructor.
Skips the generation if image or xml path are empty.
"""
if not self.image_path.strip():
print('There was no valid path set for the image. Skipping xml generation.')
return
if not self.xml_path.strip():
print('There was no valid path set for the xml. Skipping xml generation.')
return
if not self.xml_initialized:
self.convert_frame_to_VOC_xml()
filename = os.path.join(self.xml_path, self.image_name.split('.')[0] + '.xml')
with open(filename, 'w') as file:
file.write(self.xml)
def generate_gt_files_dict(path_to_gt_files):
"""
Creates a dictionary with all the ground truth files location.
Parameters
----------
path_to_gt_files : the path to the ground truth files. (string)
Returns
-------
object_gt_files_dict : dictionary in the form:
(key:value) -> (<video_name>:<video_path>)
"""
object_gt_files_dict = {}
for f in listdir(path_to_gt_files):
if isfile(join(path_to_gt_files, f)):
object_gt_files_dict[f.split('.')[0].replace('_ObjectGT','')] = join(path_to_gt_files, f)
return object_gt_files_dict
def load_mat_files_in_dict(path):
"""
Loads all the .mat files of the Singapore Maritime Dataset. It converts
each frame of the .mat files into a Frame class instance and then adds it
into a dictionary called "frames".
Parameters
----------
path : the path where the .mat files are located. (string)
Returns
-------
frames : a dictionary of the form:
(key:value) -> (<video_name>_<frame_number>:<Frame class instance>)
"""
frames = {}
object_gt_files_dict = generate_gt_files_dict(path)
for key in object_gt_files_dict.keys():
file_name = object_gt_files_dict[key]
gt = loadmat(file_name)
# get the number of frames
frames_number = len(gt['structXML'][0])
# process data for each frame
for i in range(frames_number):
image_name = file_name.split('/')[-1].replace('_ObjectGT.mat','') + ('_frame%d.jpg' % i)
bb = gt['structXML'][0]['BB'][i]
objects = gt['structXML'][0]['Object'][i]
motion = gt['structXML'][0]['Motion'][i]
distance = gt['structXML'][0]['Distance'][i]
frame = Frame(i, image_name, bb, objects, motion, distance)
frames[image_name] = frame
return frames
def get_all_gt_files_in_csv(path, integer_bb=False):
"""
Create a list with ALL frames object instance in csv format. Each frame has
multiple objects. With this function we split each object as a separate
entry as a csv value in a list.
Parameters
----------
path : the path where the .mat files are located. (string)
integer_bb : should the bounding box coordinates be integers? (boolean)
Default is False.
Returns
-------
object_list : list of csv entries. Each entry is of the form:
('filename',
'width',
'height',
'class',
'xmin',
'ymin',
'xmax',
'ymax')
"""
object_list = []
frames = load_mat_files_in_dict(path)
for key in frames.keys():
frame = frames[key]
frame.convert_frame_to_csv(integer_bb)
object_list_part = frame.get_frame_as_csv()
# append part list of objects to full list of objects
object_list = object_list + object_list_part
print("Total objects in the dataset: ", len(object_list)) # TODO: maybe remove or rephrase?
return object_list
def get_gt_files_in_csv(path, frames_tuple, integer_bb=False):
"""
Create a list with frames object instances that are included in frames_list
in csv format. Each frame has multiple objects. With this function we split
each object as a separate entry as a csv value in a list.
Parameters
----------
path : the path where the .mat files are located. (string)
frames_tuple : tuple of frames names for train and test.
(train_frames, test_frames)
integer_bb : should the bounding box coordinates be integers? (boolean)
Default is False.
Returns
-------
Tuple of lists with objects in the train and test frames.
(object_list_train,object_list_test)
Each list is a list of csv entries. Each entry is of the form:
('filename',
'width',
'height',
'class',
'xmin',
'ymin',
'xmax',
'ymax')
"""
train_frames, test_frames = frames_tuple
object_list_train = []
object_list_test = []
frames = load_mat_files_in_dict(path)
for key in frames.keys():
if key in train_frames:
frame = frames[key]
frame.convert_frame_to_csv(integer_bb)
object_list_part = frame.get_frame_as_csv()
object_list_train = object_list_train + object_list_part
elif key in test_frames:
frame = frames[key]
frame.convert_frame_to_csv(integer_bb)
object_list_part = frame.get_frame_as_csv()
object_list_test = object_list_test + object_list_part
print("Total train objects produced: ", len(object_list_train))
print("Total test objects produced: ", len(object_list_test))
return (object_list_train,object_list_test)
def get_generated_frames_dict(paths):
'''
Helper function to get the generated frames from the videos in the train
and test dataset.
Parameters
----------
paths : a tuple with the train and test path. (train_path,test_path)
Returns
-------
tuple of train frames and test frames list
'''
train_path, test_path = paths
train_frames = [frame for frame in os.listdir(train_path)
if isfile(os.path.join(train_path, frame))]
test_frames = [frame for frame in os.listdir(test_path)
if isfile(os.path.join(test_path, frame))]
return (train_frames, test_frames)
def generate_split_dataset_csv_xml(path, frames_tuple, paths_list, integer_bb=False):
"""
Loads all the .mat files of the Singapore Maritime Dataset.
It loads the .mat data for each frame included into the train/test dataset
and converts it into a Frame class instance.
Each frame is added in a "frames" dictionary as a Frame object.
Each Frame object data is also converted into CSV format and added into the
corresponding list of train/test frames.
The code also invokes the generation and file write of the VOC XML files
for each Frame instance.
Parameters
----------
path : the path where the .mat files are located. (string)
frames_tuple : tuple of frames names for train and test.
(train_frames, test_frames)
paths_list : list of the paths for images and annotations.
[images_train_path, images_test_path, xml_annotations_train_path, xml_annotations_test_path]
integer_bb : should the bounding box coordinates be integers? (boolean)
Default is False.
Returns
-------
frames : a dictionary of the form:
(key:value) -> (<video_name>_<frame_number>:<Frame class instance>)
object_list_train : list with objects in the train frames.
This is a list of csv entries. Each entry is of the form:
('filename',
'width',
'height',
'class',
'xmin',
'ymin',
'xmax',
'ymax')
object_list_test : list with objects in the test frames.
This is a list of csv entries. Each entry is of the form:
('filename',
'width',
'height',
'class',
'xmin',
'ymin',
'xmax',
'ymax')
"""
train_frames, test_frames = frames_tuple
images_train_path, images_test_path, xml_annotations_train_path, xml_annotations_test_path = paths_list
frames = {}
object_list_train = []
object_list_test = []
object_gt_files_dict = generate_gt_files_dict(path)
for key in object_gt_files_dict.keys():
file_name = object_gt_files_dict[key]
gt = loadmat(file_name)
# get the number of frames
frames_number = len(gt['structXML'][0])
# process data for each frame
for i in range(frames_number):
image_name = file_name.split('/')[-1].replace('_ObjectGT.mat','') + ('_frame%d.jpg' % i)
if image_name in train_frames:
bb = gt['structXML'][0]['BB'][i]
objects = gt['structXML'][0]['Object'][i]
motion = gt['structXML'][0]['Motion'][i]
distance = gt['structXML'][0]['Distance'][i]
frame = Frame(i, image_name, bb, objects, motion, distance, images_train_path, xml_annotations_train_path)
frames[image_name] = frame
object_list_part = frame.get_frame_as_csv()
object_list_train = object_list_train + object_list_part
frame.save_frame_as_xml()
elif image_name in test_frames:
bb = gt['structXML'][0]['BB'][i]
objects = gt['structXML'][0]['Object'][i]
motion = gt['structXML'][0]['Motion'][i]
distance = gt['structXML'][0]['Distance'][i]
frame = Frame(i, image_name, bb, objects, motion, distance, images_test_path, xml_annotations_test_path)
frames[image_name] = frame
object_list_part = frame.get_frame_as_csv()
object_list_test = object_list_test + object_list_part
frame.save_frame_as_xml()
return frames, object_list_train, object_list_test
# Initiate argument parser
parser = argparse.ArgumentParser(
description="Sample TensorFlow SMD MAT-to-CSV-XML converter")
parser.add_argument("-i",
"--inputDir",
help="Path to the folder where the unziped GT files are stored",
type=str)
parser.add_argument("-o",
"--outputDir",
help="Name of output directory", type=str)
parser.add_argument("-f",
"--framesDir",
help="Directory that has the train and test folder with the frames", type=str)
args = parser.parse_args()
if(args.inputDir is None):
args.inputDir = os.getcwd()
if(args.outputDir is None):
args.outputDir = args.inputDir# + "/labels.csv"
if(args.framesDir is None):
args.framesDir = os.getcwd()
assert(os.path.isdir(args.inputDir))
assert(os.path.isdir(args.outputDir))
assert(os.path.isdir(args.framesDir))
images_train_path = os.path.join(args.framesDir, 'train')
images_test_path = os.path.join(args.framesDir, 'test')
xml_annotations_train_path = os.path.join(args.framesDir, 'train_annotations')
xml_annotations_test_path = os.path.join(args.framesDir, 'test_annotations')
# generate the xml folders if they are not there
if not os.path.isdir(xml_annotations_train_path):
os.mkdir(xml_annotations_train_path)
if not os.path.isdir(xml_annotations_test_path):
os.mkdir(xml_annotations_test_path)
train_frames, test_frames =get_generated_frames_dict(
[os.path.join(args.framesDir, 'train'),
os.path.join(args.framesDir, 'test')])
# generate tuple of frames and list of paths
frames_tuple = (train_frames, test_frames)
paths_list = [images_train_path, images_test_path, xml_annotations_train_path, xml_annotations_test_path]
objects_list_train = []
objects_list_test = []
for mat_file in GT_FILES_PATHS_LIST:
_, object_list_train_temp, object_list_test_temp = generate_split_dataset_csv_xml(os.path.join(args.inputDir, mat_file), frames_tuple, paths_list, integer_bb=False)
#_, object_list_train_temp, object_list_test_temp = get_gt_files_in_csv(
# os.path.join(args.inputDir, mat_file), (train_frames, test_frames), False)
objects_list_train = objects_list_train + object_list_train_temp
objects_list_test = objects_list_test + object_list_test_temp
print('Total objects in train dataset: ', len(objects_list_train))
print('Total objects in test dataset: ', len(objects_list_test))
column_name = ['filename', 'width', 'height',
'class', 'xmin', 'ymin', 'xmax', 'ymax']
objects_train_df = pd.DataFrame(objects_list_train, columns=column_name)
objects_test_df = pd.DataFrame(objects_list_test, columns=column_name)
objects_train_df.to_csv(args.outputDir + '/train_labels.csv', index=None)
objects_test_df.to_csv(args.outputDir + '/test_labels.csv', index=None)
print('Successfully converted mat to csv.')