hoffstadt / DearPyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies
https://dearpygui.readthedocs.io/en
MIT License
12.96k stars 677 forks source link

Wrong value in the first mouse click while using `dpg.get_mouse_pos(local = True)` #1718

Open SriniMaiya opened 2 years ago

SriniMaiya commented 2 years ago

Version of Dear PyGui

Version: 1.5.1 Operating System: Ubuntu 20.04

My Issue/Question

I am trying to build a GUI for a camera where the user has to click on the second image and 3 mouse coordinates have to be registered. I used dpg.get_mouse_pos(local = True) to register the coordinates.

Issue:

While the second click on the same pixel returns exact pixel values of the image, the first click is offset by +108 pixels in width. The issue can be followed also on discord.

To Reproduce

Steps to reproduce the behavior:

  1. Run the code.
  2. Click button Start Camera -> Capture frame
  3. Click on any pixel in the right image window keep the mouse in the same position
  4. Note the pixel value printed in the GUI
  5. Click the same pixel again
  6. Note the value decrease by 108 pixels ( Decreased value is the correct value) In the actual code, in which the camera is connected, the offset is 180px.
  7. Repeat Step 2

Expected behavior

Same pixel values on both first and second clicks

Screenshots/Video

https://user-images.githubusercontent.com/75990547/164757359-da1868a7-78d8-4d78-8b76-fb9b8154c181.mp4

In the video above, I have used a pointer highlighter to differentiate the first and the second click.

Code for execution:

import random
import dearpygui.dearpygui as dpg
import numpy as np

dpg.create_context()
dpg.create_viewport(title="Caliberation matrix", height=800, width=1940)
# dpg.set_viewport_vsync(True)
dpg.setup_dearpygui()

run = 0
width = 640
height = 480

color_frame = np.random.rand(height, width, 3)
depth_frame =  np.random.rand(height, width, 3)
# print(color_frame, depth_frame)

window_dim = np.add(np.multiply(2, [height, width]), 10)
width_w = window_dim[1]
height_w = window_dim[0]
color = np.ravel(color_frame)
depth = np.ravel(depth_frame)
color = np.zeros(color.shape, dtype=np.float32)
depth = np.zeros(depth.shape, dtype=np.float32)

def stream():
    color = np.random.rand(height, width, 3) 
    depth = np.random.rand(height, width, 3)
    color = np.ravel(color)
    depth = np.ravel(depth)
    dpg.set_value("color_tag", color)
    dpg.set_value("depth_tag", depth)

def send_ords(sender, value, user_data):
    pixel = dpg.get_mouse_pos(local=True)
    global width
    if value[1] == "depth_img" and value[0] == 0:
        ### If the click is on second window, subtract width of first window(640) + padding(10)
        pixel[0] = pixel[0] - (width+10)        
    dpg.set_value("Print", f"The mouse click was @:  {pixel}")

with dpg.texture_registry():

    dpg.add_raw_texture(width=width, height=height, 
                            default_value=color,
                            format=dpg.mvFormat_Float_rgb,
                            tag="color_tag" )

    dpg.add_raw_texture(width=width, height=height,
                            default_value=depth, 
                            format=dpg.mvFormat_Float_rgb, 
                            tag="depth_tag" )

with dpg.window(label="Camera Stream", width=width*2+20, height=height+200, pos=[200, 0], 
                    no_move=True,tag = "Stream Window"):
    dpg.add_image(texture_tag="depth_tag", pos=[width+10, 0], tag= "depth_img", track_offset=True)
    dpg.add_image(texture_tag="color_tag", pos=[0.0, 0.0], tag="color_img", track_offset=True)
    dpg.add_text(default_value = "Can only click on second window!!!",pos=[width+50, height+100] )
    dpg.add_text(default_value="", pos = [width + 50, height + 150], tag = "Print")

with dpg.window( pos=[20,200], no_move=True, no_collapse=True, no_title_bar=True, no_resize=True ):

    dpg.add_button(label="Start Camera", pos=[10, 10], callback= stream, tag= "BtnStart", tracked=True )
    dpg.add_button(label="Capture frame", pos=[10, 60], callback= stream, tag= "BtnStop", tracked=True )

with dpg.item_handler_registry(tag="Button_reg") as reg:
    dpg.add_item_clicked_handler()

dpg.bind_item_handler_registry("BtnStart","Button_reg" )
dpg.bind_item_handler_registry("BtnStop", "Button_reg" )

# mouse-click handler for depth image 
with dpg.item_handler_registry(tag = "Plot_reg"):
    dpg.add_item_clicked_handler(tag = "Image_reg", callback=send_ords)

dpg.bind_item_handler_registry("depth_img", "Plot_reg")

dpg.show_viewport()

while dpg.is_dearpygui_running():
    # Start and stop the video stream
    if dpg.is_item_clicked("BtnStart"):
        run = 1
    elif dpg.is_item_clicked("BtnStop"):
        run = 0

    if run == 1:
        stream()
    dpg.render_dearpygui_frame()

dpg.destroy_context()
SriniMaiya commented 2 years ago

I closed the issue by mistake! Sorry