-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_classnames.py
More file actions
31 lines (25 loc) · 1.01 KB
/
extract_classnames.py
File metadata and controls
31 lines (25 loc) · 1.01 KB
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
import os
import glob
from tqdm import tqdm
label_dir = "/path/to/labelTxt"
output_dir = "/path/to/classnames"
os.makedirs(output_dir, exist_ok=True)
label_files = sorted(glob.glob(os.path.join(label_dir, "*.txt")))
print(f"Found {len(label_files)} label files.")
for label_file in tqdm(label_files, desc="Processing label files"):
# Read label file
with open(label_file, 'r') as f:
lines = f.readlines()
# Extract all class names
classnames = []
for line in lines:
parts = line.strip().split()
if len(parts) >= 10: # Ensure correct line format
classname = parts[8] # Class name is at position 9 (index 8)
classnames.append(classname)
unique_classnames = list(set(classnames))
output_filename = os.path.basename(label_file)
output_path = os.path.join(output_dir, output_filename)
with open(output_path, 'w') as f:
f.write('\n'.join(unique_classnames))
print(f"Class name extraction completed, results saved to: {output_dir}")