-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigMat_GUI.py
605 lines (561 loc) · 19.5 KB
/
ConfigMat_GUI.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import os
import dearpygui.dearpygui as dpg
import numpy as np
from Measure_DPG import ConfigMat
# Create the GUI context
dpg.create_context()
# Create main window(Viewport)
dpg.create_viewport(
title="Caliberation matrix",
height=1080,
width=1640,
large_icon=os.path.join("assets", "Icon.ico"),
)
# Setup the main window
dpg.setup_dearpygui()
# RUN: If the streaming is on
RUN = 0
# CLICK: If the "Select Points" btn is clicked
CLICK = 0
# ID: Point IDs. p1 => Origin p2 => x-axis point p3 => y-axis point
ID = ["p1", "p2", "p3"]
# NCLICKS: number of points selected
NCLICKS = 0
# Store the coordinates
CO_ORD = {"O": None, "OX": None, "OY": None}
# Final generated matrix
MATRIX = None
# Class obect from the Measure_DPG.py
cam_object = ConfigMat()
# Get a 3 frames to setup the height, WIDTH of stream window
COL_FRAME, IR_FRAME, DEP_FRAME = cam_object.get_a_frame()
# Width and height of the frame
WIDTH = DEP_FRAME.shape[1]
HEIGHT = DEP_FRAME.shape[0]
# Color image array for initialization of GUI
COLOR = np.ravel(COL_FRAME)
COLOR = np.zeros(COLOR.shape)
# Depth image array for initialization of GUI
DEPTH = np.ravel(DEP_FRAME)
DEPTH = np.zeros(DEPTH.shape)
# Infrared image array for initialization of GUI
IR = np.ravel(IR_FRAME)
IR = np.zeros(IR.shape)
##################################################
################ Stream functions ################
##################################################
def stream():
"""
Get camera frames and display on the stream window
"""
# Clear the plot annotations when streaming
for i in ID:
if dpg.does_item_exist(i):
dpg.delete_item(i)
global DEP_FRAME, CO_ORD
# Get COLOR, DEPTH and infrared frames
COLOR, IR, DEPTH, DEP_FRAME = cam_object.video()
CO_ORD = {"O": None, "OX": None, "OY": None}
dpg.set_value("Clicked", "0")
dpg.delete_item("matrix_table", children_only=True)
# Convert the frame to a list
COLOR = np.ravel(COLOR)
DEPTH = np.ravel(DEPTH)
IR = np.ravel(IR)
# Normalize the list
COLOR = np.asfarray(COLOR, dtype=np.float32) / 255
DEPTH = np.asfarray(DEPTH, dtype=np.float32) / 255
IR = np.asfarray(IR, dtype=np.float32) / 255
# Set the values of respective textures
dpg.set_value("color_tag", COLOR)
dpg.set_value("depth_tag", DEPTH)
dpg.set_value("ir_tag", IR)
# Enable the stop button to be clicked once the stream begins.
dpg.enable_item("BtnStop")
def trigger_click(sender, value, user_data):
"""
Triggers the clicking and click registeration
when "Select Point" button is clicked.
"""
global CLICK, CO_ORD, MATRIX, NCLICKS
# If left clicked on the "Select Point " button
if user_data == 1:
# Set trigger variable to 1
CLICK = 1
MATRIX = None
CO_ORD = {"O": None, "OX": None, "OY": None}
# Delete the displayed matrix
dpg.delete_item("matrix_table", children_only=True)
# f-sting for printing of the clicked pixels
fstring = f"Registered points: \nO : {CO_ORD['O']}\tO->X : {CO_ORD['OX']}\tO->Y : {CO_ORD['OY']}"
# Set the text item "Print_coords" to the fstring
dpg.set_value("Print_coords", fstring)
# Set the Info Item-"Select Point" to 0
dpg.set_value("Clicked", "0")
for i in ID:
if dpg.does_item_exist(i):
dpg.delete_item(i)
# Set variable NCLICKS to 0
NCLICKS = 0
def get_clicks():
"""Register clicks, display matrix and enable the save button"""
global CLICK, CO_ORD, DEP_FRAME, MATRIX, NCLICKS
if CLICK == 1:
# Get the plot mouse position
pixel = dpg.get_plot_mouse_pos()
# Convert the float value to int
pixel = [int(np.round_(x)) for x in pixel]
dpg.add_plot_annotation(
parent="color_img",
label=" + ",
default_value=pixel,
color=(255, 0, 0, 80),
tag=ID[NCLICKS - 1],
clamped=False,
)
# Invert the pixel value of height
pixel[1] = HEIGHT - pixel[1]
# Add red annotations for the clicked points
# Store the mouse position in the dictionary
for key, _ in CO_ORD.items():
if CO_ORD[key] == None:
CO_ORD[key] = pixel
break
# Display the registered points
fstring = f"Registered points: \nO : {CO_ORD['O']}\tO->X : {CO_ORD['OX']}\tO->Y : {CO_ORD['OY']} "
# Set the f-string to text item "Print_coords"
dpg.set_value("Print_coords", fstring)
# Increment the number of clicks
NCLICKS += 1
# Set the Info Item-"Select Point" to number of clicks
dpg.set_value("Clicked", str(NCLICKS))
# if all 3 points are not None,
if all(CO_ORD.values()):
# Set the Info Item-"Select Point" to "Done"
dpg.set_value("Clicked", "Done!")
# Calculate the transformation matrix
MATRIX = cam_object.calculate_distance(ords=CO_ORD, depth_frame=DEP_FRAME)
# Set CLICK, fstring to default values
CLICK = 0
fstring = f"Registered points: \nO : {CO_ORD['O']}\tO->X : {CO_ORD['OX']}\tO->Y : {CO_ORD['OY']} "
# Set the test item to default
dpg.set_value("Print_coords", fstring)
# Deleté the matrix contents
dpg.delete_item("matrix_table", children_only=True)
# if the matrix has been calculated,
if MATRIX is not None:
# Set the table element cells to the matrix values
for i in range(4):
dpg.add_table_column(parent="matrix_table")
for i in range(4):
with dpg.table_row(parent="matrix_table"):
for j in range(4):
dpg.add_text(round(MATRIX[i][j], 3))
# Enable save button
dpg.enable_item("SaveBtn")
def crosshair(sender, app_data, user_data):
"""
-> Adds a annotation crosshair to the neighbor plot
of the mouse position
-> Called by: hover handler.
"""
# delete the previous crosshair if present
if dpg.does_item_exist("temp"):
dpg.delete_item("temp")
# Get present mouse position
pixel = dpg.get_plot_mouse_pos()
# If the mouse is hovering in the color image
if app_data == "color_img":
# crosshair on depth image
reciever = "depth_img"
else:
# crosshair on color image
reciever = "color_img"
# Draw a temporary crosshair on the reciever
dpg.add_plot_annotation(
parent=reciever,
label=" + ",
default_value=pixel,
clamped=False,
tag="temp",
color=(128, 0, 128, 80),
)
# If the plot is clicked, trigger get_clicks function
if dpg.is_item_left_clicked(app_data):
get_clicks()
def set_camera_config(sender, app_data):
"""
-> Sets the camera config
Called by : file_dialog_tag
"""
json_path = app_data["file_path_name"]
# Loads the object with the settings present in json file
cam_object.load_settings(file_path=json_path)
# Change Info Text
dpg.set_value("Loaded", "Done!")
# If the config successfully loaded,
if dpg.get_value("Loaded") == "Done!":
# Enable stream button
dpg.enable_item("BtnStart")
def save_matrix(sender, app_data):
"""
-> Saves generated matrix to a file
Called by: file_save_tag
"""
global MATRIX
dpg.set_value("Saved", "")
# Save necessary elements of the matrix
save_arr = np.ravel(MATRIX[:3, :].T)
file_path = app_data["file_path_name"]
# Write the save_arr to text with a seperator
with open(file_path, "w") as file:
for i, val in enumerate(save_arr, start=1):
val = f"{val:.3f} "
file.write(val)
dpg.set_value("Saved", "Saved!")
def stream_button(sender, app_data, user_data):
"""
-> Reads the sent user_data and changes button theme accordingly
-> User data is only sent when the button has been clicked
-> By default color stream is True
"""
# Read the userdata
state, enabled_theme, disabled_theme = user_data
# Toggle the state
state = not state
# If the IR Button is clicked
if sender == "BtnIR" and state == True:
# Toggle to IR stream the color window
dpg.configure_item("color_window", texture_tag="ir_tag")
# Set the current user_data of the IR button
dpg.set_item_user_data(
"BtnIR",
(
state,
enabled_theme,
disabled_theme,
),
)
# Set the current user_data of the color button
dpg.set_item_user_data(
"BtnColor",
(
not state,
enabled_theme,
disabled_theme,
),
)
# Toggle theme of IR button to enabled_theme
dpg.bind_item_theme("BtnIR", enabled_theme)
# Toggle theme of color button to disabled_theme
dpg.bind_item_theme("BtnColor", disabled_theme)
# If the Color button is clicked
if sender == "BtnColor" and state == True:
# Toggle to Color stream the color window
dpg.configure_item("color_window", texture_tag="color_tag")
# Set the current user_data of the IR button
dpg.set_item_user_data(
"BtnIR",
(
not state,
enabled_theme,
disabled_theme,
),
)
# Set the current user_data of the color button
dpg.set_item_user_data(
"BtnColor",
(
state,
enabled_theme,
disabled_theme,
),
)
# Toggle theme of IR button to enabled_theme
dpg.bind_item_theme("BtnIR", disabled_theme)
# Toggle theme of color button to disabled_theme
dpg.bind_item_theme("BtnColor", enabled_theme)
##################################################
################## GUI Elements ##################
##################################################
# Set red as disabled theme
with dpg.theme() as disabled_theme:
with dpg.theme_component(dpg.mvAll):
dpg.add_theme_color(
dpg.mvThemeCol_Text, (255, 0, 0), category=dpg.mvThemeCat_Core
)
# Set green as enabled_theme
with dpg.theme() as enabled_theme:
with dpg.theme_component(dpg.mvAll):
dpg.add_theme_color(
dpg.mvThemeCol_Text, (0, 255, 0), category=dpg.mvThemeCat_Core
)
# Load the font file
font_path = os.path.join("assets", "OpenSans-Regular.ttf")
# Set two sized fonts
with dpg.font_registry():
default_font = dpg.add_font(file=font_path, size=30)
second_font = dpg.add_font(file=font_path, size=24)
# File Dialog to read the configuration file
with dpg.file_dialog(
directory_selector=False,
show=False,
callback=set_camera_config,
file_count=1,
tag="file_dialog_tag",
height=800,
max_size=[1000, 800],
):
dpg.add_file_extension(".json", color=(255, 255, 0, 255))
# File Dialog to save the matrix to a file
with dpg.file_dialog(
directory_selector=False,
show=False,
callback=save_matrix,
file_count=1,
tag="file_save_tag",
height=800,
max_size=[1000, 800],
default_filename="Target_Cam",
):
dpg.add_file_extension(".trf", color=(255, 255, 0, 255))
# textures
with dpg.texture_registry():
# Add a texture color_stream
dpg.add_raw_texture(
width=WIDTH,
height=HEIGHT,
default_value=COLOR,
format=dpg.mvFormat_Float_rgb,
tag="color_tag",
)
# Add a texture depth_stream
dpg.add_raw_texture(
width=WIDTH,
height=HEIGHT,
default_value=DEPTH,
format=dpg.mvFormat_Float_rgb,
tag="depth_tag",
)
# Add a texture IR_stream
dpg.add_raw_texture(
width=WIDTH,
height=HEIGHT,
default_value=IR,
format=dpg.mvFormat_Float_rgb,
tag="ir_tag",
)
# Stream Window
with dpg.window(
label="Camera Stream",
autosize=True,
pos=[350, 0],
no_move=True,
tag="Stream Window",
):
# Horizontal grouping of the color/IR and depth windows
with dpg.group(horizontal=True):
# Add a plot window for color/IR stream
with dpg.plot(
equal_aspects=True,
tag="color_img",
crosshairs=True,
pan_mod=None,
width=WIDTH,
height=HEIGHT,
):
# Add X plot axis
dpg.add_plot_axis(
dpg.mvXAxis, no_tick_labels=True, no_gridlines=True, no_tick_marks=True
)
# With X add Y plot axis
with dpg.plot_axis(
dpg.mvYAxis, no_tick_labels=True, no_gridlines=True, no_tick_marks=True
):
# Add Image from the color_texture
dpg.add_image_series(
"color_tag", [0, 0], [WIDTH, HEIGHT], tag="color_window"
)
# Add a plot window for depth stream
with dpg.plot(
equal_aspects=True,
tag="depth_img",
crosshairs=True,
pan_mod=None,
width=WIDTH,
height=HEIGHT,
):
# Add X plot axis
dpg.add_plot_axis(
dpg.mvXAxis, no_tick_labels=True, no_gridlines=True, no_tick_marks=True
)
# With X add Y plot axis
with dpg.plot_axis(
dpg.mvYAxis, no_tick_labels=True, no_gridlines=True, no_tick_marks=True
):
# Add Image from the color_texture
dpg.add_image_series(
"depth_tag", [0, 0], [WIDTH, HEIGHT], tag="depth_window"
)
# Add buttons to switch color and IR streams
with dpg.group(horizontal=False):
# Color stream button
dpg.add_button(
label="Color Stream",
pos=[100, HEIGHT + 50],
callback=stream_button,
user_data=(True, enabled_theme, disabled_theme),
tag="BtnColor",
)
# IR stream button
dpg.add_button(
label="Infrared Stream",
pos=[415, HEIGHT + 50],
callback=stream_button,
user_data=(False, enabled_theme, disabled_theme),
tag="BtnIR",
)
# Depth stream info
dpg.add_text(
default_value="Depth Stream", pos=[int(WIDTH / 2) + WIDTH - 80, HEIGHT + 50]
)
dpg.add_separator()
# Prints mouse click pixels.
# Values set by get_clicks() and trigger_click()
dpg.add_text(
default_value="", pos=[WIDTH + 50, HEIGHT + 130], tag="Print_coords"
)
# Transformation matrix label/text
dpg.add_text(
default_value="Transformation Matrix: ",
pos=[20, HEIGHT + 130],
tag="matrix",
)
# Set fonts to respective elements
dpg.bind_item_font("Print_coords", second_font)
dpg.bind_item_font("matrix", second_font)
dpg.bind_item_theme("BtnIR", disabled_theme)
dpg.bind_item_theme("BtnColor", enabled_theme)
# Add a table to print the matrix.
# Values set by get_clicks()
with dpg.table(
label="Transformation Matrix",
header_row=False,
tag="matrix_table",
pos=[40, HEIGHT + 160],
width=350,
borders_innerH=True,
borders_innerV=True,
borders_outerH=True,
borders_outerV=True,
resizable=False,
):
pass
# Left Button Window
with dpg.window(
pos=[40, 200],
no_move=True,
no_collapse=True,
no_title_bar=True,
no_resize=True,
no_background=True,
autosize=True,
):
# Vertically align the buttons
with dpg.group(horizontal=False, tag="Btn_instruction"):
# Text description for all the buttons
# Present on top of respective button
dpg.add_text(default_value="1. Load Camera settings.", pos=[20, 10])
dpg.add_text(default_value="2. Start streaming.", pos=[20, 90])
dpg.add_text(default_value="3. Capture a frame.", pos=[20, 170])
dpg.add_text(default_value="4. Select points", pos=[20, 250])
dpg.add_text(default_value="5. Save Matrix", pos=[20, 330])
dpg.bind_item_font("Btn_instruction", second_font)
# Button to Load camera config.
# When clicked, file dialog is made visible
dpg.add_button(
label="Camera Config",
pos=[20, 40],
callback=lambda: dpg.show_item("file_dialog_tag"),
width=200,
)
dpg.bind_item_font("file_dialog_tag", second_font)
# Info Text for "Camera Config" button
dpg.add_text(default_value="", pos=[230, 40], tag="Loaded")
# Button to start camera. Controlled in last while loop
dpg.add_button(
label="Start Camera", pos=[20, 120], tag="BtnStart", width=200, enabled=False
)
# Info Text for "Start Camera" button
dpg.add_text(default_value="", pos=[230, 120], tag="Streaming")
# Button to Capture a frame. Controlled in last while loop
dpg.add_button(
label="Capture frame", pos=[20, 200], tag="BtnStop", width=200, enabled=False
)
# Info Text for "Capture frame" button
dpg.add_text(default_value="", pos=[230, 200], tag="Captured")
# Button to select points. Controlled by trigger_click()-> get_clicks()
dpg.add_button(
label="Select Points",
pos=[20, 280],
callback=trigger_click,
user_data=1,
width=200,
enabled=False,
tag="click",
)
# Info Text for "Select points" button
dpg.add_text(default_value="", pos=[230, 280], tag="Clicked")
# Button to save generated matrix.
# called in get_clicks()
dpg.add_button(
label="Save matrix",
pos=[20, 360],
width=200,
enabled=False,
tag="SaveBtn",
callback=lambda: dpg.show_item("file_save_tag"),
)
# Info Text for "Save Matrix" button
dpg.add_text(default_value="", pos=[230, 360], tag="Saved")
# Item Clicked handler.
# Registers button clicks for "Start Camera" and "Capture frame" buttons
with dpg.item_handler_registry(tag="Button_reg") as reg:
dpg.add_item_clicked_handler(button=1)
dpg.bind_item_handler_registry("BtnStart", "Button_reg")
dpg.bind_item_handler_registry("BtnStop", "Button_reg")
# Hover handler
# Registers if the mouse is hovered over the color/IR plots
# If yes then triggers the second crosshair
with dpg.item_handler_registry(tag="hover_reg"):
dpg.add_item_hover_handler(callback=crosshair)
dpg.bind_item_handler_registry("color_img", "hover_reg")
dpg.bind_item_handler_registry("depth_img", "hover_reg")
# Set the default font
dpg.bind_font(default_font)
# Show the base viewport
dpg.show_viewport()
# When the GUI is running,
while dpg.is_dearpygui_running():
# If the "Start Camera" button is clicked, set RUN to 1
if dpg.is_item_clicked("BtnStart"):
RUN = 1
# If the "Capture frame" button is clicked, set RUN to 0
elif dpg.is_item_clicked("BtnStop") and dpg.is_item_enabled("BtnStop"):
RUN = 0
# Change the Info Texts of the corresponding buttons
dpg.set_value("Captured", "Yes")
dpg.set_value("Streaming", "")
# Enable the "Select Points" button
if not dpg.is_item_enabled("click"):
dpg.enable_item("click")
# If "Start camera" has been clicked and config is loaded (enabling the "Start Camera" button),
# Start streaming.
if RUN == 1 and dpg.is_item_enabled("BtnStart"):
stream()
dpg.set_value("Streaming", "Yes")
dpg.set_value("Captured", "")
# Render the present frame
dpg.render_dearpygui_frame()
# Exit
dpg.destroy_context()