-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_offsetter.py
584 lines (470 loc) · 19.4 KB
/
path_offsetter.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# -*- coding: utf-8 -*-
from inspect import getfile
from re import T
import numpy as np
import json
import tkinter
from tkinter import *
from tkinter.filedialog import askopenfilename
edit_or_combine_test = 1
cam_to_edit = None
consecutive = None
x_offset = None
y_offset = None
z_offset = None
edit_pan_tilt = None
pan = None
tilt = None
name = None
series_num =None
start = None
end = None
number_of_paths = None
filename = None
def set_value(value):
global edit_or_combine_test
edit_or_combine_test = value
root.destroy()
def set_value_cams():
global cam_to_edit
cam_to_edit = cam.get()
root.destroy()
def set_consecutive(value):
global consecutive
consecutive = value
root.destroy()
def getXYZ():
global x_offset ,y_offset, z_offset
x_offset = float(x.get())
y_offset = float(y.get())
z_offset = float(z.get())
root.destroy()
def pan_tilt_ask(value):
global edit_pan_tilt
edit_pan_tilt = value
root.destroy()
def getPT():
global pan, tilt
pan = float(p.get())
tilt = float(t.get())
root.destroy()
def getFilename():
global name
name = file.get()
root.destroy()
def getSections():
global series_num
series_num = int(sections.get())
root.destroy()
def getStartEnd():
global start, end
start = int(st.get())
end = int(en.get())
root.destroy()
def getPaths():
global number_of_paths
number_of_paths = int(path_num.get())
root.destroy()
def Close():
root.destroy()
root = Tk()
root.eval('tk::PlaceWindow . center')
f = Frame(root)
Button(f, text='Edit a path',command=lambda *args: set_value("a")).pack(side = LEFT, padx = 10, pady = 5)
Button(f, text='Combine paths',command=lambda *args: set_value("b")).pack(side = LEFT, padx= 10, pady = 5)
Button(f, text='Invert a path',command=lambda *args: set_value("c")).pack(side = LEFT, padx= 10, pady = 5)
label = Label(root, text = "Choose what you would like to do!")
label.pack()
f.pack()
root.mainloop()
#EDITING PATHS
if edit_or_combine_test == "a":
root = Tk()
root.withdraw()
filename = askopenfilename()
root.destroy()
#Check valid .cproj file
while True:
if filename.endswith('.cproj'):
break
else:
print("Sorry, that file is not a COPIS path, please choose another file\n")
root = Tk()
root.withdraw()
filename = askopenfilename()
root.destroy()
# reading the data from the file
with open(filename) as f:
path_file = f.read()
# reconstructing the data as a dictionary
path = json.loads(path_file)
#get the length of the imaging path (in pose sets)
poses = (len(path['imaging_path']))
root = Tk()
root.attributes('-topmost',True)
f=Frame(root)
label = Label(root, text = "If you would like to edit only a single camera \n enter its index, if not leave blank.").pack(side = TOP, pady = 5)
cam = Entry(root)
cam.pack(pady = 5)
Button(root, text = "Continue", command=lambda *args: set_value_cams()).pack(side = BOTTOM, pady = 5)
root.eval('tk::PlaceWindow . center')
root.mainloop()
#cam_to_edit = input("If you would like to edit only a single camera enter its index, if not enter 'n'.\n")
#check the camera index or n value from input
while True:
if cam_to_edit == "0" or cam_to_edit == "1" or cam_to_edit == "2" or cam_to_edit == "3" or cam_to_edit == "4" or cam_to_edit == "5" or cam_to_edit == "":
break
else:
root = Tk()
f=Frame(root)
label = Label(root, text = "Sorry, your input was not valid, enter a camera index (0-5) or n").pack(side = TOP, pady = 20)
cam = Entry(root)
cam.pack(pady = 20)
Button(root, text = "Continue", command=lambda *args: set_value_cams()).pack(side = BOTTOM, pady = 20)
root.eval('tk::PlaceWindow . center')
root.mainloop()
if cam_to_edit != "":
root = Tk()
f= Frame(root)
label = Label(root, text = "Do you wish to edit...")
Button(f, text = "All poses for this camera?", command=lambda *args: set_consecutive("a")).pack(side = LEFT, padx = 10, pady =10)
Button(f, text = "Certain sections of poses?", command=lambda *args: set_consecutive("b")).pack(side = RIGHT, padx = 10, pady = 10)
label.pack(pady = 10)
f.pack()
root.eval('tk::PlaceWindow . center')
root.mainloop()
#editing all for this cam
if consecutive == "a":
cam_to_edit = int(cam_to_edit)
root = Tk()
Label(root, text = "Enter your X, Y, and Z Offsets and press Submit").grid(row = 0, sticky= N)
Label(root, text = "X Offset").grid(row = 1, sticky = W)
Label(root, text = "Y Offset").grid(row = 2, sticky = W)
Label(root, text = "Z Offset").grid(row = 3, sticky = W)
x=Entry(root)
y=Entry(root)
z=Entry(root)
x.grid(row = 1, column = 0)
y.grid(row = 2, column = 0)
z.grid(row = 3, column = 0)
Button(root, text = "Submit", command=lambda *args: getXYZ()).grid(row = 5, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
root = Tk()
f= Frame(root)
label = Label(root, text = "Do you wish to fix pan and tilt?")
Button(f, text = "Yes", command=lambda *args: pan_tilt_ask("y")).pack(side = LEFT, padx = 10, pady =10)
Button(f, text = "No", command=lambda *args: pan_tilt_ask("n")).pack(side = RIGHT, padx = 10, pady = 10)
label.pack(pady = 10)
f.pack()
root.eval('tk::PlaceWindow . center')
root.mainloop()
#edit_pan_tilt = input("Would you like to fix pan and tilt? y/n\n")
if edit_pan_tilt == "y":
root = Tk()
Label(root, text = "Enter your pan and tilt values and press Submit").grid(row = 0, sticky= N)
Label(root, text = "Pan").grid(row = 1, sticky = W)
Label(root, text = "Tilt").grid(row = 2, sticky = W)
p=Entry(root)
t=Entry(root)
p.grid(row = 1, column = 0)
t.grid(row = 2, column = 0)
Button(root, text = "Submit", command=lambda *args: getPT()).grid(row = 5, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
for i in range(0,poses):
cams = len(path['imaging_path'][i])
for j in range(0, cams):
if path['imaging_path'][i][j][0]['device'] == cam_to_edit:
new_x = float(path['imaging_path'][i][j][0]['args'][0][1]) + x_offset
new_y = float(path['imaging_path'][i][j][0]['args'][1][1]) + y_offset
new_z = float(path['imaging_path'][i][j][0]['args'][2][1]) + z_offset
path['imaging_path'][i][j][0]['args'][0][1] = str(new_x)
path['imaging_path'][i][j][0]['args'][1][1] = str(new_y)
path['imaging_path'][i][j][0]['args'][2][1] = str(new_z)
if edit_pan_tilt == "y":
path['imaging_path'][i][j][0]['args'][3][1] = str(pan)
path['imaging_path'][i][j][0]['args'][4][1] = str(tilt)
#editing sections
else:
root = Tk()
Label(root, text = "How many sections would you like to adjust?").grid(row = 0, sticky= N)
Label(root, text = "Number of Sections").grid(row = 1, sticky = W)
sections=Entry(root)
sections.grid(row = 1, column = 0)
Button(root, text = "Submit", command=lambda *args: getSections()).grid(row = 2, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
series_dct = {}
for i in range(0,series_num):
series_dct[i] = []
root = Tk()
Label(root, text = ("Enter the starting and ending poses for Section %s" % str(i+1))).grid(row = 0, sticky= N)
Label(root, text = "Start").grid(row = 1, sticky = W)
Label(root, text = "End").grid(row = 2, sticky = W)
st=Entry(root)
en=Entry(root)
st.grid(row = 1, column = 0)
en.grid(row = 2, column = 0)
Button(root, text = "Submit", command=lambda *args: getStartEnd()).grid(row = 5, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
for p in range(start, end+1):
series_dct[i].append(p)
root = Tk()
Label(root, text = "Enter your X, Y, and Z Offsets and press Submit").grid(row = 0, sticky= N)
Label(root, text = "X Offset").grid(row = 1, sticky = W)
Label(root, text = "Y Offset").grid(row = 2, sticky = W)
Label(root, text = "Z Offset").grid(row = 3, sticky = W)
x=Entry(root)
y=Entry(root)
z=Entry(root)
x.grid(row = 1, column = 0)
y.grid(row = 2, column = 0)
z.grid(row = 3, column = 0)
Button(root, text = "Submit", command=lambda *args: getXYZ()).grid(row = 5, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
root = Tk()
f= Frame(root)
label = Label(root, text = "Do you wish to fix pan and tilt?")
Button(f, text = "Yes", command=lambda *args: pan_tilt_ask("y")).pack(side = LEFT, padx = 10, pady =10)
Button(f, text = "No", command=lambda *args: pan_tilt_ask("n")).pack(side = RIGHT, padx = 10, pady = 10)
label.pack(pady = 10)
f.pack()
root.eval('tk::PlaceWindow . center')
root.mainloop()
if edit_pan_tilt == "y":
root = Tk()
Label(root, text = "Enter your pan and tilt values and press Submit").grid(row = 0, sticky= N)
Label(root, text = "Pan").grid(row = 1, sticky = W)
Label(root, text = "Tilt").grid(row = 2, sticky = W)
p=Entry(root)
t=Entry(root)
p.grid(row = 1, column = 0)
t.grid(row = 2, column = 0)
Button(root, text = "Submit", command=lambda *args: getPT()).grid(row = 5, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
for i in range((len(series_dct))):
for j in range(len(series_dct[i])):
cams = len(path['imaging_path'][series_dct[i][j]])
for k in range(0, cams):
if path['imaging_path'][series_dct[i][j]][k][0]['device'] == cam_to_edit:
new_x = float(path['imaging_path'][series_dct[i][j]][k][0]['args'][0][1]) + x_offset
new_y = float(path['imaging_path'][series_dct[i][j]][k][0]['args'][1][1]) + y_offset
new_z = float(path['imaging_path'][series_dct[i][j]][k][0]['args'][2][1]) + z_offset
path['imaging_path'][series_dct[i][j]][k][0]['args'][0][1] = str(new_x)
path['imaging_path'][series_dct[i][j]][k][0]['args'][1][1] = str(new_y)
path['imaging_path'][series_dct[i][j]][k][0]['args'][2][1] = str(new_z)
if edit_pan_tilt == "y":
path['imaging_path'][series_dct[i][j]][k][0]['args'][3][1] = str(pan)
path['imaging_path'][series_dct[i][j]][k][0]['args'][4][1] = str(tilt)
#editing all cameras
else:
root = Tk()
Label(root, text = "Enter your X, Y, and Z Offsets and press Submit").grid(row = 0, sticky= N)
Label(root, text = "X Offset").grid(row = 1, sticky = W)
Label(root, text = "Y Offset").grid(row = 2, sticky = W)
Label(root, text = "Z Offset").grid(row = 3, sticky = W)
x=Entry(root)
y=Entry(root)
z=Entry(root)
x.grid(row = 1, column = 0)
y.grid(row = 2, column = 0)
z.grid(row = 3, column = 0)
Button(root, text = "Submit", command=lambda *args: getXYZ()).grid(row = 5, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
root = Tk()
f= Frame(root)
label = Label(root, text = "Would you like to fix pan and tilt?")
Button(f, text = "Yes", command=lambda *args: pan_tilt_ask("y")).pack(side = LEFT, padx = 10, pady =10)
Button(f, text = "No", command=lambda *args: pan_tilt_ask("n")).pack(side = RIGHT, padx = 10, pady = 10)
label.pack(pady = 10)
f.pack()
root.eval('tk::PlaceWindow . center')
root.mainloop()
if edit_pan_tilt == "y":
root = Tk()
Label(root, text = "Enter your pan and tilt values and press Submit").grid(row = 0, sticky= N)
Label(root, text = "Pan").grid(row = 1, sticky = W)
Label(root, text = "Tilt").grid(row = 2, sticky = W)
p=Entry(root)
t=Entry(root)
p.grid(row = 1, column = 0)
t.grid(row = 2, column = 0)
Button(root, text = "Submit", command=lambda *args: getPT()).grid(row = 5, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
for i in range(0,poses):
cams = len(path['imaging_path'][i])
for j in range(0, cams):
new_x = float(path['imaging_path'][i][j][0]['args'][0][1]) + x_offset
new_y = float(path['imaging_path'][i][j][0]['args'][1][1]) + y_offset
new_z = float(path['imaging_path'][i][j][0]['args'][2][1]) + z_offset
path['imaging_path'][i][j][0]['args'][0][1] = str(new_x)
path['imaging_path'][i][j][0]['args'][1][1] = str(new_y)
path['imaging_path'][i][j][0]['args'][2][1] = str(new_z)
if edit_pan_tilt == "y":
path['imaging_path'][i][j][0]['args'][3][1] = str(pan)
path['imaging_path'][i][j][0]['args'][4][1] = str(tilt)
#getting filename settings
root = Tk()
Label(root, text = "Enter 'NewFilename' or leave blank to generate automatically").grid(row = 0, sticky= N)
Label(root, text = "Filename").grid(row = 1, sticky = W)
file=Entry(root)
file.grid(row = 1, column = 0)
Button(root, text = "Submit and Save", command=lambda *args: getFilename()).grid(row = 2, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
#automatic file naming
if name == "":
new_filename = filename[:len(filename)-6] + "_edited" + ".cproj"
print(new_filename)
with open(new_filename, 'w') as convert_file:
convert_file.write(json.dumps(path))
#setting user inputed filename
else:
new_filename = filename.rsplit("/", 1)[0] + "/" + name + ".cproj"
with open(new_filename, 'w') as convert_file:
convert_file.write(json.dumps(path))
#COMBINING PATHS
elif edit_or_combine_test == "b":
root = Tk()
root.attributes('-topmost',True)
f=Frame(root)
label = Label(root, text = "How many paths would you like to combine? All settings besides \n poses (proxies, profiles etc.) will be taken from the first path chosen").pack(side = TOP, pady = 5)
path_num = Entry(root)
path_num.pack(pady = 5)
Button(root, text = "Submit", command=lambda *args: getPaths()).pack(side = BOTTOM, pady = 5)
root.eval('tk::PlaceWindow . center')
root.mainloop()
# number_of_paths = int(input("How many paths would you like to combine? All settings besides camera poses (proxies, device profiles, etc.) will be taken from the first path chosen\n"))
paths = {}
combined_path = {"imaging_path" : [] , "profile": {}, "proxies":{}}
for i in range(number_of_paths):
paths[i] = {}
root = Tk()
root.withdraw()
filename = askopenfilename()
root.destroy()
while True:
if filename.endswith('.cproj'):
break
else:
root = Tk()
root.attributes('-topmost',True)
label = Label(text = "Sorry, that file is not a COPIS path, please choose another file").pack(side = TOP, pady = 10)
Button(root, text = "Try again", command=Close).pack(side = BOTTOM, pady = 10)
root.eval('tk::PlaceWindow . center')
root.mainloop()
root = Tk()
root.withdraw()
filename = askopenfilename()
root.destroy()
if i == 0:
path1name = filename
# reading the data from the file
with open(filename) as f:
path = f.read()
# reconstructing the data as a dictionary
path_i= json.loads(path)
paths[i] = path_i
combined_path['profile'] = paths[0]['profile']
combined_path['proxies'] = paths[0]['proxies']
#getting filename settings
root = Tk()
root.eval('tk::PlaceWindow . center')
root.attributes('-topmost',True)
Label(root, text = "Enter 'NewFilename' or leave blank to generate automatically").grid(row = 0, sticky= N)
Label(root, text = "Filename").grid(row = 1, sticky = W)
file=Entry(root)
file.grid(row = 1, column = 0)
Button(root, text = "Submit and Save", command=lambda *args: getFilename()).grid(row = 2, column = 0)
root.mainloop()
for i in range(len(paths)):
for j in range(len(paths[i]['imaging_path'])):
combined_path['imaging_path'].append(paths[i]['imaging_path'][j])
if name == "":
new_filename = path1name[:len(path1name)-6] + "_combined" + ".cproj"
print(new_filename)
with open(new_filename, 'w') as convert_file:
convert_file.write(json.dumps(combined_path))
else:
new_filename = path1name.rsplit("/", 1)[0] + "/" + name + ".cproj"
print(new_filename)
with open(new_filename, 'w') as convert_file:
convert_file.write(json.dumps(combined_path))
print(new_filename)
else:
root = Tk()
root.withdraw()
filename = askopenfilename()
root.destroy()
#Check valid .cproj file
while True:
if filename.endswith('.cproj'):
break
else:
print("Sorry, that file is not a COPIS path, please choose another file\n")
root = Tk()
root.withdraw()
filename = askopenfilename()
root.destroy()
# reading the data from the file
with open(filename) as f:
path_file = f.read()
# reconstructing the data as a dictionary
path = json.loads(path_file)
#get the length of the imaging path (in pose sets)
poses = (len(path['imaging_path']))
inverted_path = {"imaging_path" : [] , "profile": {}, "proxies":{}}
inverted_path['profile'] = path['profile']
inverted_path['proxies'] = path['proxies']
#getting filename settings
root = Tk()
Label(root, text = "Enter 'NewFilename' or leave blank to generate automatically").grid(row = 0, sticky= N)
Label(root, text = "Filename").grid(row = 1, sticky = W)
file=Entry(root)
file.grid(row = 1, column = 0)
Button(root, text = "Submit and Save", command=lambda *args: getFilename()).grid(row = 2, column = 0)
root.eval('tk::PlaceWindow . center')
root.mainloop()
#inverting the path
for i in range(0,poses):
cams = len(path['imaging_path'][i])
for j in range(0,cams):
inverted_path['imaging_path'].append(path['imaging_path'][i][j])
if path['imaging_path'][i][j][0]['device'] == 0:
path['imaging_path'][i][j][0]['device'] = 3
path['imaging_path'][i][j][1][0]['device'] = 3
elif path['imaging_path'][i][j][0]['device'] == 1:
path['imaging_path'][i][j][0]['device'] = 4
path['imaging_path'][i][j][1][0]['device'] = 4
elif path['imaging_path'][i][j][0]['device'] == 2:
path['imaging_path'][i][j][0]['device'] = 5
path['imaging_path'][i][j][1][0]['device'] = 5
elif path['imaging_path'][i][j][0]['device'] == 3:
path['imaging_path'][i][j][0]['device'] = 0
path['imaging_path'][i][j][1][0]['device'] = 0
elif path['imaging_path'][i][j][0]['device'] == 4:
path['imaging_path'][i][j][0]['device'] = 1
path['imaging_path'][i][j][1][0]['device'] = 1
else:
path['imaging_path'][i][j][0]['device'] = 2
path['imaging_path'][i][j][1][0]['device'] = 2
inverted_z = -1 * float(path['imaging_path'][i][j][0]['args'][2][1])
path['imaging_path'][i][j][0]['args'][2][1] = str(inverted_z)
inverted_path['imaging_path'].append(path['imaging_path'][i][j])
#automatic file naming
if name == "":
new_filename = filename[:len(filename)-6] + "_inverted" + ".cproj"
print(new_filename)
with open(new_filename, 'w') as convert_file:
convert_file.write(json.dumps(inverted_path))
#setting user inputed filename
else:
new_filename = filename.rsplit("/", 1)[0] + "/" + name + ".cproj"
with open(new_filename, 'w') as convert_file:
convert_file.write(json.dumps(inverted_path))