forked from RyanWangZf/construction-site-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_instance.py
52 lines (38 loc) · 1.28 KB
/
count_instance.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
# -*- coding: utf-8 -*-
import numpy as np
import os
import pdb
import json
from collections import defaultdict
def count_instance(name_list=None):
json_dir = "mask"
with open("label_list.txt", "r") as f:
all_labels = [x.split()[0] for x in f.readlines()]
# instance_stats = dict().fromkeys(all_labels, 0)
instance_stats = defaultdict(int)
if name_list is None:
# count all
json_list = [os.path.join(json_dir,x) for x in os.listdir(json_dir)]
else:
# filter json list
json_list = [os.path.join(json_dir,"{}.json".format(x)) for x in name_list]
for idx,file in enumerate(json_list):
# print(idx+1, file)
with open(file, "r") as f:
json_txt = json.load(f)
num_instances = len(json_txt["shapes"])
for i in range(num_instances):
instance_stats[json_txt["shapes"][i]["label"]] += 1
print(instance_stats)
return instance_stats
if __name__ == '__main__':
# get train name list
with open("train_list.txt","r") as f:
name_list = f.readlines()
name_list = [x.split(".")[0] for x in name_list]
# stats = count_instance(name_list)
stats = count_instance()
stats_list = list(stats.values())
print(stats_list)
pdb.set_trace()
pass