forked from WongKinYiu/yolov7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploytester.py
executable file
·141 lines (109 loc) · 4.46 KB
/
deploytester.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
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
import time
import glob
# working VM library
from VMheader import VectorFormatter, BinCompletion
true_positives = []
false_positives = []
true_negatives = []
false_negatives = []
def evaluate_match(truth, guess):
if truth != guess:
return 1
else:
return 0
def deg_to_dms(deg): # degrees, minutes, seconds. Returns string
d = int(deg)
md = abs(deg - d) * 60
m = float(md)
return str(d) + str(m)
def flatten(l):
return [item for sublist in l for item in sublist]
def hamming2(s1, s2):
"""Calculate the Hamming distance between two bit strings"""
# assert len(s1) == len(s2)
return sum(c1 != c2 for c1, c2 in zip(s1, s2))
def main():
sat_crop = '..\Vermont_sim_data3\ cropped_600\ '
# Set up for Vector Construction
cropped_image_vector = []
cropped_true_match = []
cropped_imdist = []
cropped_imangle = []
cropped_feature_class = []
cropped_feature_closest = []
for frame_name in glob.glob(sat_crop + '*.jpg', recursive=True):
label_name = frame_name[:-4] + '.txt'
satellite = VectorFormatter('satellite')
satellite.yolo_initialize(label_name, frame_name)
if len(satellite.p_x) > neighbor:
satellite.k_d_tree_test(neighbor)
cropped_true_match.append(satellite.true_match)
cropped_imdist.append(satellite.im_dist)
cropped_imangle.append(satellite.im_angle)
cropped_feature_closest.append(satellite.feature_closest)
cropped_feature_class.append(satellite.feature_class)
binn = BinCompletion('binn')
binn.bin_initialize(flatten(cropped_imdist), flatten(cropped_imangle), .8, .8)
for i in range(len(cropped_true_match)):
vector_sat_all = []
for j in range(len(cropped_feature_class[i])):
vector_sat_all.append(
binn.vector_def(j, cropped_feature_class[i], cropped_imdist[i], cropped_imangle[i],
cropped_feature_closest[i])[0])
cropped_image_vector.append(vector_sat_all)
test_image_dir = '..\Vermont_sim_data2\ cropped_800\ '
distance_ratings = []
hammingtotal = []
features_detected = []
times = []
for frame_name in glob.glob(test_image_dir + '*.jpg', recursive=True):
start = time.time()
test_label_dir = frame_name[:-4] + '.txt'
drone = VectorFormatter('drone')
drone.yolo_initialize(test_label_dir, frame_name)
features_detected.append(drone.true_match)
if len(drone.p_x) > neighbor + 1:
drone.k_d_tree_test(neighbor)
vector_drone = []
for i in range(len(drone.feature_class)):
vector_drone.append(
binn.vector_def(i, drone.feature_class, drone.im_dist, drone.im_angle,
drone.feature_closest)[0])
cropped_truth = flatten(cropped_true_match)
vector_sat_all = flatten(cropped_image_vector)
distance = []
for i in range(len(vector_drone)):
last_rating = threshold
for j in range(len(vector_sat_all)):
# if vector_drone[i][0:12] == vector_sat_all[j][0:12]:
# compair drone to sat, keep only features of the same classes
rating = hamming2(vector_drone[i], vector_sat_all[j])
if rating <= last_rating:
distance = [rating, cropped_truth[j], drone.true_match[i]]
last_rating = rating
hammingtotal.append(last_rating)
distance_ratings.append(distance)
times.append(time.time() - start)
# print(distance_ratings)
# print(len(binn.min_ind_dist), len(binn.min_ind_angle))
# print(len(distance_ratings))
if len(distance_ratings) >= 1:
tp = 0
fp = 0
for i in distance_ratings:
if len(i) > 2:
if evaluate_match(i[1], i[2]) == 0:
tp = tp + 1
else:
fp = fp + 1
fn = len(flatten(features_detected)) - tp - fp
precision = tp / (tp + fp)
recall = tp / (tp + fn) # also known as senitivity
# false_positive_rate = fp/(0+fp)
average_time = sum(times) / len(times)
fps = 1 / average_time
if (tp != 0 or fp != 0):
print(neighbor, threshold, precision, recall, tp, fp, fps)
if __name__ == "__main__":
main()