-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
130 lines (102 loc) · 4.7 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
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
import os
import sys
import cv2
import numpy as np
def makedirs(path):
# Intended behavior: try to create the directory,
# pass if the directory exists already, fails otherwise.
# Meant for Python 2.7/3.n compatibility.
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
class ResultWriter:
# 초기자(initializer)
def __init__(self, args):
self.common_args = {
'experiment_num': args.experiment_num,
'drone_folder_path': args.drone_folder_path,
'save_csv': args.save_csv,
'save_video': args.save_video,
'save_masked_pcl': args.save_masked_pcl,
'save_masked_drone': args.save_masked_drone,
'save_matching': args.save_matching,
'save_keypoints': args.save_keypoints
}
dataSetStr = os.path.basename(os.path.normpath(
self.common_args['drone_folder_path']))
self.dst_path = os.path.join(
'./', 'experiment' + str(self.common_args['experiment_num']) + '_' + dataSetStr)
makedirs(self.dst_path)
if self.common_args['save_csv'] is True:
self.f = open(os.path.join(self.dst_path, 'result.csv'), 'w')
if self.common_args['save_video'] is True:
video_size = (3000, 3000)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
video_dst = os.path.join(self.dst_path, 'output.avi')
self.vid = cv2.VideoWriter(
video_dst, fourcc, 1, video_size, True, )
def __del__(self):
if self.common_args['save_csv'] is True:
self.f.close()
if self.common_args['save_video'] is True:
self.vid.release()
# 메서드
def saveResults(self, result, idx):
cv2.imwrite(os.path.join(self.dst_path, 'result_' +
str(idx) + '.jpg'), result['image'])
if self.common_args['save_csv'] is True:
print("save_csv")
data = "Num, %d\n" % idx
self.f.write(data)
drone_total_keypoints = result['drone_total_keypoints']
data = "drone_total_keypoints, %d\n" % drone_total_keypoints
self.f.write(data)
pcl_total_keypoints = result['pcl_total_keypoints']
data = "pcl_total_keypoints, %d\n" % pcl_total_keypoints
self.f.write(data)
num_raw_matches = result['num_raw_matches']
data = "num_raw_matches, %d\n" % num_raw_matches
self.f.write(data)
num_good_matches = result['num_good_matches']
data = "num_good_matches, %d\n" % num_good_matches
self.f.write(data)
num_inliers = result['num_inliers']
data = "num_inliers, %d\n" % num_inliers
self.f.write(data)
np.savetxt(self.f, result['homography'], delimiter=",")
det = np.linalg.det(result['homography'])
data = "Det, %f\n" % det
self.f.write(data)
x = result['homography'][0][0]
y = result['homography'][1][1]
ratio = x/y
data = "ratio, %f\n\n" % ratio
self.f.write(data)
if self.common_args['save_video'] is True:
print("save_video")
self.vid.write(cv2.resize(result['image'], (3000, 3000)))
if self.common_args['save_masked_pcl'] is True:
print("save_masked_pcl")
cv2.imwrite(os.path.join(self.dst_path,
'masked_pcl.jpg'), result['masked_pcl'])
if self.common_args['save_masked_drone'] is True:
print("save_masked_drone")
cv2.imwrite(os.path.join(self.dst_path, 'masked_drone_' +
str(idx) + '.jpg'), result['masked_drone'])
if self.common_args['save_matching'] is True:
print("save_matching")
cv2.imwrite(os.path.join(self.dst_path, 'matching1_' +
str(idx) + '.jpg'), result['matching1'])
cv2.imwrite(os.path.join(self.dst_path, 'matching2_' +
str(idx) + '.jpg'), result['matching2'])
cv2.imwrite(os.path.join(self.dst_path, 'matching3_' +
str(idx) + '.jpg'), result['matching3'])
if self.common_args['save_keypoints'] is True:
print("save_keypoints")
cv2.imwrite(os.path.join(
self.dst_path, 'keypoints_lidar.jpg'), result['keypoints_lidar_image'])
cv2.imwrite(os.path.join(self.dst_path, 'keypoints_drone_' +
str(idx) + '.jpg'), result['keypoints_drone_image'])
return print("save done")