-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathEDAPCalibration.py
More file actions
384 lines (317 loc) · 20.7 KB
/
Copy pathEDAPCalibration.py
File metadata and controls
384 lines (317 loc) · 20.7 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from __future__ import annotations
import tkinter as tk
import webbrowser
from tkinter import ttk, messagebox
import json
import os
from Screen_Regions import Quad, load_default_calib_data, MyRegion
def str_to_float(input_str: str) -> float:
try:
return float(input_str)
except ValueError:
return 0.0 # Assign a default value on error
class Calibration:
def __init__(self, ed_ap, cb):
self.ap = ed_ap
self.ap_ckb = cb
self.frame = None
self.ocr_calibration_data: dict[str, MyRegion] = {}
self.subregion_keys = None
def create_calibration_tab(self, tab):
self.ocr_calibration_data = load_default_calib_data()
tab.columnconfigure(0, weight=1)
# Region Calibration
blk_region_cal = ttk.LabelFrame(tab, text="Region Calibration")
blk_region_cal.grid(row=0, column=0, padx=10, pady=5, sticky="NSEW")
blk_region_cal.columnconfigure(1, weight=1)
ttk.Label(blk_region_cal, text="Region:").grid(row=0, column=0, padx=5, pady=5, sticky=tk.W)
region_keys = sorted([key for key, value in self.ocr_calibration_data.items() if isinstance(value,
dict) and 'rect' in value and 'compass' not in key and 'target' not in key and 'subregion' not in key])
self.calibration_region_var = tk.StringVar()
self.calibration_region_combo = ttk.Combobox(blk_region_cal, textvariable=self.calibration_region_var,
values=region_keys)
self.calibration_region_combo.grid(row=0, column=1, padx=5, pady=5, sticky="EW")
self.calibration_region_combo.bind("<<ComboboxSelected>>", self.on_region_select)
ttk.Label(blk_region_cal, text="Procedure:").grid(row=1, column=0, padx=5, pady=5, sticky="NW")
self.calibration_rect_text_var = tk.StringVar()
ttk.Label(blk_region_cal, textvariable=self.calibration_rect_text_var).grid(row=1, column=1, padx=5, pady=5,
sticky=tk.W)
ttk.Label(blk_region_cal, text="Rect:").grid(row=2, column=0, padx=5, pady=5, sticky=tk.W)
self.calibration_rect_label_var = tk.StringVar()
ttk.Label(blk_region_cal, textvariable=self.calibration_rect_label_var).grid(row=2, column=1, padx=5, pady=5,
sticky=tk.W)
ttk.Label(blk_region_cal,
text="Manually change the region below and save.\nHint: You can also use your keyboard up and down arrow keys.").grid(
row=3, column=0, columnspan=2, padx=5, pady=5, sticky=tk.W)
self.calibration_rect_left_var = tk.StringVar()
lbl_calibration_rect_left = ttk.Label(blk_region_cal, text='Left:')
lbl_calibration_rect_left.grid(row=4, column=0, padx=5, pady=5, sticky=tk.W)
spn_calibration_rect_left = ttk.Spinbox(blk_region_cal, textvariable=self.calibration_rect_left_var, width=10,
from_=0, to=1, increment=0.001, justify=tk.RIGHT,
command=self.on_region_size_change)
spn_calibration_rect_left.grid(row=4, column=1, padx=5, pady=5, sticky=tk.W)
self.calibration_rect_top_var = tk.StringVar()
lbl_calibration_rect_top = ttk.Label(blk_region_cal, text='Top:')
lbl_calibration_rect_top.grid(row=5, column=0, padx=5, pady=5, sticky=tk.W)
spn_calibration_rect_top = ttk.Spinbox(blk_region_cal, textvariable=self.calibration_rect_top_var, width=10,
from_=0, to=1, increment=0.001, justify=tk.RIGHT,
command=self.on_region_size_change)
spn_calibration_rect_top.grid(row=5, column=1, padx=5, pady=5, sticky=tk.W)
self.calibration_rect_right_var = tk.StringVar()
lbl_calibration_rect_right = ttk.Label(blk_region_cal, text='Right:')
lbl_calibration_rect_right.grid(row=6, column=0, padx=5, pady=5, sticky=tk.W)
spn_calibration_rect_right = ttk.Spinbox(blk_region_cal, textvariable=self.calibration_rect_right_var, width=10,
from_=0, to=1, increment=0.001, justify=tk.RIGHT,
command=self.on_region_size_change)
spn_calibration_rect_right.grid(row=6, column=1, padx=5, pady=5, sticky=tk.W)
self.calibration_rect_bottom_var = tk.StringVar()
lbl_calibration_rect_bottom = ttk.Label(blk_region_cal, text='Bottom:')
lbl_calibration_rect_bottom.grid(row=7, column=0, padx=5, pady=5, sticky=tk.W)
spn_calibration_rect_bottom = ttk.Spinbox(blk_region_cal, textvariable=self.calibration_rect_bottom_var,
width=10, from_=0, to=1, increment=0.001, justify=tk.RIGHT,
command=self.on_region_size_change)
spn_calibration_rect_bottom.grid(row=7, column=1, padx=5, pady=5, sticky=tk.W)
r = 9
# Region Calibration
blk_subregion_cal = ttk.LabelFrame(blk_region_cal, text="Sub-Region Calibration")
blk_subregion_cal.grid(row=r, column=0, columnspan=2, padx=10, pady=5, sticky="NSEW")
blk_subregion_cal.columnconfigure(1, weight=1)
# SUB-REGION
ttk.Label(blk_subregion_cal, text="Sub-region:").grid(row=r, column=0, padx=5, pady=5, sticky=tk.W)
self.calibration_subregion_var = tk.StringVar()
self.calibration_subregion_combo = ttk.Combobox(blk_subregion_cal, textvariable=self.calibration_subregion_var,
values=self.subregion_keys)
self.calibration_subregion_combo.grid(row=r, column=1, padx=5, pady=5, sticky="EW")
self.calibration_subregion_combo.bind("<<ComboboxSelected>>", self.on_subregion_select)
r += 1
ttk.Label(blk_subregion_cal, text="Procedure:").grid(row=r, column=0, padx=5, pady=5, sticky="NW")
self.calibration_subrect_text_var = tk.StringVar()
ttk.Label(blk_subregion_cal, textvariable=self.calibration_subrect_text_var).grid(row=r, column=1, padx=5, pady=5, sticky=tk.W)
r += 1
self.calibration_subrect_left_var = tk.StringVar()
lbl_calibration_subrect_left = ttk.Label(blk_subregion_cal, text='Left:')
lbl_calibration_subrect_left.grid(row=r, column=0, padx=5, pady=5, sticky=tk.W)
spn_calibration_subrect_left = ttk.Spinbox(blk_subregion_cal, textvariable=self.calibration_subrect_left_var,
width=10,
from_=0, to=1, increment=0.001, justify=tk.RIGHT,
command=self.on_subregion_size_change)
spn_calibration_subrect_left.grid(row=r, column=1, padx=5, pady=5, sticky=tk.W)
r += 1
self.calibration_subrect_top_var = tk.StringVar()
lbl_calibration_subrect_top = ttk.Label(blk_subregion_cal, text='Top:')
lbl_calibration_subrect_top.grid(row=r, column=0, padx=5, pady=5, sticky=tk.W)
spn_calibration_subrect_top = ttk.Spinbox(blk_subregion_cal, textvariable=self.calibration_subrect_top_var,
width=10,
from_=0, to=1, increment=0.001, justify=tk.RIGHT,
command=self.on_subregion_size_change)
spn_calibration_subrect_top.grid(row=r, column=1, padx=5, pady=5, sticky=tk.W)
r += 1
self.calibration_subrect_right_var = tk.StringVar()
lbl_calibration_subrect_right = ttk.Label(blk_subregion_cal, text='Right:')
lbl_calibration_subrect_right.grid(row=r, column=0, padx=5, pady=5, sticky=tk.W)
spn_calibration_subrect_right = ttk.Spinbox(blk_subregion_cal, textvariable=self.calibration_subrect_right_var,
width=10,
from_=0, to=1, increment=0.001, justify=tk.RIGHT,
command=self.on_subregion_size_change)
spn_calibration_subrect_right.grid(row=r, column=1, padx=5, pady=5, sticky=tk.W)
r += 1
self.calibration_subrect_bottom_var = tk.StringVar()
lbl_calibration_subrect_bottom = ttk.Label(blk_subregion_cal, text='Bottom:')
lbl_calibration_subrect_bottom.grid(row=r, column=0, padx=5, pady=5, sticky=tk.W)
spn_calibration_subrect_bottom = ttk.Spinbox(blk_subregion_cal, textvariable=self.calibration_subrect_bottom_var,
width=10, from_=0, to=1, increment=0.001, justify=tk.RIGHT,
command=self.on_subregion_size_change)
spn_calibration_subrect_bottom.grid(row=r, column=1, padx=5, pady=5, sticky=tk.W)
r += 1
# Button Frame
button_frame = ttk.Frame(blk_region_cal)
button_frame.grid(row=r, column=0, padx=10, pady=10, sticky=tk.W)
ttk.Button(button_frame, text="Calibrate Region help online", command=self.calibrate_region_help).grid(row=r, column=0, padx=5, pady=10, sticky=tk.W)
ttk.Button(button_frame, text="Save All Calibrations", command=self.save_ocr_calibration_data, style="Accent.TButton").grid(row=r, column=1, padx=5, pady=10, sticky=tk.W)
ttk.Button(button_frame, text="Reset All to Default", command=self.reset_all_calibrations).grid(row=r, column=2, padx=5, pady=10, sticky=tk.W)
r += 1
# Compass and Target Calibrations
blk_other_cal = ttk.LabelFrame(tab, text="Target Calibration")
blk_other_cal.grid(row=r, column=0, padx=10, pady=5, sticky="NSEW")
btn_calibrate_target = ttk.Button(blk_other_cal, text="Calibrate Target", command=self.calibrate_callback)
btn_calibrate_target.grid(row=1, padx=10, pady=5, sticky="W")
lbl_calibrate_target = ttk.Label(blk_other_cal, wraplength=500, text='Performs target calibration for your '
'screen. Perform when the target is '
'visible center screen.')
lbl_calibrate_target.grid(row=1, column=1, padx=10, pady=5, sticky=tk.W)
def save_ocr_calibration_data(self):
# q = Quad.from_rect(self.ocr_calibration_data['EDCodex.full_panel']['rect'])
# fx = 0.95
# fy = 0.96
# q.scale(fx, fy)
# self.ocr_calibration_data['EDStationServicesInShip.station_services']['rect'] = q.to_rect_list(round_dp=4)
q = Quad.from_rect(self.ocr_calibration_data['EDCodex.full_panel']['rect'])
q.scale(fx=1.025, fy=1.0)
self.ocr_calibration_data['EDStationServicesInShip.commodities_market']['rect'] = q.to_rect_list(round_dp=4)
q = Quad.from_rect(self.ocr_calibration_data['EDCodex.full_panel']['rect'])
q.scale(fx=1.05, fy=1.08)
self.ocr_calibration_data['EDSystemMap.full_panel']['rect'] = q.to_rect_list(round_dp=4)
q = Quad.from_rect(self.ocr_calibration_data['EDCodex.full_panel']['rect'])
q.scale(fx=1.05, fy=1.08)
self.ocr_calibration_data['EDGalaxyMap.full_panel']['rect'] = q.to_rect_list(round_dp=4)
q = Quad.from_rect(self.ocr_calibration_data['EDCodex.full_panel']['rect'])
q.scale(fx=1.0, fy=1.0)
self.ocr_calibration_data['EDFSS.full_panel']['rect'] = q.to_rect_list(round_dp=4)
# q = Quad.from_rect(self.ocr_calibration_data['EDStationServicesInShip.station_services']['rect'])
# q.crop(0.0, 0.0, 0.25, 0.25)
# self.ocr_calibration_data['EDStationServicesInShip.connected_to']['rect'] = q.to_rect_list(round_dp=4)
calibration_file = 'configs/ocr_calibration.json'
with open(calibration_file, 'w') as f:
json.dump(self.ocr_calibration_data, f, indent=4)
# self.log_msg("OCR calibration data saved.")
self.ap_ckb('log', f"OCR calibration data saved.")
# messagebox.showinfo("Saved", "OCR calibration data saved.\nPlease restart the application for changes to take effect.")
def reset_all_calibrations(self):
if messagebox.askyesno("Reset All Calibrations",
"Are you sure you want to reset all OCR calibrations to their default values? This cannot be undone."):
calibration_file = 'configs/ocr_calibration.json'
if os.path.exists(calibration_file):
os.remove(calibration_file)
# self.log_msg("Removed existing ocr_calibration.json.")
self.ap_ckb('log', f"Removed existing ocr_calibration.json.")
# This will recreate the file with defaults
self.ocr_calibration_data = load_default_calib_data()
# --- Repopulate UI ---
# Clear current selections
self.calibration_region_var.set('')
# self.calibration_size_var.set('')
self.calibration_rect_label_var.set('')
# self.calibration_rect_left_var.set('')
# self.calibration_size_label_var.set('')
# Repopulate region dropdown
region_keys = sorted([key for key in self.ocr_calibration_data.keys() if
'.size.' not in key and 'compass' not in key and 'target' not in key])
self.calibration_region_combo['values'] = region_keys
# Repopulate size dropdown
# size_keys = sorted([key for key in self.ocr_calibration_data.keys() if '.size.' in key])
# self.calibration_size_combo['values'] = size_keys
# self.log_msg("All OCR calibrations have been reset to default.")
self.ap_ckb('log', f"All OCR calibrations have been reset to default.")
messagebox.showinfo("Reset Complete",
"All calibrations have been reset to default. Please restart the application for all changes to take effect.")
def on_region_select(self, event):
selected_region = self.calibration_region_var.get()
if selected_region in self.ocr_calibration_data.keys():
reg = self.ocr_calibration_data[selected_region]
rect = reg['rect']
self.calibration_rect_label_var.set(f"[{rect[0]:.4f}, {rect[1]:.4f}, {rect[2]:.4f}, {rect[3]:.4f}]")
self.calibration_rect_text_var.set(f"{self.ocr_calibration_data[selected_region].get('text', '')}")
self.calibration_rect_left_var.set(str(rect[0]))
self.calibration_rect_top_var.set(str(rect[1]))
self.calibration_rect_right_var.set(str(rect[2]))
self.calibration_rect_bottom_var.set(str(rect[3]))
self.subregion_keys = [key for key, value in self.ocr_calibration_data.items() if
isinstance(value, dict) and key.startswith(selected_region) and 'subregion' in key]
self.calibration_subregion_var.set('')
self.calibration_subregion_combo['values'] = self.subregion_keys
if len(self.subregion_keys) > 0:
self.calibration_subregion_var.set(self.subregion_keys[0])
else:
# No subregion
self.ap.overlay.overlay_remove_quad('subregion select')
self.ap.overlay.overlay_paint()
# if 'regions' in reg:
# self.sub_region_keys = reg['regions'].keys
# self.sub_region_keys = [key for key, value in reg['regions'] if isinstance(value, dict) and 'regions' in value]
reg_f = Quad.from_rect(rect)
self.ap.overlay.overlay_quad_pct('region select', reg_f, (0, 255, 0), 2, 15)
self.ap.overlay.overlay_paint()
# Trigger the subregion select to highlight the default subregion
self.on_subregion_select(None)
def on_subregion_select(self, event):
selected_region = self.calibration_region_var.get()
selected_subregion = self.calibration_subregion_var.get()
if selected_region in self.ocr_calibration_data.keys() and selected_subregion in self.ocr_calibration_data.keys():
reg = self.ocr_calibration_data[selected_region]
rect = reg['rect']
sub_reg = self.ocr_calibration_data[selected_subregion]
sub_rect = sub_reg['rect']
self.calibration_subrect_text_var.set(f"{self.ocr_calibration_data[selected_subregion].get('text', '')}")
self.calibration_subrect_left_var.set(str(sub_rect[0]))
self.calibration_subrect_top_var.set(str(sub_rect[1]))
self.calibration_subrect_right_var.set(str(sub_rect[2]))
self.calibration_subrect_bottom_var.set(str(sub_rect[3]))
scaled_rect = Quad.from_rect(reg['rect'])
sub_reg_quad = Quad.from_rect(sub_reg['rect'])
scaled_rect.crop(sub_reg_quad)
self.ap.overlay.overlay_quad_pct('subregion select', scaled_rect, (0, 255, 0), 2, 15)
self.ap.overlay.overlay_paint()
else:
self.calibration_subrect_text_var.set('')
self.calibration_subrect_left_var.set('0.0')
self.calibration_subrect_top_var.set('0.0')
self.calibration_subrect_right_var.set('0.0')
self.calibration_subrect_bottom_var.set('0.0')
def on_region_size_change(self):
# Check if variables are valid
l_str = self.calibration_rect_left_var.get()
t_str = self.calibration_rect_top_var.get()
r_str = self.calibration_rect_right_var.get()
b_str = self.calibration_rect_bottom_var.get()
# Check if any are empty
if l_str == '' or r_str == '' or t_str == '' or b_str == '':
return
selected_region = self.calibration_region_var.get()
if selected_region in self.ocr_calibration_data:
rect = self.ocr_calibration_data[selected_region]['rect']
rect[0] = str_to_float(l_str)
rect[1] = str_to_float(t_str)
rect[2] = str_to_float(r_str)
rect[3] = str_to_float(b_str)
self.calibration_rect_label_var.set(f"[{rect[0]:.4f}, {rect[1]:.4f}, {rect[2]:.4f}, {rect[3]:.4f}]")
self.calibration_rect_text_var.set(f"{self.ocr_calibration_data[selected_region].get('text', '')}")
reg_f = Quad.from_rect(rect)
self.ap.overlay.overlay_quad_pct('region select', reg_f, (0, 255, 0), 2, 15)
self.ap.overlay.overlay_paint()
def on_subregion_size_change(self):
# Check if variables are valid
l_str = self.calibration_rect_left_var.get()
t_str = self.calibration_rect_top_var.get()
r_str = self.calibration_rect_right_var.get()
b_str = self.calibration_rect_bottom_var.get()
sub_l_str = self.calibration_subrect_left_var.get()
sub_t_str = self.calibration_subrect_top_var.get()
sub_r_str = self.calibration_subrect_right_var.get()
sub_b_str = self.calibration_subrect_bottom_var.get()
# Check if any are empty
if l_str == '' or r_str == '' or t_str == '' or b_str == '':
return
selected_region = self.calibration_region_var.get()
selected_subregion = self.calibration_subregion_var.get()
if selected_region in self.ocr_calibration_data.keys() and selected_subregion in self.ocr_calibration_data.keys():
reg = self.ocr_calibration_data[selected_region]
rect = self.ocr_calibration_data[selected_region]['rect']
rect[0] = str_to_float(l_str)
rect[1] = str_to_float(t_str)
rect[2] = str_to_float(r_str)
rect[3] = str_to_float(b_str)
sub_reg = self.ocr_calibration_data[selected_subregion]
sub_rect = self.ocr_calibration_data[selected_subregion]['rect']
sub_rect[0] = str_to_float(sub_l_str)
sub_rect[1] = str_to_float(sub_t_str)
sub_rect[2] = str_to_float(sub_r_str)
sub_rect[3] = str_to_float(sub_b_str)
scaled_rect = Quad.from_rect(reg['rect'])
sub_reg_quad = Quad.from_rect(sub_reg['rect'])
scaled_rect.crop(sub_reg_quad)
self.ap.overlay.overlay_quad_pct('subregion select', scaled_rect, (0, 255, 0), 2, 15)
self.ap.overlay.overlay_paint()
@staticmethod
def calibrate_region_help():
webbrowser.open_new("https://github.com/SumZer0-git/EDAPGui/blob/main/docs/Calibration.md")
def calibrate_callback(self):
self.ap.calibrate_target()
def dummy_cb(msg, body=None):
pass
def main():
# from ED_AP import EDAutopilot
#ed_ap = EDAutopilot(cb=None)
ce = Calibration(None, cb=dummy_cb) # False = Horizons
# ce.create_calibration_tab()
if __name__ == "__main__":
main()