Skip to content

Commit ac6b6c6

Browse files
authored
Merge pull request #12 from silenceagle/new-feature
New feature
2 parents a507638 + f534429 commit ac6b6c6

File tree

2 files changed

+152
-7
lines changed

2 files changed

+152
-7
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ normalize datapoint to unit length (L2 norm equals 1)
9090

9191
### *function* gen_dataset_ssdd
9292

93-
### *function* gen_dataset_hrsc2016
93+
### *function* gen_dataset_hrsc2016
94+
95+
### *function* rename_and_save_opensarship
96+
97+
### *function* gen_dataset_ship_dataset
9498

9599

96100

xml_process.py

Lines changed: 147 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from tqdm import tqdm
99
import cv2
1010
import numpy as np
11-
11+
import shutil
12+
import csv
1213

1314
def pnpoly(test_point, polygon):
1415
"""
@@ -85,7 +86,7 @@ def gen_dataset_ssdd(xml_path, source_img_path, save_img_path):
8586
sub_imagename = img_name+'_'+str(xmin)+'_'+str(ymin)+'_'+str(xmax)+'_'+str(ymax)+'.png'
8687
cv2.imwrite(os.path.join(save_img_path, sub_imagename), sub_image[:, :, 0])
8788

88-
89+
8990
def gen_dataset_hrsc2016(xml_path, source_img_path, save_img_path):
9091
"""
9192
crop ship images from original bigger images and save them to folders named with their categories and keep the train
@@ -329,11 +330,151 @@ def gen_dataset_hrsc2016(xml_path, source_img_path, save_img_path):
329330
cv2.imwrite(os.path.join(ship_save_folder, sub_imagename), sub_image)
330331
except: #
331332
print(f'''could not find {os.path.join(xml_path, test_img_name + '.xml')}''')
333+
334+
335+
def get_ship_type_given_ais_type_number(ship_type_number):
336+
"""
337+
return the ship type name according to the given ship type number in AIS information
338+
:param ship_type_number: int, [20, 99]
339+
:return: str, ship type name
340+
"""
341+
assert ship_type_number >= 20 and ship_type_number <= 99
342+
if ship_type_number <= 29:
343+
return 'Wing in ground'
344+
elif ship_type_number <= 30:
345+
return 'Fishing'
346+
elif ship_type_number <= 32:
347+
return 'Towing'
348+
elif ship_type_number <= 33:
349+
return 'Dredging or underwater ops'
350+
elif ship_type_number <= 34:
351+
return 'Diving ops'
352+
elif ship_type_number <= 35:
353+
return 'Military Ops'
354+
elif ship_type_number <= 36:
355+
return 'Sailing'
356+
elif ship_type_number <= 37:
357+
return 'Pleasure Craft'
358+
elif ship_type_number <= 39:
359+
return 'Reserved'
360+
elif ship_type_number <= 49:
361+
return 'High speed craft'
362+
elif ship_type_number <= 50:
363+
return 'Pilot Vessel'
364+
elif ship_type_number <= 51:
365+
return 'Search and Rescue vessel'
366+
elif ship_type_number <= 52:
367+
return 'Tug'
368+
elif ship_type_number <= 53:
369+
return 'Port Tender'
370+
elif ship_type_number <= 54:
371+
return 'Anti-pollution equipment'
372+
elif ship_type_number <= 55:
373+
return 'Law Enforcement'
374+
elif ship_type_number <= 57:
375+
return 'Spare'
376+
elif ship_type_number <= 58:
377+
return 'Medical Transport'
378+
elif ship_type_number <= 59:
379+
return 'Ship according to RR Resolution No. 18'
380+
elif ship_type_number <= 69:
381+
return 'Passenger'
382+
elif ship_type_number <= 79:
383+
return 'Cargo'
384+
elif ship_type_number <= 89:
385+
return 'Tanker'
386+
elif ship_type_number <= 99:
387+
return 'Other Type'
388+
389+
390+
def rename_and_save_opensarship(source_path, save_path):
391+
"""
392+
rename the tif images in 'Patch' to '{TYPE}_{AIS TYPE NUMBER}_{MARINE TRAFFIC TYPE}_ort_{}_{}_x_{}_y{}.tif'
393+
original ship type|AIS information|Marine traffic information|orientation sar and AIS|center x and y
394+
and save them to a new folder structed by save_path/Cargo/sub type folders/ ship images
395+
/Cargo images
396+
/...
397+
:param source_path: str, original data root path, the structure should be source_path/subfolder/Patch/tif files
398+
/Ship.xml
399+
:param save_path: str
400+
:return:
401+
"""
402+
if not os.path.exists(source_path):
403+
raise FileExistsError('path not found! : %s' % source_path)
404+
pbar = tqdm(os.scandir(source_path))
405+
for sub_folder in pbar:
406+
if sub_folder.is_dir():
407+
pbar.set_description("Processing %s" % sub_folder.path)
408+
# GRDH or SLC
409+
sar_mode = sub_folder.name.split('_')[2]
410+
document = xml.dom.minidom.parse(os.path.join(sub_folder.path, 'Ship.xml'))
411+
ships = document.getElementsByTagName('ShipList')[0].getElementsByTagName('ship')
412+
for ship in ships:
413+
ship_information = ship.getElementsByTagName('SARShipInformation')[0]
414+
ship_cx = int(ship_information.getElementsByTagName('Center_x')[0].firstChild.data)
415+
ship_cy = int(ship_information.getElementsByTagName('Center_y')[0].firstChild.data)
416+
print(f'cx: {ship_cx}\tcy: {ship_cy}')
417+
ship_orientation_degree_sar = float(
418+
ship_information.getElementsByTagName('North_Direction')[0].firstChild.data)
419+
ship_orientation_sar = ship_orientation_degree_sar / 180.0 * np.pi
420+
ship_ais_information = ship.getElementsByTagName('AISShipInformation')[0]
421+
ship_orientation_degree_ais = float(
422+
ship_ais_information.getElementsByTagName('True_Head')[0].firstChild.data)
423+
ship_orientation_ais = ship_orientation_degree_ais / 180.0 * np.pi
424+
ship_type_number_ais = int(
425+
ship_ais_information.getElementsByTagName('Ship_Type')[0].firstChild.data)
426+
print(f'AIS Ship Type Number: {ship_type_number_ais}')
427+
try:
428+
ship_type_ais = get_ship_type_given_ais_type_number(ship_type_number_ais)
429+
if 'Search and Rescue vessel' == ship_type_ais:
430+
ship_type_ais = 'Search'
431+
elif 'Dredging or underwater ops' == ship_type_ais:
432+
ship_type_ais = 'Dredging'
433+
except AssertionError: # no ship type AIS information
434+
ship_type_ais = 'Other Type'
435+
print(f'AIS Ship Type: {ship_type_ais}')
436+
ship_marine_traffic_information = ship.getElementsByTagName('MarineTrafficInformation')[0]
437+
ship_type_traffic = \
438+
ship_marine_traffic_information.getElementsByTagName('Elaborated_type')[0].firstChild.data.replace('/', 'or')
439+
# original ship name TYPE_x{}_y{}.tif
440+
ori_img_name = f'{ship_type_ais}_x{ship_cx}_y{ship_cy}.tif'
441+
# new name {TYPE}_{AIS TYPE NUMBER}_{MARINE TRAFFIC TYPE}_ort_{sar}_{ais}_x_{}_y{}.tif
442+
new_name = f'{ship_type_ais}_{ship_type_number_ais}_{ship_type_traffic}_ort_' \
443+
f'{ship_orientation_sar:.3f}_{ship_orientation_ais:.3f}_' \
444+
f'x_{ship_cx}_y_{ship_cy}.tif'
445+
if ship_type_traffic == ship_type_ais:
446+
ship_save_path = os.path.join(save_path, sar_mode, ship_type_ais)
447+
else:
448+
ship_save_path = os.path.join(save_path, sar_mode, ship_type_ais, ship_type_traffic)
449+
os.makedirs(ship_save_path, exist_ok=True)
450+
try:
451+
shutil.move(os.path.join(sub_folder.path, 'Patch', ori_img_name),
452+
os.path.join(ship_save_path, new_name))
453+
except OSError:
454+
print(f'''can not open {os.path.join(sub_folder.path, 'Patch', ori_img_name)}''')
455+
pass
456+
457+
458+
def gen_dataset_ship_dataset(source_path):
459+
ship_types = ['Cargo', 'Military', 'Carrier', 'Cruise', 'Tankers']
460+
for ship_type in ship_types:
461+
os.makedirs(os.path.join(source_path, 'train', 'images', ship_type), exist_ok=True)
462+
with open(os.path.join(source_path, 'train', 'train.csv')) as csv_file:
463+
reader = csv.DictReader(csv_file)
464+
for row in reader:
465+
shutil.move(os.path.join(source_path, 'train', 'images', row['image']),
466+
os.path.join(source_path, 'train', 'images', ship_types[int(row['category'])-1], row['image']))
332467

333468

334469
if __name__ == '__main__':
335-
gen_dataset_ssdd(xml_path=r'F:\dataset_se\SSDD\Annotations',
336-
source_img_path=r'F:\dataset_se\SSDD\JPEGImages',
337-
save_img_path=r'F:\dataset_se\SSDD\crop_img')
338-
pass
470+
# gen_dataset_ssdd(xml_path=r'F:\dataset_se\SSDD\Annotations',
471+
# source_img_path=r'F:\dataset_se\SSDD\JPEGImages',
472+
# save_img_path=r'F:\dataset_se\SSDD\crop_img')
473+
# gen_dataset_hrsc2016(xml_path=r'C:\files\HRSC2016\HRSC2016\FullDataSet\Annotations',
474+
# source_img_path=r'C:\files\HRSC2016\HRSC2016',
475+
# save_img_path=r'C:\files\HRSC2016')
476+
# rename_and_save_opensarship(source_path=r'C:\files\OpenSARShip_total',
477+
# save_path=r'C:\files\OpenSARShip')
478+
gen_dataset_ship_dataset(source_path=r'C:\files\game-of-deep-learning-ship-datasets')
479+
pass
339480

0 commit comments

Comments
 (0)