-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml-2.py
186 lines (146 loc) · 4.72 KB
/
ml-2.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# ml-2.py
# This file is used to train homography matrices
# to learn to distinguish good and bad images
# with updated border
import peek
import cv2
import math
import json
TRAIN_N = 60 # <= 100
VALID_N = 40
TEST_N = 100
ROTATION_BORDER = 5 # < border is good
PROJECTIVE_BORDER = 3.5 # >= border is good
TRAIN_PATH = "complete/"
path_len = len(TRAIN_PATH)
TYPE = "border" # type = bad-17/good-73/borderline-10
USE_TYPE = True
LOG = True
SHOW_RESULT = True
def getImgPath(img_paths, img):
return [img_paths[j] for j in range(len(img_paths)) if \
img.find(img_paths[j][9:-5]) != -1][0]
def getThetas():
rotation_thetas_r2 = {}
rotation_thetas_r1 = {}
img_paths = peek.getAllImgPaths(TRAIN_PATH)
imgs = peek.getAllImgs(TYPE)
if not USE_TYPE and len(img_paths) < TRAIN_N:
print("img_paths != TRAIN_N")
exit(0)
n = len(imgs) if USE_TYPE else TRAIN_N
for i in range(n):
img_path = getImgPath(img_paths, imgs[i]) if USE_TYPE else img_paths[i]
Hl, Hr = peek.readmat(img_path, i)
## derive rotation angle from homography
theta = - math.atan2(Hl[0,1], Hl[0,0]) * 180 / math.pi
theta_abs_r2 = round(abs(theta),2)
theta_abs_r1 = round(abs(theta),1)
rotation_thetas_r2[i] = (img_path[path_len:-5], theta_abs_r2)
rotation_thetas_r1[i] = (img_path[path_len:-5], theta_abs_r1)
if LOG:
print("[Rotation][" + \
str(i) + ":" + img_path + "] " + \
str(theta_abs_r2) + " degree(s)")
with open('rotation_thetas_r2.json', 'w') as outfile:
json.dump(rotation_thetas_r2, outfile)
with open('rotation_thetas_r1.json', 'w') as outfile:
json.dump(rotation_thetas_r1, outfile)
return rotation_thetas_r2
def getProjComponents():
proj_components_r2 = {}
proj_components_r1 = {}
img_paths = peek.getAllImgPaths(TRAIN_PATH)
imgs = peek.getAllImgs(TYPE)
if not USE_TYPE and len(img_paths) < TRAIN_N:
print("img_paths != TRAIN_N")
exit(0)
n = len(imgs) if USE_TYPE else TRAIN_N
for i in range(n):
img_path = getImgPath(img_paths, imgs[i]) if USE_TYPE else img_paths[i]
Hl, Hr = peek.readmat(img_path, i)
## derive rotation angle from homography
p1_abs = abs(Hl[0,2])
p2_abs = abs(Hl[1,2])
p3_abs = abs(Hl[2,2])
# close = abs((p1_abs - 0) + (p2_abs - 0) + (p3_abs - 1))
s = 1 / p3_abs
close = abs(s*(p1_abs - 0) + s*(p2_abs - 0))
close_log_r2 = round(-math.log10(close),2)
close_log_r1 = round(-math.log10(close), 1)
proj_components_r2[i] = (img_path[path_len:-5], close_log_r2)
proj_components_r1[i] = (img_path[path_len:-5], close_log_r1)
if LOG:
print("[Projective][" + \
str(i) + ":" + img_path + "]" + \
str(close_log_r2))
with open('proj_components_r2.json', 'w') as outfile:
json.dump(proj_components_r2, outfile)
with open('proj_components_r1.json', 'w') as outfile:
json.dump(proj_components_r1, outfile)
return proj_components_r2
def ml_rotation():
rotation_thetas = getThetas()
good = 0
bad = 0
goods = []
bads = []
for i in rotation_thetas.keys():
(img, theta) = rotation_thetas[i]
if theta < ROTATION_BORDER:
good += 1
goods.append(int(img))
else:
bad += 1
bads.append(int(img))
if SHOW_RESULT:
print("[Rotation] good=" + str(good) + \
" bad=" + str(bad) + ",\ngoods=" + str(goods) +\
"\nbads=" + str(bads) + "\ngoods/all =" + \
str(good/(good+bad)))
return goods, bads
def ml_projective():
proj_components = getProjComponents()
good = 0
bad = 0
goods = []
bads = []
for i in proj_components.keys():
(img, close_log) = proj_components[i]
if close_log >= PROJECTIVE_BORDER:
good += 1
goods.append(int(img))
else:
bad += 1
bads.append(int(img))
if SHOW_RESULT:
print("[Projective] good=" + str(good) + \
" bad=" + str(bad) + ",\ngoods=" + str(goods) +\
" \nbads=" + str(bads) + "\ngoods/all =" + \
str(good/(good+bad)))
return goods,bads
def in_sequenceGoods(rotation_goods, proj_goods):
intersection_set = rotation_goods.intersection(proj_goods)
if SHOW_RESULT:
print("[Rot+Proj] result goods=" + str(sorted(list(intersection_set))))
return intersection_set
def in_sequenceBads(rotation_bads, proj_bads):
union_set = rotation_bads.union(proj_bads)
if SHOW_RESULT:
print("[Rot+Proj] result bads=" + str(sorted(list(union_set))))
return union_set
def ml():
rotationGB = ml_rotation()
projectiveGB = ml_projective()
rotation_goods = set(rotationGB[0])
proj_goods = set(projectiveGB[0])
total_goods = in_sequenceGoods(rotation_goods, proj_goods)
rotation_bads = set(rotationGB[1])
proj_bads = set(projectiveGB[1])
total_bads = in_sequenceBads(rotation_bads, proj_bads)
if SHOW_RESULT:
print("[Rot+Proj] result good=" + str(len(total_goods)) + \
" result bad=" + str(len(total_bads)) + "\ngoods/all =" + \
str(len(total_goods)/(len(total_goods)+len(total_bads))))
if __name__ == '__main__':
ml()