This repository was archived by the owner on Sep 16, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathget_stats.py
More file actions
142 lines (111 loc) · 3.94 KB
/
Copy pathget_stats.py
File metadata and controls
142 lines (111 loc) · 3.94 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
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
#!/usr/bin/python
import csv
import os
from datetime import datetime
from src.config import DEBUG, DIR_MASKS, DIR_META
from src.mask_types import MaskTypes
from src.oid import get_oid_label
from src.utils import count_files, flush_spacer, get_folder
import argparse
import os
total = 0
def main(args):
class_stats = {}
csv_data = []
unique_images = []
masks = 0
percent = 0
time_elapsed = 0
if DEBUG is True:
start_time = datetime.now()
print(
"› Starting Validation: {}".format(start_time.strftime("%Y-%m-%d %H:%M:%S"))
)
print("› Loading Masks ...", end="\r", flush=True)
# Make meta folder if it is not already present
os.makedirs(DIR_META, exist_ok=True)
# Define folder name
folder_name = get_folder(args["type"])
# Define download folder path
download_folder = os.path.join(DIR_MASKS, folder_name)
for f in os.listdir(download_folder):
if f.lower().endswith((".png")):
masks += 1
# Get data from file name
uuid = f.replace(".png", "", 1)
image_id = uuid.split("_", 1)[0]
segment_id = uuid.rpartition("_")[-1]
# Generate class ID
code = uuid.replace(image_id + "_", "")
code = code.replace("_" + segment_id, "")
if code[0] == "g":
code = code.replace("g", "/g/", 1)
elif code[0] == "m":
code = code.replace("m", "/m/", 1)
elif code[0] == "o" and code[1] == "i":
code = code.replace("oi", "/oi/", 1)
# Get class ID from label
label = get_oid_label(code)
# Create class stats
if label not in class_stats:
class_stats[label] = {
"code": code,
"label": label,
"masks": 0,
"images": 0,
}
# Update mask count
class_stats[label]["masks"] += 1
# Add image to unique images
if image_id not in unique_images:
unique_images.append(image_id)
class_stats[label]["images"] += 1
if DEBUG is True:
time_elapsed = datetime.now() - start_time
percent = (masks / total) * 100
print(
"› {:.2f}%: {}/{} ({}) {}".format(
percent, masks, total, time_elapsed, flush_spacer(25)
),
end="\r",
flush=True,
)
# Sort Data
sorted_data = dict(sorted(class_stats.items()))
# Add sorted data to CSV
row_num = 0
for row in sorted_data:
sorted_data[row]["id"] = row_num
csv_data.append(sorted_data[row])
row_num += 1
# Define CSV Fields
fields = ["id", "code", "label", "masks", "images"]
# Write CSV
with open(
"{}/{}-stats.csv".format(DIR_META, args["type"]), "w", newline=""
) as outfile:
writer = csv.DictWriter(outfile, fieldnames=fields)
writer.writeheader()
writer.writerows(csv_data)
if DEBUG is True:
print("› Completed: {}".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
print(
"› {:.2f}%: {}/{} ({}) {}".format(
percent, masks, total, time_elapsed, flush_spacer(25)
)
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate Statistics for Classname Usage",
epilog="Find By Color - OID Segmentation Model",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("type", type=MaskTypes)
try:
args = vars(parser.parse_args())
total = count_files(DIR_MASKS, args["type"])
main(args)
except KeyboardInterrupt:
print(flush_spacer(100), end="\r", flush=True)
print("Exited Application")
exit(0)