Allow to use h also and lock the code. msg box not here anymore.

This commit is contained in:
Anaaim 2024-04-15 17:12:19 +02:00
parent 9e4ec13934
commit bc497fdf37

View File

@ -912,8 +912,9 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
if 'objp_confirmed' in globals(): if 'objp_confirmed' in globals():
del objp_confirmed del objp_confirmed
# If 'c', allows retrieving imgp_confirmed by clicking them on the image # If 'c', allows retrieving imgp_confirmed by clicking them on the image
scat = ax.scatter([],[],s=100,marker='+',color='g') scat = ax.scatter([], [], s=100, marker='+', color='g')
plt.connect('button_press_event', on_click) plt.connect('button_press_event', on_click)
plt.connect('key_press_event', on_click)
# If objp is given, display 3D object points in black # If objp is given, display 3D object points in black
if len(objp) != 0 and not plt.fignum_exists(2): if len(objp) != 0 and not plt.fignum_exists(2):
fig_3d = plt.figure() fig_3d = plt.figure()
@ -922,45 +923,17 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
ax_3d = fig_3d.add_subplot(projection='3d') ax_3d = fig_3d.add_subplot(projection='3d')
plt.rc('xtick', labelsize=5) plt.rc('xtick', labelsize=5)
plt.rc('ytick', labelsize=5) plt.rc('ytick', labelsize=5)
for i, (xs,ys,zs) in enumerate(np.float32(objp)): for i, (xs, ys, zs) in enumerate(np.float32(objp)):
ax_3d.scatter(xs,ys,zs, marker='.', color='k') ax_3d.scatter(xs, ys, zs, marker='.', color='k')
ax_3d.text(xs,ys,zs, f'{str(i+1)}', size=10, zorder=1, color='k') ax_3d.text(xs, ys, zs, f'{str(i + 1)}', size=10, zorder=1, color='k')
set_axes_equal(ax_3d) set_axes_equal(ax_3d)
ax_3d.set_xlabel('X') ax_3d.set_xlabel('X')
ax_3d.set_ylabel('Y') ax_3d.set_ylabel('Y')
ax_3d.set_zlabel('Z') ax_3d.set_zlabel('Z')
if np.all(objp[:,2] == 0): if np.all(objp[:, 2] == 0):
ax_3d.view_init(elev=-90, azim=0) ax_3d.view_init(elev=-90, azim=0)
fig_3d.show() fig_3d.show()
if event.key == 'h':
# If 'h', indicates that one of the objp is not visible on image
# Displays it in red on 3D plot
if len(objp) != 0 and 'ax_3d' in globals():
count = [0 if 'count' not in globals() else count+1][0]
if 'events' not in globals():
# retrieve first objp_confirmed_notok and plot 3D
events = [event]
objp_confirmed_notok = objp[count]
ax_3d.scatter(*objp_confirmed_notok, marker='o', color='r')
fig_3d.canvas.draw()
elif count == len(objp)-1:
# if all objp have been clicked or indicated as not visible, close all
objp_confirmed = np.array([[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed+[objp[count]]][0])[:-1]
imgp_confirmed = np.array(np.expand_dims(scat.get_offsets(), axis=1), np.float32)
plt.close('all')
for var_to_delete in ['events', 'count', 'scat', 'fig_3d', 'ax_3d', 'objp_confirmed_notok']:
if var_to_delete in globals():
del globals()[var_to_delete]
else:
# retrieve other objp_confirmed_notok and plot 3D
events.append(event)
objp_confirmed_notok = objp[count]
ax_3d.scatter(*objp_confirmed_notok, marker='o', color='r')
fig_3d.canvas.draw()
else:
pass
def on_click(event): def on_click(event):
''' '''
@ -971,23 +944,35 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
global imgp_confirmed, objp_confirmed, objp_confirmed_notok, scat, ax_3d, fig_3d, events, count, xydata, in_prompt global imgp_confirmed, objp_confirmed, objp_confirmed_notok, scat, ax_3d, fig_3d, events, count, xydata, in_prompt
# To avoid to much click during the prompt # To avoid to much click during the prompt
in_prompt_not_exist = "in_prompt" not in globals() in_prompt_not_exist = "in_prompt" not in globals()
# This part is done because to allow to manage in the same function button_press_event and key_press_event
is_button_one = False
is_button_three = False
is_key_h = False
if event.name == 'button_press_event':
if event.button == 1:
is_button_one = True
elif event.button == 3:
is_button_three = True
elif event.name == 'key_press_event':
if event.key == "h":
is_key_h = True
if in_prompt_not_exist or not in_prompt: if in_prompt_not_exist or not in_prompt:
# Left click: Add clicked point to imgp_confirmed # Left click: Add clicked point to imgp_confirmed
# Display it on image and on 3D plot # Display it on image and on 3D plot
if event.button == 1: if is_button_one or is_key_h:
# To remember the event to cancel after right click # To remember the event to cancel after right click
if 'events' in globals(): if 'events' in globals():
events.append(event) events.append(event)
else: else:
events = [event] events = [event]
if is_button_one:
# Add clicked point to image # Add clicked point to image
xydata = scat.get_offsets() xydata = scat.get_offsets()
new_xydata = np.concatenate((xydata, [[event.xdata, event.ydata]])) new_xydata = np.concatenate((xydata, [[event.xdata, event.ydata]]))
scat.set_offsets(new_xydata) scat.set_offsets(new_xydata)
imgp_confirmed = np.expand_dims(scat.get_offsets(), axis=1) imgp_confirmed = np.expand_dims(scat.get_offsets(), axis=1)
plt.draw() plt.draw()
# Add clicked point to 3D object points if given # Add clicked point to 3D object points if given
if len(objp) != 0: if len(objp) != 0:
@ -995,25 +980,40 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
if count == 0: if count == 0:
# retrieve objp_confirmed and plot 3D # retrieve objp_confirmed and plot 3D
objp_confirmed = [objp[count]] objp_confirmed = [objp[count]]
ax_3d.scatter(*objp[count], marker='o', color='g') if is_button_one:
ax_3d.scatter(*objp[count], marker='o', color='g')
elif is_key_h:
ax_3d.scatter(*objp_confirmed_notok, marker='o', color='r')
fig_3d.canvas.draw() fig_3d.canvas.draw()
print(count) print(count)
elif count == len(objp) - 1: elif count == len(objp) - 1:
in_prompt = True in_prompt = True
objp_confirmed = \ if is_button_one:
[[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed + [objp[count]]][0] objp_confirmed = \
[[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed + [objp[count]]][0]
elif is_key_h:
objp_confirmed = np.array([[objp[
count]] if 'objp_confirmed' not in globals() else objp_confirmed + [
objp[count]]][0])[:-1]
ax_3d.scatter(*objp[count], marker='o', color='g') ax_3d.scatter(*objp[count], marker='o', color='g')
fig_3d.canvas.draw() fig_3d.canvas.draw()
# Small bit of code to not have the basic windows of tkinter when using the askquestion messsage box
msg = tkinter.Tk()
msg.withdraw()
msg_box = tkinter.messagebox.askquestion( msg_box = tkinter.messagebox.askquestion(
"Validate Calibration", "Are you satisfied with point positioning ? ", icon="warning" "Validate Calibration", "Are you satisfied with point positioning ? ", icon="warning"
) )
if msg_box.lower() == "yes": if msg_box.lower() == "yes":
# close all
plt.close('all')
# retrieve objp_confirmed # retrieve objp_confirmed
# objp_confirmed = np.array([[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed+[objp[count]]][0]) if is_button_one:
imgp_confirmed = np.array(imgp_confirmed, np.float32) imgp_confirmed = np.array(imgp_confirmed, np.float32)
# delete all elif is_key_h:
# if all objp have been clicked or indicated as not visible, close all
imgp_confirmed = np.array(np.expand_dims(scat.get_offsets(), axis=1), np.float32)
# delete all
plt.close('all')
in_prompt = False in_prompt = False
for var_to_delete in ['events', 'count', 'scat', 'scat_3d', 'fig_3d', 'ax_3d', for var_to_delete in ['events', 'count', 'scat', 'scat_3d', 'fig_3d', 'ax_3d',
'objp_confirmed_notok']: 'objp_confirmed_notok']:
@ -1040,19 +1040,36 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
ax_3d.collections[-1].remove() ax_3d.collections[-1].remove()
fig_3d.canvas.draw() fig_3d.canvas.draw()
# If last event was 'h' key
elif events[-1].key == 'h':
if len(objp) != 0:
if count >= 1: count -= 1
# Remove last point from objp_confirmed_notok
objp_confirmed_notok = objp_confirmed_notok[:-1]
# remove from plot
if len(ax_3d.collections) > len(objp):
ax_3d.collections[-1].remove()
fig_3d.canvas.draw()
else: else:
# retrieve objp_confirmed and plot 3D # retrieve objp_confirmed and plot 3D
objp_confirmed = \ if is_button_one:
[[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed + [objp[count]]][0] objp_confirmed = \
ax_3d.scatter(*objp[count], marker='o', color='g') [[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed + [objp[count]]][0]
fig_3d.canvas.draw() ax_3d.scatter(*objp[count], marker='o', color='g')
print(count) fig_3d.canvas.draw()
elif is_key_h:
# retrieve other objp_confirmed_notok and plot 3D
events.append(event)
objp_confirmed_notok = objp[count]
ax_3d.scatter(*objp_confirmed_notok, marker='o', color='r')
fig_3d.canvas.draw()
# Right click: # Right click:
# If last event was left click, remove last point and if objp given, from objp_confirmed # If last event was left click, remove last point and if objp given, from objp_confirmed
# If last event was 'H' and objp given, remove last point from objp_confirmed_notok # If last event was 'H' and objp given, remove last point from objp_confirmed_notok
elif event.button == 3: # right click elif is_button_three: # right click
if 'events' in globals(): if 'events' in globals():
# If last event was left click: # If last event was left click:
if 'button' in dir(events[-1]): if 'button' in dir(events[-1]):