(Py)ImGui for Data Science
A set of utilities for data science using python, imgui, numpy and opencv.
- Free software: Apache Software License 2.0
- Documentation: https://imgui_datascience.readthedocs.io.
- Demo:
Compatible with python 2 and python3.
Acknowledgments
This library is based on the two following projects:
Dear ImGui : an amazing 'Immediate Mode GUI' C++ library
pyimgui : Python bindings for imgui (based on Cython).
imdebug, the image debugger :a neat image debugger / viewing by William Baxter has provided some ideas for the image_explorer feature.
Many thanks to their developers for their wonderful job.
Install & test:
- Code::
- git clone https://github.com/pthom/imgui_datascience.git cd imgui_datascience virtualenv env -p python3 source env/bin/activate pip install -r requirements.txt python run_example.py
Features
Display numpy.ndarray (aka opencv image)
The following types are supported : RGB, RGBA, GRAY, float32, float64
Code:
# returns mouse_position imgui_cv.image(img, height=150, title="flowers")
Display matplotlib figures
Code:
figure = matplotlib.pyplot.figure() x = numpy.arange(0.1, 100, 0.1) y = numpy.sin(x) / x plot.plot(x, y) imgui_fig.fig(figure, height=250, title="f(x) = sin(x) / x")
Inspect images
See https://www.youtube.com/watch?v=yKw7VaQNFCI&feature=youtu.be for an animated demo.
Code:
imgui_cv.image_explorer(img)
A simple way to run imgui programs
The simplest way to run a program a start adding gui buttons is shown below
Code:
def gui_loop(): imgui.button("Click me") def main(): imgui_runner.run(gui_loop, imgui_runner.Params())
A simple way to quickly inspect images
Below is the simplest to quickly display any type of numpy array (RGB, float, etc) and to be able to inspect it.
Code:
image = ... # cv2.imread("...") ImGuiImageLister.push_image("owl", image) ImGuiLister_ShowStandalone()
Full demo
You can run a full demo using either
- Case 1 (from pip install):
Code:
pip install imgui_datascience python -m imgui_datascience --example
- Case 2 (from checkout, with a virtualenv):
Code:
git clone https://github.com/pthom/imgui_datascience.git cd imgui_datascience virtualenv venv source venv/bin/activate pip install -r requirements.txt pip install -r requirements_dev.txt python run_example.py
- View the full demo (1'50") on youtube
click on the link below
https://www.youtube.com/watch?v=qstEZyLGsTQ&feature=youtu.be
Gotchas
Widget unique identifiers
Imgui identifies the widget through their label. If you have two buttons that have the same label, it might not differentiate them.
A workaround is to add "##" + an id after your label
Code:
if imgui.button("Click Me"): print("Clicked first button") if imgui.button("Click Me##2"): print("Clicked second button")
Another workaround is to use imgui_ext.make_unique_label
Code:
if imgui.button(imgui_ext.make_unique_label("Click Me")): print("Clicked first button") if imgui.button(imgui_ext.make_unique_label("Click Me")): print("Clicked second button")
OpenGL
This lib makes a heavy usage of OpenGL : it transfers the images from the RAM to you graphic card at each frame. The image textures are cached and only recreated if the image data has changed.
The library will detect that an image has changed by using a hash of its data. Two hash variant are possible :
- if imgui_cv.USE_FAST_HASH is set to True (which is default) : select 100 random pixels and hash them
- otherwise, compute the hash of the whole image data (using xxhash for performance)
You can change imgui_cv.USE_FAST_HASH value in order to change the behavior if needed.
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.