-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
90 lines (76 loc) · 2.89 KB
/
utils.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
import cv2
import os
from os import listdir
from os.path import isfile, join
# video_id, frame_id, timestamp, convnext_class_id, yolo_class_id, x, y, w, h, convnext_conf
def get_timestamp(frame_number,fps):
timestamp = float(frame_number / fps)
return timestamp
# get_video_id
def get_video_id(lines):
video_id = []
for line in lines:
if line.split(' ')[0] not in video_id:
video_id.append(line.split(' ')[0])
return video_id
def export_submit(submit_name, class_id, video_id, lines):
submit = open(submit_name, 'a')
submit.write(get_first_line(class_id, video_id, lines))
# get first line of each class
def get_first_line(class_id, video_id, lines):
for line in lines:
if video_id == line.split(' ')[0] and class_id == line.split(' ')[3]:
split_lines = line.split(' ')
return str(split_lines[0]) + " " + str(split_lines[3]) + " " + str(int(float(split_lines[2]) + 0.5)) + "\n"
# get all class_id not duplicate
def get_class_id(lines, video_id):
class_id = []
for line in lines:
if video_id == line.split(' ')[0]:
if line.split(' ')[3] not in class_id:
class_id.append(line.split(' ')[3])
return class_id
def cal_score(w_0, w_1, w_2, class_id, video_id, lines):
n_class = count_class(class_id, video_id, lines)
n_vid = count_video_lines(video_id, lines)
conf = cal_average_conf(class_id, video_id, lines)
n_overlap = cal_overlap_model(class_id, video_id, lines)
score = w_0 * (n_class / n_vid) + w_1 * conf + w_2 * (n_overlap / n_vid)
return score
def cal_average_conf(class_id, video_id, lines):
n = count_class(class_id, video_id, lines)
sum = 0
for line in lines:
if video_id == line.split(' ')[0] and class_id == line.split(' ')[3]:
conf = float(line.split(' ')[9])
sum += conf
return sum / n
def cal_overlap_model(class_id, video_id, lines):
n_overlap = 0
for line in lines:
if video_id == line.split(' ')[0] and class_id == line.split(' ')[3]:
if(int(line.split(' ')[3]) == int(line.split(' ')[4])+1):
n_overlap += 1
return n_overlap
# count number appearance of each class
def count_class(class_id, video_id, lines):
count = 0
for line in lines:
if video_id == line.split(' ')[0] and class_id == line.split(' ')[3]:
count += 1
return count
# count number of lines of each video
def count_video_lines(video_id, lines):
count = 0
for line in lines:
if video_id == line.split(' ')[0]:
count += 1
return count
def crop(image_path, crop_path, x1, y1, x2, y2):
image = cv2.imread(image_path)
crop_img = image[y1:y2, x1:x2]
cv2.imwrite(crop_path, crop_img)
# get all files in a folder and subfolder
def get_files(folder):
files = [f for f in listdir(folder) if isfile(join(folder, f))]
return files