-
Notifications
You must be signed in to change notification settings - Fork 0
/
label_handler.py
executable file
·252 lines (168 loc) · 6.92 KB
/
label_handler.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 2 12:33:42 2021
@author: Sam I
"""
import os
import subprocess
import pandas as pd
import numpy as np
import cv2
import bootleg_autonav as ba
import image_correction as ic
#######################################################################################
#######################################################################################
#data = os.listdir(image_lib)
cur_year = 0
sequence_counts = {}
sequence_list = {}
cur_lbl = 1
wd = os.getcwd()
#tot_lbl = len(data)/2
def rotate_image(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result
# definition of a label - contains essential information for... stuff
"""
Label class
Label objects represent an image and once loaded contain all image data for processing
"""
class label:
def __init__(self, input_file, raw = False, as_png = False):
info={}
self.color = False
self.color_img = None
self.info = info
if not raw:
pointer = input_file.split('.')[0] + ".png"
if as_png:
self.path = input_file.split('.')[0] + ".lbl"
self.info["path"] = self.path
else:
self.path = input_file
self.info["path"] = pointer
# read label file ans parse into self
with open(self.path) as f_in:
for line in f_in:
if line:
line = line.strip()
if not line.find("=") == -1:
char = '\\'
values = line.replace(" ","").replace('"',"").replace(char[0],"").strip()
values = values.split("=")
self.info[values[0]] = values[1]
def get_info(self):
return self.info
# Exports self as an image of format f_type
"""
Converts the label from .img format to a format of choice
format = desired image output format - "png" or "jpg"
output = output directory for converted image
returns out - the new file path of the converted image
"""
def export_raw(self,format,output):
print(subprocess.check_output(f'cmd /k "{wd}/transform-1.11.1/bin/transform.bat -f {format} -o {output} -t {self.path}"'))
out = output +"/" + self.info["^IMAGE_HEADER"].split(".")[0].replace("(","") + "." + format
#img = cv2.imread(out)
return out
def export(self):
return self.info["path"].split('.')[0] + ".png"
def set_color(self, color_image):
self.color = True
self.color_img = color_image
return self
def export_img(self):
out = self.export("png","image_output")
return cv2.imread(out)
def rotate_image(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result
def resize_image(self):
img = cv2.imread(self.info["path"].split('.')[0] + ".png")
if int(self.info["LINES"]) < 1024:
#scale_percent = 200
#width = int(img.shape[1] * scale_percent / 100)
#height = int(img.shape[0] * scale_percent / 100)
dim = (1024,1024)
img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
return img
"""
Traslates image
format = output format
x = x pixels to move image by
y = y pixels to move image by
angle = angle to rotate image in degrees
border = T/F add border or not
"""
def translate(self,format, x,y,angle,border):
if self.color:
img = self.color_img
lines =1024
else:
image = label.export(self)
lines = int(self.info["LINES"])
img = cv2.imread(image)
M = np.float32([[1, 0, x], [0, 1, y]])
print(f"number of lines {lines}")
if border:
max_bound = max(x,y)
if max_bound > (lines*2):
border = int(max_bound) + 1024
else:
border = lines
border = 1024
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA)
img= cv2.copyMakeBorder(img,border,border,border,border,cv2.BORDER_CONSTANT,value=[0,0,0,0])
(rows, cols) = img.shape[:2]
img = cv2.warpAffine(img, M, (cols, rows),borderMode=cv2.BORDER_TRANSPARENT)
name = self.info["SPACECRAFT_CLOCK_START_COUNT".split('.')[0]]
print(name)
img = rotate_image(img,angle)
return img
"""
Automagically centers and rotates the image to align with its target
border = True/False - should a border be added around the image (default is true since the border is pretty essential)
"""
def auto_center(self, border=True):
im_year = int(self.info["IMAGE_TIME"].split('-')[0])
global cur_year
if cur_year != im_year:
ba.unload_kernels()
ba.load_kernels(im_year,im_year)
cur_year = im_year
target_loc = ba.find_target(self, False, "graphs")
print(target_loc)
return self.translate("png", target_loc[0],target_loc[1],target_loc[3],border)
def is_mosaic(self):
panels = self.info["OBSERVATION_ID"].split("X")
if len(panels) > 1:
return True
else:
return False
"""
Load all labels in the target directory
folder_path = path of labels to load
"""
def load_lbls(folder_path):
data = os.listdir(folder_path)
total = len(data) /2
cur = 0
labels = []
for i in range(0,len(data)):
file = data[i]
if file.endswith(".LBL"):
ticks = file.strip('W').strip('.LBL')
ticks = ticks[:-2]
cur+=1
print(f"Label {cur} of {total} ")
path = folder_path + "/" + file
# print(path)
labels.append(label(path))
def subset(dictionary, col_names):
return {key: dictionary[key] for key in col_names}
def convert_image(image_path,output,format):
print(subprocess.check_output(f'cmd /k "C:/Users/Sam/Desktop/Planetary_Data_Project/Programs-code/transform-1.11.1/bin/transform.bat -f {format} -o {output} -t {image_path}"'))