-
Notifications
You must be signed in to change notification settings - Fork 0
/
replaceLabels.py
42 lines (35 loc) · 1.21 KB
/
replaceLabels.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
import os
# Define the root directory containing your label folders
root_dir = r'/media/ubuntu/USB DISK/TrainingData copy 9/labels/val/rpactorpedo'
# Mapping of text labels to numeric labels
label_mapping = {
'Buoy': '0',
'Glyph1': '1',
'Glyph2': '2',
'Glyph3': '3',
'Glyph4': '4',
'Gate': '5',
'5_glyph': '6',
'Torpedo_open': '7',
'Torpedo_closed': '8',
'Torpedo_hole': '9',
'Bin': '10'
}
# Function to replace text labels in a file with numeric labels
def replace_labels_in_file(file_path, mapping):
with open(file_path, 'r') as file:
lines = file.readlines()
with open(file_path, 'w') as file:
for line in lines:
for text_label, num_label in mapping.items():
if line.startswith(text_label):
line = line.replace(text_label, num_label, 1)
break
file.write(line)
# Walk through the directory tree and update files
for subdir, dirs, files in os.walk(root_dir):
for filename in files:
if filename.endswith('.txt'):
file_path = os.path.join(subdir, filename)
replace_labels_in_file(file_path, label_mapping)
print("Label conversion completed.")