Skip to content

Commit

Permalink
added camera capture example, plot with button, and a status bar at t…
Browse files Browse the repository at this point in the history
…he bottom of viewport
  • Loading branch information
my1e5 committed Nov 8, 2023
1 parent 7d0b3f7 commit d9878ff
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
67 changes: 67 additions & 0 deletions misc/camera_capture_with_opencv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Credit Pcothren - example taken directly from https://github.com/Pcothren/DearPyGui-Examples/blob/d644fa2053c40c5b4dad8dd07ff2366ff2d1a356/camera_capture_with_opencv.py

import dearpygui.dearpygui as dpg
import cv2 as cv
import numpy as np

dpg.create_context()
dpg.create_viewport(title='Custom Title', width=600, height=800)
dpg.setup_dearpygui()

vid = cv.VideoCapture(0)
ret, frame = vid.read()

# image size or you can get this from image shape
frame_width = vid.get(cv.CAP_PROP_FRAME_WIDTH)
frame_height = vid.get(cv.CAP_PROP_FRAME_HEIGHT)
video_fps = vid.get(cv.CAP_PROP_FPS)
print(frame_width)
print(frame_height)
print(video_fps)

print("Frame Array:")
print("Array is of type: ", type(frame))
print("No. of dimensions: ", frame.ndim)
print("Shape of array: ", frame.shape)
print("Size of array: ", frame.size)
print("Array stores elements of type: ", frame.dtype)
data = np.flip(frame, 2) # because the camera data comes in as BGR and we need RGB
data = data.ravel() # flatten camera data to a 1 d stricture
data = np.asfarray(data, dtype='f') # change data type to 32bit floats
texture_data = np.true_divide(data, 255.0) # normalize image data to prepare for GPU

print("texture_data Array:")
print("Array is of type: ", type(texture_data))
print("No. of dimensions: ", texture_data.ndim)
print("Shape of array: ", texture_data.shape)
print("Size of array: ", texture_data.size)
print("Array stores elements of type: ", texture_data.dtype)

with dpg.texture_registry(show=True):
dpg.add_raw_texture(frame.shape[1], frame.shape[0], texture_data, tag="texture_tag", format=dpg.mvFormat_Float_rgb)

with dpg.window(label="Example Window"):
dpg.add_text("Hello, world")
dpg.add_image("texture_tag")

dpg.show_metrics()
dpg.show_viewport()
while dpg.is_dearpygui_running():

# updating the texture in a while loop the frame rate will be limited to the camera frame rate.
# commenting out the "ret, frame = vid.read()" line will show the full speed that operations and updating a texture can run at

ret, frame = vid.read()
data = np.flip(frame, 2)
data = data.ravel()
data = np.asfarray(data, dtype='f')
texture_data = np.true_divide(data, 255.0)
dpg.set_value("texture_tag", texture_data)

# to compare to the base example in the open cv tutorials uncomment below
#cv.imshow('frame', frame)
dpg.render_dearpygui_frame()

vid.release()
#cv.destroyAllWindows() # when using upen cv window "imshow" call this also
dpg.destroy_context()
37 changes: 37 additions & 0 deletions plots/plot_with_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# see https://discord.com/channels/736279277242417272/1171570899888123934/1171570899888123934
# and see my1e6/dpg-examples/window/pop_to_window.py
import dearpygui.dearpygui as dpg
dpg.create_context()

def move_group(sender, app_data, user_data):
group, window = user_data
def close():
dpg.move_item(group, parent=window)
dpg.delete_item(new_window)
dpg.show_item(sender)
with dpg.window(on_close=close) as new_window:
dpg.move_item(group, parent=new_window)
dpg.hide_item(sender)


with dpg.window() as main_window:

with dpg.group() as my_group:
with dpg.plot():
dpg.add_plot_legend(location=dpg.mvPlot_Location_NorthEast)
dpg.add_plot_axis(dpg.mvXAxis)
with dpg.plot_axis(dpg.mvYAxis):
dpg.add_line_series([0, 1, 2, 3, 4], [0, 3, 4, 1, 5], label="line1")

with dpg.child_window(border=False, pos=(50,24), width=106, height=19): # The trick is you need to set the child window width and height to be exactly the size of the button. Any bigger and the extra space will cover the plot and prevent mouse inputs.
dpg.add_button(label="Pop to window!", callback=move_group, user_data=(my_group, main_window))

dpg.set_primary_window(main_window, True)
dpg.create_viewport(title='Plot buttons', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()



39 changes: 39 additions & 0 deletions window/status_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import dearpygui.dearpygui as dpg
dpg.create_context()

STATUS_BAR_HEIGHT = 20

with dpg.theme() as status_bar_theme:
with dpg.theme_component():
dpg.add_theme_color(dpg.mvThemeCol_WindowBg, (42, 123, 207, 255))
dpg.add_theme_style(dpg.mvStyleVar_WindowPadding, 4, 0)

def resize_primary_window():
x,y = dpg.get_item_rect_size(primary_window)
dpg.configure_item(status_bar, width=x)
dpg.configure_item(status_bar, pos=(0, y-STATUS_BAR_HEIGHT))

with dpg.window() as primary_window:
dpg.set_primary_window(primary_window, True)
with dpg.item_handler_registry() as registry:
dpg.add_item_resize_handler(callback=resize_primary_window)
dpg.bind_item_handler_registry(primary_window, registry)

with dpg.menu_bar():
with dpg.menu(label="View"):
dpg.add_menu_item(label="Show/hide status bar", callback=lambda: dpg.configure_item(status_bar, show=not dpg.is_item_shown(status_bar)))

with dpg.window():
dpg.add_button(label="Hello world")

with dpg.window(no_title_bar=True, no_move=True, no_resize=False) as status_bar:
dpg.bind_item_theme(status_bar, status_bar_theme)
with dpg.group(horizontal=True):
dpg.add_text("Hello")
dpg.add_button(label="world")

dpg.create_viewport(width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

0 comments on commit d9878ff

Please sign in to comment.