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():
del objp_confirmed
# 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('key_press_event', on_click)
# If objp is given, display 3D object points in black
if len(objp) != 0 and not plt.fignum_exists(2):
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')
plt.rc('xtick', labelsize=5)
plt.rc('ytick', labelsize=5)
for i, (xs,ys,zs) in enumerate(np.float32(objp)):
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')
for i, (xs, ys, zs) in enumerate(np.float32(objp)):
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')
set_axes_equal(ax_3d)
ax_3d.set_xlabel('X')
ax_3d.set_ylabel('Y')
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)
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):
'''
@ -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
# To avoid to much click during the prompt
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:
# Left click: Add clicked point to imgp_confirmed
# 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
if 'events' in globals():
events.append(event)
else:
events = [event]
# Add clicked point to image
xydata = scat.get_offsets()
new_xydata = np.concatenate((xydata, [[event.xdata, event.ydata]]))
scat.set_offsets(new_xydata)
imgp_confirmed = np.expand_dims(scat.get_offsets(), axis=1)
plt.draw()
if is_button_one:
# Add clicked point to image
xydata = scat.get_offsets()
new_xydata = np.concatenate((xydata, [[event.xdata, event.ydata]]))
scat.set_offsets(new_xydata)
imgp_confirmed = np.expand_dims(scat.get_offsets(), axis=1)
plt.draw()
# Add clicked point to 3D object points if given
if len(objp) != 0:
@ -995,25 +980,40 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
if count == 0:
# retrieve objp_confirmed and plot 3D
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()
print(count)
elif count == len(objp) - 1:
in_prompt = True
objp_confirmed = \
[[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed + [objp[count]]][0]
if is_button_one:
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')
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(
"Validate Calibration", "Are you satisfied with point positioning ? ", icon="warning"
)
if msg_box.lower() == "yes":
# close all
plt.close('all')
# retrieve objp_confirmed
# objp_confirmed = np.array([[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed+[objp[count]]][0])
imgp_confirmed = np.array(imgp_confirmed, np.float32)
# delete all
if is_button_one:
imgp_confirmed = np.array(imgp_confirmed, np.float32)
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
for var_to_delete in ['events', 'count', 'scat', 'scat_3d', 'fig_3d', 'ax_3d',
'objp_confirmed_notok']:
@ -1040,19 +1040,36 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
ax_3d.collections[-1].remove()
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:
# retrieve objp_confirmed and plot 3D
objp_confirmed = \
[[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed + [objp[count]]][0]
ax_3d.scatter(*objp[count], marker='o', color='g')
fig_3d.canvas.draw()
print(count)
if is_button_one:
objp_confirmed = \
[[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed + [objp[count]]][0]
ax_3d.scatter(*objp[count], marker='o', color='g')
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:
# 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
elif event.button == 3: # right click
elif is_button_three: # right click
if 'events' in globals():
# If last event was left click:
if 'button' in dir(events[-1]):