-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_object_size_distribution.py
129 lines (96 loc) · 3.29 KB
/
get_object_size_distribution.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
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
import argparse
import torch
import cv2
import os
import numpy as np
import matplotlib.pyplot as plt
import math
from collections import defaultdict
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--img_dir', type=str)
parser.add_argument('--start', type=str)
parser.add_argument('--end', type=str)
args = parser.parse_args()
# Video Index (include start and end)
start = int(args.start)
end = int(args.end)
# Model
model = torch.hub.load("ultralytics/yolov5", "yolov5x6")
# Data
GT_folder = args.img_dir
# Storage
d = defaultdict(list)
# Get image size sample (video resolution)
FR_folder = os.path.join(GT_folder, str(start))
images = sorted(os.listdir(FR_folder))
idx = images[0]
img = cv2.imread(os.path.join(FR_folder, idx))[..., ::-1]
(img_h, img_w, img_c) = img.shape
# Image size catagory (percentage)
small = 1/3
medium = 3
# Iterate through every single videos
for vid in range(start, end+1):
# print(vid)
# # Storage
# d = defaultdict(list)
FR_folder = os.path.join(GT_folder, str(vid))
images = sorted(os.listdir(FR_folder))
for idx in images:
img = cv2.imread(os.path.join(FR_folder, idx))[..., ::-1]
results = model(img)
for i in range(results.xyxy[0].shape[0]):
detection = results.xyxy[0][i]
# class x_min, y_min, x_max, y_max
size = (detection[2] - detection[0]) * (detection[3] - detection[1])
# get normalized area
normalized_size = size.item() / (img_h * img_w)
# get rounded percentage
normalized_size = round(normalized_size, 3) * 100
if normalized_size < small:
d[detection[5].item()].append(0)
elif normalized_size < medium:
d[detection[5].item()].append(1)
else:
d[detection[5].item()].append(2)
# Get count for each size
count_list = [0] * 3
for keys in d:
for val in d[keys]:
count_list[int(val)] += 1
x = ['S', 'M', 'L']
y = count_list
plt.bar(x, y)
plt.xlabel('Size')
plt.ylabel('Number')
plt.title(f'object size distribution for {img_h}p')
plt.savefig(f'plot/ob_dis_{img_h}p.png', dpi=200, transparent=True)
plt.clf()
# Write txt
with open(f'plot/ob_dis_{img_h}p.txt', 'w') as f:
for cat, count in zip(x, y):
f.write(f'{cat}, {count}\n')
# Get class name
name = []
with open('coco-classes.txt') as f:
for line in f.readlines():
name.append(line[:-1])
# Get count for each class
count_list = [0] * 80
for keys in d:
count_list[int(keys)] = len(d[keys])
# x = [*range(1, 81, 1)]
x = name
y = count_list
plt.bar(x, y)
plt.xticks(x, rotation=45, fontsize=3, ha='right')
plt.xlabel('Class')
plt.ylabel('Number')
plt.title(f'object class distribution for {img_h}p')
plt.savefig(f'plot/cl_dis_{img_h}p.png', dpi=800, transparent=True)
plt.clf()
# Write txt
with open(f'plot/cl_dis_{img_h}p.txt', 'w') as f:
for cat, count in zip(x, y):
f.write(f'{cat}, {count}\n')