This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_ui.py
515 lines (475 loc) · 21 KB
/
new_ui.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
from tkinter import *
import tkinter as tk # 使用Tkinter前需要先导入
from tkinter import filedialog, ttk
import tkinter.messagebox # 这个是消息框,对话框的关键
from PIL import ImageTk
from PIL import Image
import numpy as np
import math
import cv2
from easydict import EasyDict
import threading
import random
from xfield_test import XFieldTest
def mouse_left_button_down_event(event):
global is_mouse_left_button_down, mouse_moving_oval_index
if math.sqrt(math.pow(abs(event.x - oval_x1), 2) + math.pow(abs(event.y - oval_y1), 2)) < 5:
is_mouse_left_button_down = True
mouse_moving_oval_index = 1
elif math.sqrt(math.pow(abs(event.x - oval_x2), 2) + math.pow(abs(event.y - oval_y2), 2)) < 5:
is_mouse_left_button_down = True
mouse_moving_oval_index = 2
print("mouse down", event.x, event.y)
def mouse_left_button_release_event(event):
global is_mouse_left_button_down
is_mouse_left_button_down = False
print("mouse realease", event.x, event.y)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
def mouse_motion(event):
global oval_x1, oval_y1, oval_x2, oval_y2, light_value, view_value, time_value, result_photo
text_var = "鼠标当前位置:x:{}, y:{}".format(str(event.x).zfill(3), str(event.y).zfill(3))
var.set(text_var)
if is_mouse_left_button_down:
if mouse_moving_oval_index == 1:
canvas.delete("oval1")
canvas.delete("light_text")
canvas.delete("view_text")
oval_x1 = event.x
oval_y1 = event.y
if oval_x1 < 440:
oval_x1 = 440
elif oval_x1 > 580:
oval_x1 = 580
if oval_y1 < 100:
oval_y1 = 100
elif oval_y1 > 240:
oval_y1 = 240
canvas.create_oval(oval_x1 - 4, oval_y1 - 4, oval_x1 + 4, oval_y1 + 4, fill="black", tag="oval1")
light_value, view_value = transformOval1()
canvas.create_text(510, 340, text='Light:' + str(light_value), tag="light_text")
canvas.create_text(510, 360, text='View:' + str(view_value), tag="view_text")
elif mouse_moving_oval_index == 2:
# 仅允许在横轴上运动
canvas.delete("oval2")
canvas.delete("time_text")
oval_x2 = event.x
if oval_x2 < 440:
oval_x2 = 440
elif oval_x2 > 580:
oval_x2 = 580
canvas.create_oval(oval_x2 - 4, oval_y2 - 4, oval_x2 + 4, oval_y2 + 4, fill="black", tag="oval2")
time_value = transformOval2()
canvas.create_text(510, 380, text='Time:' + str(time_value), tag="time_text")
if mode == ['light','view','time']:
img = x.generate3DimensionResult(light_value, view_value, time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
elif mode == ['time']:
img = x.generate1DimensionResult(time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
def transformOval1():
light_value = round(max_coordinate - (oval_y1 - 100) / 140 * max_coordinate, 2)
view_value = round((oval_x1 - 440) / 140 * max_coordinate, 2)
return light_value, view_value
def transformOval2():
time_value = round((oval_x2 - 440) * max_coordinate / 140, 2)
return time_value
def resize(event):
global oval_x1, oval_y1, oval_x2, oval_y2, time_value, light_value, view_value
global max_coordinate # 最大坐标
old_max_coordinate = max_coordinate
max_coordinate = scaleLine.get()
oval_x1 = (oval_x1 - 440) * old_max_coordinate / max_coordinate + 440
oval_y1 = 240 - (240 - oval_y1) * old_max_coordinate / max_coordinate
oval_x2 = (oval_x2 - 440) * old_max_coordinate / max_coordinate + 440
if oval_x1 > 580:
oval_x1 = 580
view_value = max_coordinate
if oval_y1 < 100:
oval_y1 = 100
light_value = max_coordinate
if oval_x2 > 580:
oval_x2 = 580
time_value = max_coordinate
canvas.delete("light_text")
canvas.delete("time_text")
canvas.delete("view_text")
canvas.delete("scale_text_x")
canvas.delete("scale_text_y")
canvas.delete("scale_text")
canvas.delete("oval1")
canvas.delete("oval2")
if mode == ["time"]:
canvas.create_text(510, 380, text='Time:' + str(time_value), tag="time_text")
canvas.create_text(585, 315, text=str(max_coordinate), tag="scale_text")
canvas.create_oval(oval_x2 - 4, oval_y2 - 4, oval_x2 + 4, oval_y2 + 4, fill="black", tag="oval2")
elif mode == ['light','view','time'] :
canvas.create_text(510, 380, text='Time:' + str(time_value), tag="time_text")
canvas.create_text(510, 340, text='Light:' + str(light_value), tag="light_text")
canvas.create_text(510, 360, text='View:' + str(view_value), tag="view_text")
canvas.create_text(585, 315, text=str(max_coordinate), tag="scale_text_x")
canvas.create_text(430, 95, text=str(max_coordinate), tag="scale_text_y")
canvas.create_text(585, 250, text=str(max_coordinate), tag="scale_text")
canvas.create_oval(oval_x1 - 4, oval_y1 - 4, oval_x1 + 4, oval_y1 + 4, fill="black", tag="oval1")
canvas.create_oval(oval_x2 - 4, oval_y2 - 4, oval_x2 + 4, oval_y2 + 4, fill="black", tag="oval2")
def cmb_select(event):
pb.start()
th = threading.Thread(target=cmb_select_main, args=(cmb.get(),))
th.start()
tk.messagebox.showinfo('进行', '开始加载数据请耐心等待')
def delete_all():
canvas.delete("time_text")
canvas.delete("light_text")
canvas.delete("view_text")
canvas.delete("oval1")
canvas.delete("oval2")
canvas.delete("2-dimension-box")
canvas.delete("1-dimension-line")
canvas.delete("scale_text_x")
canvas.delete("scale_text_y")
canvas.delete("scale_text")
canvas.delete("x_text")
canvas.delete("y_text")
canvas.delete("text")
canvas.delete("zero1")
canvas.delete("zero2")
def draw_all():
# 绘制两个0
canvas.create_text(430, 250, text='0', tag="zero1")
canvas.create_text(430, 315, text='0', tag="zero2")
# 绘制放缩栏
canvas.create_text(585, 315, text=str(max_coordinate), tag="scale_text")
canvas.create_text(430, 95, text=str(max_coordinate), tag="scale_text_y")
canvas.create_text(585, 250, text=str(max_coordinate), tag="scale_text_x")
# 绘制文字
canvas.create_text(440, 80, text='Light', tag="y_text")
canvas.create_text(585, 265, text='View', tag="x_text")
canvas.create_text(585, 330, text='Time', tag="text")
# 绘制指示值
canvas.create_text(510, 340, text='Light:0', tag="light_text")
canvas.create_text(510, 360, text='View:0', tag="view_text")
canvas.create_text(510, 380, text='Time:0', tag="time_text")
# 二维空间
canvas.create_line(440, 100, 580, 100, tag="2-dimension-box")
canvas.create_line(580, 100, 580, 240, tag="2-dimension-box")
canvas.create_line(580, 240, 440, 240, tag="2-dimension-box")
canvas.create_line(440, 240, 440, 100, tag="2-dimension-box")
# 单维轴
canvas.create_line(440, 300, 580, 300, tag="1-dimension-line")
# 第一个滑块
canvas.create_oval(oval_x1 - 4, oval_y1 - 4, oval_x1 + 4, oval_y1 + 4, fill="black", tag="oval1")
# 第二个滑块
canvas.create_oval(oval_x2 - 4, oval_y2 - 4, oval_x2 + 4, oval_y2 + 4, fill="black", tag="oval2")
def cmb_select_main(dataset):
global x, canvas, result_photo,mode
delete_all()
if dataset == "dataset-perfect-result-apple-3-dimension-3x3x3":
draw_all() #绘制所有canvas内容
args = EasyDict({
'dataset': './data/img-data/dataset-perfect-result-apple-3-dimension-3x3x3',
'savedir': './data/img-results/dataset-perfect-result-apple-3-dimension-3x3x3',
'type': ['light','view','time'],
'video': False,
'dims': [3, 3, 3],
'DSfactor': 8,
'neighbor_num': 2,
'lr': 0.0001,
'sigma': 0.1,
'stop_l1_thr': 0.01
})
mode = ['light','view','time']
num_n = 8 # 生成用的邻居数量
scale = 90
x = XFieldTest(args, num_n, scale)
img = x.generate3DimensionResult(light_value, view_value, time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
elif dataset == "dataset-perfect-result-elephant-3-dimension-3x3x3":
draw_all() # 绘制所有canvas内容
args = EasyDict({
'dataset': './data/img-data/dataset-perfect-result-elephant-3-dimension-3x3x3',
'savedir': './data/img-results/dataset-perfect-result-elephant-3-dimension-3x3x3',
'type': ['light', 'view', 'time'],
'video': False,
'dims': [3, 3, 3],
'DSfactor': 8,
'neighbor_num': 2,
'lr': 0.0001,
'sigma': 0.1,
'stop_l1_thr': 0.01
})
mode = ['light', 'view', 'time']
num_n = 8 # 生成用的邻居数量
scale = 90
x = XFieldTest(args, num_n, scale)
img = x.generate3DimensionResult(light_value, view_value, time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
elif dataset == "dataset-perfect-result-teapot-3-dimension-3x3x3":
draw_all() # 绘制所有canvas内容
args = EasyDict({
'dataset': './data/img-data/dataset-perfect-result-teapot-3-dimension-3x3x3',
'savedir': './data/img-results/dataset-perfect-result-teapot-3-dimension-3x3x3',
'type': ['light', 'view', 'time'],
'video': False,
'dims': [3, 3, 3],
'DSfactor': 16,
'neighbor_num': 2,
'lr': 0.0001,
'sigma': 0.1,
'stop_l1_thr': 0.01
})
mode = ['light', 'view', 'time']
num_n = 8 # 生成用的邻居数量
scale = 90
x = XFieldTest(args, num_n, scale)
img = x.generate3DimensionResult(light_value, view_value, time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
elif dataset == "dataset-perfect-result-chair-3-dimension-5x5x5":
draw_all() # 绘制所有canvas内容
args = EasyDict({
'dataset': './data/img-data/dataset-perfect-result-chair-3-dimension-5x5x5',
'savedir': './data/img-results/dataset-perfect-result-chair-3-dimension-5x5x5',
'type': ['light', 'view', 'time'],
'video': False,
'dims': [5, 5, 5],
'DSfactor': 8,
'neighbor_num': 2,
'lr': 0.0001,
'sigma': 0.1,
'stop_l1_thr': 0.01
})
mode = ['light', 'view', 'time']
num_n = 8 # 生成用的邻居数量
scale = 90
x = XFieldTest(args, num_n, scale)
img = x.generate3DimensionResult(light_value, view_value, time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
elif dataset == "ourdata-less-perfect-result-desktop-3-dimension-3x3x3":
draw_all() #绘制所有canvas内容
args = EasyDict({
'dataset': './data/img-data/ourdata-less-perfect-result-desktop-3-dimension-3x3x3',
'savedir': './data/img-results/ourdata-less-perfect-result-desktop-3-dimension-3x3x3',
'type': ['light','view','time'],
'video': False,
'dims': [3, 3, 3],
'DSfactor': 16,
'neighbor_num': 8,
'lr': 0.0001,
'sigma': 0.1,
'stop_l1_thr': 0.01
})
mode = ['light','view','time']
num_n = 8 # 生成用的邻居数量
scale = 90
x = XFieldTest(args, num_n, scale)
img = x.generate3DimensionResult(light_value, view_value, time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
elif dataset == "ourdata-less-perfect-result-medal-1-dimension-3":
# 第二个滑块
mode = ['time']
canvas.create_oval(oval_x2 - 4, oval_y2 - 4, oval_x2 + 4, oval_y2 + 4, fill="black", tag="oval2")
# 单维轴
canvas.create_line(440, 300, 580, 300, tag="1-dimension-line")
canvas.create_text(510, 380, text='Time:0', tag="time_text")
canvas.create_text(585, 330, text='Time', tag="text")
canvas.create_text(585, 315, text=str(max_coordinate), tag="scale_text")
canvas.create_text(430, 315, text='0', tag="zero2")
args = EasyDict({
'dataset': './data/img-data/ourdata-less-perfect-result-medal-1-dimension-3',
'savedir': './data/img-results/ourdata-less-perfect-result-medal-1-dimension-3',
'type': ['time'],
'dims': [3],
'DSfactor': 10,
'video':False,
'neighbor_num': 2,
'lr': 0.0001,
'sigma': 0.1,
'stop_l1_thr': 0.01,
'stop_delta_l1_thr': 0.0005
})
num_n = 2 # 生成用的邻居数量
scale = 90
x = XFieldTest(args, num_n, scale)
img = x.generate1DimensionResult(time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
elif dataset == "ourdata-perfect-result-medal-1-dimension-5":
# 第二个滑块
mode = ['time']
canvas.create_oval(oval_x2 - 4, oval_y2 - 4, oval_x2 + 4, oval_y2 + 4, fill="black", tag="oval2")
# 单维轴
canvas.create_line(440, 300, 580, 300, tag="1-dimension-line")
canvas.create_text(510, 380, text='Time:0', tag="time_text")
canvas.create_text(585, 330, text='Time', tag="text")
canvas.create_text(585, 315, text=str(max_coordinate), tag="scale_text")
canvas.create_text(430, 315, text='0', tag="zero2")
args = EasyDict({
'dataset': './data/img-data/ourdata-perfect-result-medal-1-dimension-5',
'savedir': './data/img-results/ourdata-perfect-result-medal-1-dimension-5',
'type': ['time'],
'dims': [5],
'DSfactor': 10,
'video': False,
'neighbor_num': 2,
'lr': 0.0001,
'sigma': 0.1,
'stop_l1_thr': 0.01,
'stop_delta_l1_thr': 0.0005
})
num_n = 2 # 生成用的邻居数量
scale = 90
x = XFieldTest(args, num_n, scale)
img = x.generate1DimensionResult(time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
elif dataset == "ourdata-perfect-result-water-1-dimension-7":
# 第二个滑块
mode = ['time']
canvas.create_oval(oval_x2 - 4, oval_y2 - 4, oval_x2 + 4, oval_y2 + 4, fill="black", tag="oval2")
# 单维轴
canvas.create_line(440, 300, 580, 300, tag="1-dimension-line")
canvas.create_text(510, 380, text='Time:0', tag="time_text")
canvas.create_text(585, 330, text='Time', tag="text")
canvas.create_text(585, 315, text=str(max_coordinate), tag="scale_text")
canvas.create_text(430, 315, text='0', tag="zero2")
args = EasyDict({
'dataset': './data/img-data/ourdata-perfect-result-water-1-dimension-7',
'savedir': './data/img-results/ourdata-perfect-result-water-1-dimension-7',
'type': ['time'],
'dims': [7],
'DSfactor': 8,
'video': False,
'neighbor_num': 2,
'lr': 0.0001,
'sigma': 0.1,
'stop_l1_thr': 0.01,
'stop_delta_l1_thr': 0.0005
})
num_n = 2 # 生成用的邻居数量
scale = 90
x = XFieldTest(args, num_n, scale)
img = x.generate1DimensionResult(time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
pb.stop()
tk.messagebox.showinfo('成功', '数据加载完成')
# 鼠标左键是否处于按下框选状态
result_photo = None
is_mouse_left_button_down = False
# 模式维度
mode = ["light","view","time"]
# 当前移动的是哪个oval
mouse_moving_oval_index = 1
max_coordinate = 1
light_value, view_value, time_value = 0, 0, 0
# UI生成———————————————————————————————————————————————————————————————————————————————————————————————————————————
# 生成窗口
root = Tk()
root.title("XField")
root.minsize(800, 600) # 最小尺寸
root.maxsize(800, 600) # 最大尺寸
# 生成introduction文字提示信息
introduction = Label(root, text="XField-Pytorch,请在下方选取数据集", font=('Arial', 14))
introduction.pack()
# 设置下拉菜单
cmb = ttk.Combobox(root,width=45)
cmb.pack()
# 设置下拉菜单中的值
cmb['value'] = (
'dataset-perfect-result-apple-3-dimension-3x3x3',
'dataset-perfect-result-elephant-3-dimension-3x3x3',
'dataset-perfect-result-teapot-3-dimension-3x3x3',
'dataset-perfect-result-chair-3-dimension-5x5x5',
'ourdata-less-perfect-result-desktop-3-dimension-3x3x3',
'ourdata-less-perfect-result-medal-1-dimension-3',
'ourdata-perfect-result-medal-1-dimension-5',
'ourdata-perfect-result-water-1-dimension-7',
)
# 设置默认值,即默认下拉框中的内容
cmb.current(0)
# 生成canvas画布
canvas = Canvas(root, width=600, height=400, background='white')
canvas.pack(anchor=CENTER, side=TOP, padx=0, pady=0)
oval_x1 = 440
oval_y1 = 240
oval_x2 = 440
oval_y2 = 300
# Scale
frame = LabelFrame(root, height=60, width=150, text='改变最大坐标Scale')
frame.pack(side='left', fill='none', expand=True)
scaleLine = tk.Scale(frame, from_=1, to=10, resolution=1, orient=tk.HORIZONTAL, command=resize)
scaleLine.set(1) # 设置初始值
scaleLine.grid(row=0, column=0)
draw_all()
# 鼠标位置实时显示模块生成
x, y = 0, 0
var = StringVar()
text = "鼠标当前位置:x:{}, y:{}".format(str(x).zfill(3), str(y).zfill(3))
var.set(text)
lab = Label(root, textvariable=var)
lab.pack(anchor=S, side=RIGHT, padx=10, pady=10)
pb = ttk.Progressbar(root, length=400, value=0, mode="indeterminate")
pb.pack(pady=10)
# 初始参数选择与加载绘制
args = EasyDict({
'dataset': './data/img-data/dataset-perfect-result-apple-3-dimension-3x3x3',
'savedir': './data/img-results/dataset-perfect-result-apple-3-dimension-3x3x3',
'type': ['light','view','time'],
'video':False,
'dims': [3, 3, 3],
'DSfactor': 8,
'neighbor_num': 2,
'lr': 0.0001,
'sigma': 0.1,
'stop_l1_thr': 0.01
})
num_n = 8 # 生成用的邻居数量
scale = 90
x = XFieldTest(args, num_n, scale)
img = x.generate3DimensionResult(light_value, view_value, time_value)
img = cv2.resize(img, (380, 280))
result_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
result_photo = ImageTk.PhotoImage(result_image)
canvas.create_image(210, 200, image=result_photo, tag="image")
canvas.update()
canvas.bind("<Motion>", mouse_motion)
canvas.bind("<Button-1>", mouse_left_button_down_event)
canvas.bind("<ButtonRelease-1>", mouse_left_button_release_event)
cmb.bind("<<ComboboxSelected>>", cmb_select)
mainloop()