私的AI研究会 > PythonGUI4
Python プログラム作成に必要な GUI を試してみる。
cvuiは OpenCVのUIを描画する軽量ライブラリでパッケージのインストールなしで使える。
cvui の Python版は少し前までは「pip」によるインストールだったたようだが、現在は GitHub からモジュールで配布されているよう。結構優秀だと思うが活用例がなかなか見当たらない。
サンプルプログラムはたくさん付属しているが動かしてみないとどのようなものかはわからない。
というわけで、ダウンロードしたプログラムを再配置して実行してみるとともに、ソースコードも転載して、見やすくした。
~/workspace_py37/exercise/cvui$ tree . ├── Class1.py ├── Class2.py ├── button-shortcut.py ├── canny.py ├── complex-layout.py ├── fruits.jpg ├── hello-world.py ├── image-button.py ├── interaction-area.py ├── lena-face-gray.jpg ├── lena-face-red.jpg ├── lena-face.jpg ├── lena.jpg ├── main-app.py ├── mouse-complex-buttons.py ├── mouse-complex.py ├── mouse.py ├── multiple-files.py ├── multiple-windows-complex-dynamic.py ├── multiple-windows-complex-mouse.py ├── multiple-windows-complex.py ├── multiple-windows.py ├── nested-rows-columns.py ├── on-image.py ├── row-column.py ├── sparkline.csv ├── sparkline.py ├── trackbar-complex.py ├── trackbar-sparkline.py ├── trackbar.py ├── ui-enhanced-canny.py └── ui-enhanced-window-component.py
サンプルプログラムの実行
~/workspace_py37/exercise/cvui/~
# # This is a demo application to showcase keyboard shortcuts. # # Author: # Pascal Thomet # # Contributions: # Fernando Bevilacqua <dovyski@gmail.com> # # Copyright (c) 2018 Authors and Contributors # Code licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Button shortcut' def main(): frame = np.zeros((150, 650, 3), np.uint8) # Init cvui and tell it to use a value of 20 for cv2.waitKey() # because we want to enable keyboard shortcut for # all components, e.g. button with label "&Quit". # If cvui has a value for waitKey, it will call # waitKey() automatically for us within cvui.update(). cvui.init(WINDOW_NAME, 20); while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) cvui.text(frame, 40, 40, 'To exit this app click the button below or press Q (shortcut for the button below).') # Exit the application if the quit button was pressed. # It can be pressed because of a mouse click or because # the user pressed the "q" key on the keyboard, which is # marked as a shortcut in the button label ("&Quit"). if cvui.button(frame, 300, 80, "&Quit"): break # Since cvui.init() received a param regarding waitKey, # there is no need to call cv.waitKey() anymore. cvui.update() # will do it automatically. cvui.update() cv2.imshow(WINDOW_NAME, frame) if __name__ == '__main__': main()
# # This demo showcases some components of cvui being used to control # the application of the Canny edge algorithm to a loaded image. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'CVUI Canny Edge' def main(): lena = cv2.imread('lena.jpg', cv2.IMREAD_COLOR) frame = np.zeros(lena.shape, np.uint8) low_threshold = [50] high_threshold = [150] use_canny = [False] # Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Should we apply Canny edge? if use_canny[0]: # Yes, we should apply it. frame = cv2.cvtColor(lena, cv2.COLOR_BGR2GRAY) frame = cv2.Canny(frame, low_threshold[0], high_threshold[0], 3) frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) else: # No, so just copy the original image to the displaying frame. frame[:] = lena[:] # Render the settings window to house the checkbox # and the trackbars below. cvui.window(frame, 10, 50, 180, 180, 'Settings') # Checkbox to enable/disable the use of Canny edge cvui.checkbox(frame, 15, 80, 'Use Canny Edge', use_canny) # Two trackbars to control the low and high threshold values # for the Canny edge algorithm. cvui.trackbar(frame, 15, 110, 165, low_threshold, 5, 150) cvui.trackbar(frame, 15, 180, 165, high_threshold, 80, 300) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This application showcases how UI components can be placed # to create a more complex layout. The position of all components # is determined by a (x,y) pair prodived by the developer. # # Take a look at the "row-column" application to see how to use # automatic positioning by leveraging the begin*()/end*() API. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Complex layout' def group(frame, x, y, width, height): padding = 5 w = (width - padding) / 4 h = (height - 15 - padding) / 2 pos = cvui.Point(x + padding, y + 5) cvui.text(frame, pos.x, pos.y, 'Group title') pos.y += 15 cvui.window(frame, pos.x, pos.y, width - padding * 2, h - padding, 'Something') cvui.rect(frame, pos.x + 2, pos.y + 20, width - padding * 2 - 5, h - padding - 20, 0xff0000) pos.y += h cvui.window(frame, pos.x, pos.y, w / 3 - padding, h, 'Some') cvui.text(frame, pos.x + 25, pos.y + 60, '65', 1.1) pos.x += w / 3 cvui.window(frame, pos.x, pos.y, w / 3 - padding, h, 'Info') cvui.text(frame, pos.x + 25, pos.y + 60, '30', 1.1) pos.x += w / 3 cvui.window(frame, pos.x, pos.y, w / 3 - padding, h, 'Here') cvui.text(frame, pos.x + 25, pos.y + 60, '70', 1.1) pos.x += w / 3 cvui.window(frame, pos.x, pos.y, w - padding, h, 'And') cvui.rect(frame, pos.x + 2, pos.y + 22, w - padding - 5, h - padding - 20, 0xff0000) pos.x += w cvui.window(frame, pos.x, pos.y, w - padding, h, 'Here') cvui.rect(frame, pos.x + 2, pos.y + 22, w - padding - 5, h - padding - 20, 0xff0000) pos.x += w cvui.window(frame, pos.x, pos.y, w - padding, h, 'More info') cvui.rect(frame, pos.x + 2, pos.y + 22, w - padding - 5, h - padding - 20, 0xff0000) pos.x += w def main(): height = 220 spacing = 10 frame = np.zeros((height * 3, 1300, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) rows, cols, channels = frame.shape # Render three groups of components. group(frame, 0, 0, cols, height - spacing) group(frame, 0, height, cols, height - spacing) group(frame, 0, height * 2, cols, height - spacing) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is an extremely simple demo application to showcase the # basic structure, features and use of cvui. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'CVUI Hello World!' def main(): frame = np.zeros((200, 500, 3), np.uint8) count = 0; # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Buttons will return true if they were clicked, which makes # handling clicks a breeze. if (cvui.button(frame, 110, 80, "Hello, world!")): # The button was clicked, so let's increment our counter. count += 1 # Sometimes you want to show text that is not that simple, e.g. strings + numbers. # You can use cvui::printf for that. It accepts a variable number of parameter, pretty # much like printf does. # Let's show how many times the button has been clicked. cvui.printf(frame, 250, 90, 0.4, 0xff0000, "Button click count: %d", count); # Update cvui stuff and show everything on the screen cvui.imshow(WINDOW_NAME, frame); # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is a demo application to showcase the image button component. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Image button' def main(): frame = np.zeros((300, 600, 3), np.uint8) out = cv2.imread('lena-face.jpg', cv2.IMREAD_COLOR) down = cv2.imread('lena-face-red.jpg', cv2.IMREAD_COLOR) over = cv2.imread('lena-face-gray.jpg', cv2.IMREAD_COLOR) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Render an image-based button. You can provide images # to be used to render the button when the mouse cursor is # outside, over or down the button area. if cvui.button(frame, 200, 80, out, over, down): print('Image button clicked!') cvui.text(frame, 150, 200, 'This image behaves as a button') # Render a regular button. if cvui.button(frame, 360, 80, 'Button'): print('Regular button clicked!') # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is a demo application to showcase how you can use an # interaction area (iarea). An iarea can be used to track mouse # interactions over an specific space. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Interaction area' def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Render a rectangle on the screen. rectangle = cvui.Rect(50, 50, 100, 100) cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # Check what is the current status of the mouse cursor # regarding the previously rendered rectangle. status = cvui.iarea(rectangle.x, rectangle.y, rectangle.width, rectangle.height); # cvui::iarea() will return the current mouse status: # CLICK: mouse just clicked the interaction are # DOWN: mouse button was pressed on the interaction area, but not released yet. # OVER: mouse cursor is over the interaction area # OUT: mouse cursor is outside the interaction area if status == cvui.CLICK: print('Rectangle was clicked!') if status == cvui.DOWN: cvui.printf(frame, 240, 70, "Mouse is: DOWN") if status == cvui.OVER: cvui.printf(frame, 240, 70, "Mouse is: OVER") if status == cvui.OUT: cvui.printf(frame, 240, 70, "Mouse is: OUT") # Show the coordinates of the mouse pointer on the screen cvui.printf(frame, 240, 50, "Mouse pointer is at (%d,%d)", cvui.mouse().x, cvui.mouse().y) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is a demo application to showcase the UI components of cvui. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'CVUI Test' def main(): frame = np.zeros((300, 600, 3), np.uint8) checked = [False] checked2 = [True] count = [0] countFloat = [0.0] trackbarValue = [0.0] # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Show some pieces of text. cvui.text(frame, 50, 30, 'Hey there!') # You can also specify the size of the text and its color # using hex 0xRRGGBB CSS-like style. cvui.text(frame, 200, 30, 'Use hex 0xRRGGBB colors easily', 0.4, 0xff0000) # Sometimes you want to show text that is not that simple, e.g. strings + numbers. # You can use cvui.printf for that. It accepts a variable number of parameter, pretty # much like printf does. cvui.printf(frame, 200, 50, 0.4, 0x00ff00, 'Use printf formatting: %d + %.2f = %f', 2, 3.2, 5.2) # Buttons will return true if they were clicked, which makes # handling clicks a breeze. if cvui.button(frame, 50, 60, 'Button'): print('Button clicked') # If you do not specify the button width/height, the size will be # automatically adjusted to properly house the label. cvui.button(frame, 200, 70, 'Button with large label') # You can tell the width and height you want cvui.button(frame, 410, 70, 15, 15, 'x') # Window components are useful to create HUDs and similars. At the # moment, there is no implementation to constraint content within a # a window. cvui.window(frame, 50, 120, 120, 100, 'Window') # The counter component can be used to alter int variables. Use # the 4th parameter of the function to point it to the variable # to be changed. cvui.counter(frame, 200, 120, count) # Counter can be used with doubles too. You can also specify # the counter's step (how much it should change # its value after each button press), as well as the format # used to print the value. cvui.counter(frame, 320, 120, countFloat, 0.1, '%.1f') # The trackbar component can be used to create scales. # It works with all numerical types (including chars). cvui.trackbar(frame, 420, 110, 150, trackbarValue, 0., 50.) # Checkboxes also accept a pointer to a variable that controls # the state of the checkbox (checked or not). cvui.checkbox() will # automatically update the value of the boolean after all # interactions, but you can also change it by yourself. Just # do "checked = [True]" somewhere and the checkbox will change # its appearance. cvui.checkbox(frame, 200, 160, 'Checkbox', checked) cvui.checkbox(frame, 200, 190, 'A checked checkbox', checked2) # Display the lib version at the bottom of the screen cvui.printf(frame, 600 - 80, 300 - 20, 0.4, 0xCECECE, 'cvui v.%s', cvui.VERSION) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is application shows the mouse API of cvui. It shows a rectangle on the # screen everytime the user clicks and drags the mouse cursor around. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Mouse' def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME); # Rectangle to be rendered according to mouse interactions. rectangle = cvui.Rect(0, 0, 0, 0) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Show the coordinates of the mouse pointer on the screen cvui.text(frame, 10, 30, 'Click (any) mouse button and drag the pointer around to select an area.') cvui.printf(frame, 10, 50, 'Mouse pointer is at (%d,%d)', cvui.mouse().x, cvui.mouse().y) # The function "bool cvui.mouse(int query)" allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for a single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for a single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for a single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the rectangle at the mouse pointer. rectangle.x = cvui.mouse().x rectangle.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust rectangle dimensions according to mouse pointer rectangle.width = cvui.mouse().x - rectangle.x rectangle.height = cvui.mouse().y - rectangle.y # Show the rectangle coordinates and size cvui.printf(frame, rectangle.x + 5, rectangle.y + 5, 0.3, 0xff0000, '(%d,%d)', rectangle.x, rectangle.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', rectangle.width, rectangle.height) # Did any mouse button go up? if cvui.mouse(cvui.UP): # Hide the rectangle rectangle.x = 0 rectangle.y = 0 rectangle.width = 0 rectangle.height = 0 # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.CLICK): cvui.text(frame, 10, 70, 'Mouse was clicked!') # Render the rectangle cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc, then # shows the frame in a window like cv2.imshow() does. cvui.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is application uses the mouse API to dynamically create a ROI # for image visualization. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Mouse - ROI interaction' ROI_WINDOW = 'ROI' def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchor = cvui.Point() roi = cvui.Rect(0, 0, 0, 0) working = False # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text(frame, 10, 10, 'Click (any) mouse button and drag the pointer around to select a ROI.') # The function 'bool cvui.mouse(int query)' allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Inform we are working, so the ROI window is not updated every frame working = True # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, 0xff0000, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', roi.width, roi.height) # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.UP): # We are done working with the ROI. working = False # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lena.cols - (roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lena.rows - (roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # Render the roi cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # If the ROI is valid, show it. if roi.area() > 0 and working == False: lenaRoi = lena[roi.y : roi.y + roi.height, roi.x : roi.x + roi.width] cv2.imshow(ROI_WINDOW, lenaRoi) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is application uses the fine details of the mouse API to dynamically # create ROIs for image visualization based on different mouse buttons. # # This demo is very similiar to 'mouse-complex', except that it handles 3 ROIs, # one for each mouse button. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Mouse complex buttons- ROI interaction' def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchors = [cvui.Point() for i in range(3)] # one anchor for each mouse button rois = [cvui.Rect() for i in range(3)] # one ROI for each mouse button colors = [0xff0000, 0x00ff00, 0x0000ff] # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text(frame, 10, 10, 'Click (any) mouse button then drag the pointer around to select a ROI.') cvui.text(frame, 10, 25, 'Use different mouse buttons (right, middle and left) to select different ROIs.') # Iterate all mouse buttons (left, middle and right button) button = cvui.LEFT_BUTTON while button <= cvui.RIGHT_BUTTON: # Get the anchor, ROI and color associated with the mouse button anchor = anchors[button] roi = rois[button] color = colors[button] # The function 'bool cvui.mouse(int button, int query)' allows you to query a particular mouse button for events. # E.g. cvui.mouse(cvui.RIGHT_BUTTON, cvui.DOWN) # # Available queries: # - cvui.DOWN: mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did the mouse button go down? if cvui.mouse(button, cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(button, cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, color, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, color, 'w:%d, h:%d', roi.width, roi.height) # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lenaCols - (roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lenaRows - (roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # If the ROI is valid, render it in the frame and show in a window. if roi.area() > 0: cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, color) cvui.printf(frame, roi.x + 5, roi.y - 10, 0.3, color, 'ROI %d', button) lenaRoi = lena[roi.y : roi.y + roi.height, roi.x : roi.x + roi.width] cv2.imshow('ROI button' + str(button), lenaRoi) button += 1 # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is application demonstrates how you can use cvui when your project has # multiple files that use cvui. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui # Import the two dummy external files from Class1 import Class1 from Class2 import Class2 WINDOW_NAME = 'CVUI Multiple files' def main(): frame = np.zeros((300, 500, 3), np.uint8) c1 = Class1() c2 = Class2() # Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) c1.renderInfo(frame) c2.renderMessage(frame) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
""" This demo shows how you can use cvui in an application that uses more than one OpenCV window. When using cvui with multiple OpenCV windows, you must call cvui component calls between cvui.contex(NAME) and cvui.update(NAME), where NAME is the name of the window. That way, cviu knows which window you are using (NAME in this case), so it can track mouse events, for instance. E.g. # Code for window 'window1'. cvui.context('window1') cviu.text(frame, ...) cviu.button(frame, ...) cviu.update('window1') # somewhere else, code for 'window2' cvui.context('window2') cviu.printf(frame, ...) cviu.printf(frame, ...) cviu.update('window2') # Show everything in a window cv2.imshow(frame) Pay attention to the pair cvui.context(NAME)/cviu.update(NAME), which encloses the component calls for that window. You need such pair for each window of your application. After calling cvui.update(), you can show the result in a window using cv2.imshow(). If you want to save some typing, you can use cvui.imshow(), which calls cvui.update() for you and then shows the frame in a window. E.g. # Code for window 'window1'. cvui.context('window1') cviu.text(frame, ...) cviu.button(frame, ...) cviu.imshow('window1') # somewhere else, code for 'window2' cvui.context('window2') cviu.printf(frame, ...) cviu.printf(frame, ...) cviu.imshow('window2') In that case, you don't have to bother calling cvui.update() yourself, since cvui.imshow() will do it for you. Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> Licensed under the MIT license. """ import numpy as np import cv2 import cvui WINDOW1_NAME = 'Window 1' WINDOW2_NAME = 'Windows 2' WINDOW3_NAME = 'Windows 3' WINDOW4_NAME = 'Windows 4' # Update a window using cvui functions, then show it using cv2.imshow(). def window(name): # Create a frame for this window and fill it with a nice color frame = np.zeros((200, 500, 3), np.uint8) frame[:] = (49, 52, 49) # Inform cvui that the components to be rendered from now one belong to # a window in particular. # # If you don't inform that, cvui will assume the components belong to # the default window (informed in cvui.init()). In that case, the # interactions with all other windows being used will not work. cvui.context(name) # Show info regarding the window cvui.printf(frame, 110, 50, '%s - click the button', name) # Buttons return true if they are clicked if cvui.button(frame, 110, 90, 'Button'): cvui.printf(frame, 200, 95, 'Button clicked!') print('Button clicked on: ', name) # Tell cvui to update its internal structures regarding a particular window. # # If cvui is being used in multiple windows, you need to enclose all component # calls between the pair cvui.context(NAME)/cvui.update(NAME), where NAME is # the name of the window being worked on. cvui.update(name) # Show the content of this window on the screen cvui.imshow(name, frame) # Update and show a window in a single call using cvui.imshow(). def compact(name): # Create a frame for this window and fill it with a nice color frame = np.zeros((200, 500, 3), np.uint8) frame[:] = (49, 52, 49) # Inform cvui that the components to be rendered from now one belong to # a window in particular. # # If you don't inform that, cvui will assume the components belong to # the default window (informed in cvui.init()). In that case, the # interactions with all other windows being used will not work. cvui.context(name) cvui.printf(frame, 110, 50, '%s - click the button', name) if cvui.button(frame, 110, 90, 'Button'): cvui.printf(frame, 200, 95, 'Button clicked!') print('Button clicked on: ', name) # Tell cvui to update its internal structures regarding a particular window # then show it. Below we are using cvui.imshow(), which is cvui's version of # the existing cv2.imshow(). They behave exactly the same, the only difference # is that cvui.imshow() will automatically call cvui.update(name) for you. cvui.imshow(name, frame) def main(): # Init cvui. If you don't tell otherwise, cvui will create the required OpenCV # windows based on the list of names you provided. windows = [WINDOW1_NAME, WINDOW2_NAME, WINDOW3_NAME, WINDOW4_NAME] cvui.init(windows, 4) while (True): # The functions below will update a window and show them using cv2.imshow(). # In that case, you must call the pair cvui.context(NAME)/cvui.update(NAME) # to render components and update the window. window(WINDOW1_NAME) window(WINDOW2_NAME) window(WINDOW3_NAME) # The function below will do the same as the funcitons above, however it will # use cvui.imshow() (cvui's version of cv2.imshow()), which will automatically # call cvui.update() for us. compact(WINDOW4_NAME) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This demo shows how to use cvui in multiple windows relying on rows and columns. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui import random WINDOW1_NAME = 'Window 1' WINDOW2_NAME = 'Windows 2' def main(): # We have one mat for each window. frame1 = np.zeros((600, 800, 3), np.uint8) frame2 = np.zeros((600, 800, 3), np.uint8) # Create variables used by some components window1_values = [] window2_values = [] window1_checked = [False] window1_checked2 = [False] window2_checked = [False] window2_checked2 = [False] window1_value = [1.0] window1_value2 = [1.0] window1_value3 = [1.0] window2_value = [1.0] window2_value2 = [1.0] window2_value3 = [1.0] img = cv2.imread('lena-face.jpg', cv2.IMREAD_COLOR) imgRed = cv2.imread('lena-face-red.jpg', cv2.IMREAD_COLOR) imgGray = cv2.imread('lena-face-gray.jpg', cv2.IMREAD_COLOR) padding = 10 # Fill the vector with a few random values for i in range(0, 20): window1_values.append(random.uniform(0., 300.0)) window2_values.append(random.uniform(0., 300.0)) # Start two OpenCV windows cv2.namedWindow(WINDOW1_NAME) cv2.namedWindow(WINDOW2_NAME) # Init cvui and inform it to use the first window as the default one. # cvui.init() will automatically watch the informed window. cvui.init(WINDOW1_NAME) # Tell cvui to keep track of mouse events in window2 as well. cvui.watch(WINDOW2_NAME) while (True): # Inform cvui that all subsequent component calls and events are related to window 1. cvui.context(WINDOW1_NAME) # Fill the frame with a nice color frame1[:] = (49, 52, 49) cvui.beginRow(frame1, 10, 20, 100, 50) cvui.text('This is ') cvui.printf('a row') cvui.checkbox('checkbox', window1_checked) cvui.window(80, 80, 'window') cvui.rect(50, 50, 0x00ff00, 0xff0000) cvui.sparkline(window1_values, 50, 50) cvui.counter(window1_value) cvui.button(100, 30, 'Fixed') cvui.image(img) cvui.button(img, imgGray, imgRed) cvui.endRow() padding = 50 cvui.beginRow(frame1, 10, 150, 100, 50, padding) cvui.text('This is ') cvui.printf('another row') cvui.checkbox('checkbox', window1_checked2) cvui.window(80, 80, 'window') cvui.button(100, 30, 'Fixed') cvui.printf('with 50px paddin7hg.') cvui.endRow() cvui.beginRow(frame1, 10, 250, 100, 50) cvui.text('This is ') cvui.printf('another row with a trackbar ') cvui.trackbar(150, window1_value2, 0., 5.) cvui.printf(' and a button ') cvui.button(100, 30, 'button') cvui.endRow() cvui.beginColumn(frame1, 50, 330, 100, 200) cvui.text('Column 1 (no padding)') cvui.button('button1') cvui.button('button2') cvui.text('End of column 1') cvui.endColumn() padding = 10 cvui.beginColumn(frame1, 300, 330, 100, 200, padding) cvui.text('Column 2 (padding = 10)') cvui.button('button1') cvui.button('button2') cvui.trackbar(150, window1_value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25) cvui.text('End of column 2') cvui.endColumn() cvui.beginColumn(frame1, 550, 330, 100, 200) cvui.text('Column 3 (use space)') cvui.space(5) cvui.button('button1 5px below') cvui.space(50) cvui.text('Text 50px below') cvui.space(20) cvui.button('Button 20px below') cvui.space(40) cvui.text('End of column 2 (40px below)') cvui.endColumn() # Update all components of window1, e.g. mouse clicks, and show it. cvui.update(WINDOW1_NAME) cv2.imshow(WINDOW1_NAME, frame1) # From this point on, we are going to render the second window. We need to inform cvui # that all updates and components from now on are connected to window 2. # We do that by calling cvui.context(). cvui.context(WINDOW2_NAME) frame2[:] = (49, 52, 49) cvui.beginRow(frame2, 10, 20, 100, 50) cvui.text('This is ') cvui.printf('a row') cvui.checkbox('checkbox', window2_checked) cvui.window(80, 80, 'window') cvui.rect(50, 50, 0x00ff00, 0xff0000) cvui.sparkline(window2_values, 50, 50) cvui.counter(window2_value) cvui.button(100, 30, 'Fixed') cvui.image(img) cvui.button(img, imgGray, imgRed) cvui.endRow() padding = 50 cvui.beginRow(frame2, 10, 150, 100, 50, padding) cvui.text('This is ') cvui.printf('another row') cvui.checkbox('checkbox', window2_checked2) cvui.window(80, 80, 'window') cvui.button(100, 30, 'Fixed') cvui.printf('with 50px paddin7hg.') cvui.endRow() # Another row mixing several components cvui.beginRow(frame2, 10, 250, 100, 50) cvui.text('This is ') cvui.printf('another row with a trackbar ') cvui.trackbar(150, window2_value2, 0., 5.) cvui.printf(' and a button ') cvui.button(100, 30, 'button') cvui.endRow() cvui.beginColumn(frame2, 50, 330, 100, 200) cvui.text('Column 1 (no padding)') cvui.button('button1') cvui.button('button2') cvui.text('End of column 1') cvui.endColumn() padding = 10 cvui.beginColumn(frame2, 300, 330, 100, 200, padding) cvui.text('Column 2 (padding = 10)') cvui.button('button1') cvui.button('button2') cvui.trackbar(150, window2_value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25) cvui.text('End of column 2') cvui.endColumn() cvui.beginColumn(frame2, 550, 330, 100, 200) cvui.text('Column 3 (use space)') cvui.space(5) cvui.button('button1 5px below') cvui.space(50) cvui.text('Text 50px below') cvui.space(20) cvui.button('Button 20px below') cvui.space(40) cvui.text('End of column 2 (40px below)') cvui.endColumn() # Update all components of window2, e.g. mouse clicks, and show it. cvui.update(WINDOW2_NAME) cv2.imshow(WINDOW2_NAME, frame2) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This demo shows how to use cvui in multiple windows, using rows and colums. # Additionally a custom error window (an OpenCV window) is dynamically opened/closed # based on UI buttons. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui import random GUI_WINDOW1_NAME = 'Window 1' GUI_WINDOW2_NAME = 'Windows 2' ERROR_WINDOW_NAME = 'Error window' # Check if an OpenCV window is open. # From: https:#stackoverflow.com/a/48055987/29827 def isWindowOpen(name): return cv2.getWindowProperty(name, cv2.WND_PROP_AUTOSIZE) != -1 # Open a new OpenCV window and watch it using cvui def openWindow(name): cv2.namedWindow(name) cvui.watch(name) # Open an OpenCV window def closeWindow(name): cv2.destroyWindow(name) # Ensure OpenCV window event queue is processed, otherwise the window # will not be closed. cv2.waitKey(1) def main(): # We have one mat for each window. frame1 = np.zeros((150, 600, 3), np.uint8) frame2 = np.zeros((150, 600, 3), np.uint8) error_frame = np.zeros((100, 300, 3), np.uint8) # Flag to control if we should show an error window. error = False # Create two OpenCV windows cv2.namedWindow(GUI_WINDOW1_NAME) cv2.namedWindow(GUI_WINDOW2_NAME) # Init cvui and inform it to use the first window as the default one. # cvui.init() will automatically watch the informed window. cvui.init(GUI_WINDOW1_NAME) # Tell cvui to keep track of mouse events in window2 as well. cvui.watch(GUI_WINDOW2_NAME) while (True): # Inform cvui that all subsequent component calls and events are related to window 1. cvui.context(GUI_WINDOW1_NAME) # Fill the frame with a nice color frame1[:] = (49, 52, 49) cvui.beginColumn(frame1, 50, 20, -1, -1, 10) cvui.text('[Win1] Use the buttons below to control the error window') if cvui.button('Close'): closeWindow(ERROR_WINDOW_NAME) # If the button is clicked, we open the error window. # The content and rendering of such error window will be performed # after we handled all other windows. if cvui.button('Open'): error = True openWindow(ERROR_WINDOW_NAME) cvui.endColumn() # Update all components of window1, e.g. mouse clicks, and show it. cvui.update(GUI_WINDOW1_NAME) cv2.imshow(GUI_WINDOW1_NAME, frame1) # From this point on, we are going to render the second window. We need to inform cvui # that all updates and components from now on are connected to window 2. # We do that by calling cvui.context(). cvui.context(GUI_WINDOW2_NAME) frame2[:] = (49, 52, 49) cvui.beginColumn(frame2, 50, 20, -1, -1, 10) cvui.text('[Win2] Use the buttons below to control the error window') if cvui.button('Close'): closeWindow(ERROR_WINDOW_NAME) # If the button is clicked, we open the error window. # The content and rendering of such error window will be performed # after we handled all other windows. if cvui.button('Open'): openWindow(ERROR_WINDOW_NAME) error = True cvui.endColumn() # Update all components of window2, e.g. mouse clicks, and show it. cvui.update(GUI_WINDOW2_NAME) cv2.imshow(GUI_WINDOW2_NAME, frame2) # Handle the content and rendering of the error window, # if we have un active error and the window is actually open. if error and isWindowOpen(ERROR_WINDOW_NAME): # Inform cvui that all subsequent component calls and events are # related to the error window from now on cvui.context(ERROR_WINDOW_NAME) # Fill the error window if a vibrant color error_frame[:] = (10, 10, 49) cvui.text(error_frame, 70, 20, 'This is an error message', 0.4, 0xff0000) if cvui.button(error_frame, 110, 40, 'Close'): error = False if error: # We still have an active error. # Update all components of the error window, e.g. mouse clicks, and show it. cvui.update(ERROR_WINDOW_NAME) cv2.imshow(ERROR_WINDOW_NAME, error_frame) else: # No more active error. Let's close the error window. closeWindow(ERROR_WINDOW_NAME) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This demo shows how to use cvui in multiple windows, accessing information about # the mouse cursor on each window. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW1_NAME = 'Window 1' WINDOW2_NAME = 'Windows 2' WINDOW3_NAME = 'Windows 3' def main(): # We have one mat for each window. frame1 = np.zeros((200, 500, 3), np.uint8) frame2 = np.zeros((200, 500, 3), np.uint8) frame3 = np.zeros((200, 500, 3), np.uint8) # Init cvui, instructing it to create 3 OpenCV windows. windows = [WINDOW1_NAME, WINDOW2_NAME, WINDOW3_NAME] cvui.init(windows, 3) while (True): # clear all frames frame1[:] = (49, 52, 49) frame2[:] = (49, 52, 49) frame3[:] = (49, 52, 49) # Inform cvui that all subsequent component calls and events are related to window 1. # We do that by calling cvui.context(). cvui.context(WINDOW1_NAME) cvui.printf(frame1, 10, 10, 'In window1, mouse is at: %d,%d (obtained from window name)', cvui.mouse(WINDOW1_NAME).x, cvui.mouse(WINDOW1_NAME).y) if cvui.mouse(WINDOW1_NAME, cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame1, 10, 30, 'In window1, mouse LEFT_BUTTON is DOWN') cvui.imshow(WINDOW1_NAME, frame1) # From this point on, we are going to render the second window. We need to inform cvui # that all updates and components from now on are connected to window 2. cvui.context(WINDOW2_NAME) cvui.printf(frame2, 10, 10, 'In window2, mouse is at: %d,%d (obtained from context)', cvui.mouse().x, cvui.mouse().y) if cvui.mouse(cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame2, 10, 30, 'In window2, mouse LEFT_BUTTON is DOWN') cvui.imshow(WINDOW2_NAME, frame2) # Finally we are going to render the thrid window. Again we need to inform cvui # that all updates and components from now on are connected to window 3. cvui.context(WINDOW3_NAME) cvui.printf(frame3, 10, 10, 'In window1, mouse is at: %d,%d', cvui.mouse(WINDOW1_NAME).x, cvui.mouse(WINDOW1_NAME).y) cvui.printf(frame3, 10, 30, 'In window2, mouse is at: %d,%d', cvui.mouse(WINDOW2_NAME).x, cvui.mouse(WINDOW2_NAME).y) cvui.printf(frame3, 10, 50, 'In window3, mouse is at: %d,%d', cvui.mouse(WINDOW3_NAME).x, cvui.mouse(WINDOW3_NAME).y) if cvui.mouse(WINDOW1_NAME, cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame3, 10, 90, 'window1: LEFT_BUTTON is DOWN') if cvui.mouse(WINDOW2_NAME, cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame3, 10, 110, 'window2: LEFT_BUTTON is DOWN') if cvui.mouse(WINDOW3_NAME, cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame3, 10, 130, 'window3: LEFT_BUTTON is DOWN') cvui.imshow(WINDOW3_NAME, frame3) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This application demonstrates the use of begin*()/end*() to # create nested rows/columns. Check the 'row-column' app to # understand automatic positioning and to check a simpler # use of begin*()/end*(). # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui import random WINDOW_NAME = 'Nested rows and columns' def main(): frame = np.zeros((600, 800, 3), np.uint8) values = [] checked = [False] value = [1.0] # Fill the vector with a few random values for i in range(0, 20): values.append(random.uniform(0., 300.0)) # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Define a row at position (10, 50) with width 100 and height 150. cvui.beginRow(frame, 10, 50, 100, 150) # The components below will be placed one beside the other. cvui.text('Row starts') cvui.button('here') # When a column or row is nested within another, it behaves like # an ordinary component with the specified size. In this case, # let's create a column with width 100 and height 50. The # next component added will behave like it was added after # a component with width 100 and heigth 150. cvui.beginColumn(100, 150) cvui.text('Column 1') cvui.button('button1') cvui.button('button2') cvui.button('button3') cvui.text('End of column 1') cvui.endColumn() # Add two pieces of text cvui.text('Hi again,') cvui.text('its me!') # Start a new column cvui.beginColumn(100, 50) cvui.text('Column 2') cvui.button('button1') cvui.button('button2') cvui.button('button3') cvui.space() cvui.text('Another text') cvui.space(40) cvui.text('End of column 2') cvui.endColumn() # Add more text cvui.text('this is the ') cvui.text('end of the row!') cvui.endRow() # Here is another nested row/column cvui.beginRow(frame, 50, 300, 100, 150) # If you don't want to calculate the size of any row/column WITHIN # a begin*()/end*() block, just use negative width/height when # calling beginRow() or beginColumn() (or don't provide width/height at all!) # For instance, the following column will have its width/height # automatically adjusted according to its content. cvui.beginColumn() cvui.text('Column 1') cvui.button('button with very large label') cvui.text('End of column 1') cvui.endColumn() # Add two pieces of text cvui.text('Hi again,') cvui.text('its me!') # Start a new column cvui.beginColumn() cvui.text('Column 2') cvui.button('btn') cvui.space() cvui.text('text') cvui.button('btn2') cvui.text('text2') if cvui.button('&Quit'): break cvui.endColumn() # Add more text cvui.text('this is the ') cvui.text('end of the row!') cvui.endRow() # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is a demo application to showcase the UI components of cvui. # # Author: # Pascal Thomet # # Contributions: # Fernando Bevilacqua <dovyski@gmail.com> # # Copyright (c) 2018 Authors and Contributors # Code licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'CVUI Test' def main(): lena = cv2.imread('lena.jpg', cv2.IMREAD_COLOR) frame = np.zeros(lena.shape, np.uint8) doubleBuffer = np.zeros(lena.shape, np.uint8) trackbarWidth = 130 # Adjustments values for RGB and HSV rgb = [[1.], [1.], [1]] hsv = [[1.], [1.], [1]] # Copy the loaded image to the buffer doubleBuffer[:] = lena[:] # Init cvui and tell it to use a value of 20 for cv2.waitKey() # because we want to enable keyboard shortcut for # all components, e.g. button with label '&Quit'. # If cvui has a value for waitKey, it will call # waitKey() automatically for us within cvui.update(). cvui.init(WINDOW_NAME, 20) while (True): frame[:] = doubleBuffer[:] frameRows,frameCols,frameChannels = frame.shape # Exit the application if the quit button was pressed. # It can be pressed because of a mouse click or because # the user pressed the 'q' key on the keyboard, which is # marked as a shortcut in the button label ('&Quit'). if cvui.button(frame, frameCols - 100, frameRows - 30, '&Quit'): break # RGB HUD cvui.window(frame, 20, 50, 180, 240, 'RGB adjust') # Within the cvui.beginColumns() and cvui.endColumn(), # all elements will be automatically positioned by cvui. # In a columns, all added elements are vertically placed, # one under the other (from top to bottom). # # Notice that all component calls within the begin/end block # below DO NOT have (x,y) coordinates. # # Let's create a row at position (35,80) with automatic # width and height, and a padding of 10 cvui.beginColumn(frame, 35, 80, -1, -1, 10) rgbModified = False # Trackbar accept a pointer to a variable that controls their value # They return true upon edition if cvui.trackbar(trackbarWidth, rgb[0], 0., 2., 2, '%3.02Lf'): rgbModified = True if cvui.trackbar(trackbarWidth, rgb[1], 0., 2., 2, '%3.02Lf'): rgbModified = True if cvui.trackbar(trackbarWidth, rgb[2], 0., 2., 2, '%3.02Lf'): rgbModified = True cvui.space(2) cvui.printf(0.35, 0xcccccc, ' RGB: %3.02lf,%3.02lf,%3.02lf', rgb[0][0], rgb[1][0], rgb[2][0]) if (rgbModified): b,g,r = cv2.split(lena) b = (b * rgb[2][0]).astype(np.float) g = (g * rgb[1][0]).astype(np.float) r = (r * rgb[0][0]).astype(np.float) doubleBuffer = cv2.merge((b,g,r), doubleBuffer) cvui.endColumn() # HSV lenaRows,lenaCols,lenaChannels = lena.shape cvui.window(frame, lenaCols - 200, 50, 180, 240, 'HSV adjust') cvui.beginColumn(frame, lenaCols - 180, 80, -1, -1, 10) hsvModified = False if cvui.trackbar(trackbarWidth, hsv[0], 0., 2., 2, '%3.02Lf'): hsvModified = True if cvui.trackbar(trackbarWidth, hsv[1], 0., 2., 2, '%3.02Lf'): hsvModified = True if cvui.trackbar(trackbarWidth, hsv[2], 0., 2., 2, '%3.02Lf'): hsvModified = True cvui.space(2) cvui.printf(0.35, 0xcccccc, ' HSV: %3.02lf,%3.02lf,%3.02lf', hsv[0][0], hsv[1][0], hsv[2][0]) if hsvModified: hsvMat = cv2.cvtColor(lena, cv2.COLOR_BGR2HSV) h,s,v = cv2.split(hsvMat) h = (h * hsv[0][0]).astype(np.float) s = (s * hsv[1][0]).astype(np.float) v = (v * hsv[2][0]).astype(np.float) hsvMat = cv2.merge((h,s,v), hsvMat) hsvMat = np.uint8(hsvMat) doubleBuffer = cv2.cvtColor(hsvMat, cv2.COLOR_HSV2BGR) cvui.endColumn() # Display the lib version at the bottom of the screen cvui.printf(frame, frameCols - 300, frameRows - 20, 0.4, 0xCECECE, 'cvui v.%s', cvui.VERSION) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. # # Since cvui.init() received a param regarding waitKey, # there is no need to call cv2.waitKey() anymore. cvui.update() # will do it automatically. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) if __name__ == '__main__': main()
# # One of the most annoying tasks when building UI is to calculate # where each component should be placed on the screen. cvui has # a set of methods that abstract the process of positioning # components, so you don't have to think about assigning a # X and Y coordinate. Instead you just add components and cvui # will place them as you go. # # In order to use that approach, you must use begin*()/end*(). # You use begin*() to start a group of elements, then you add # the components you want (without the X and Y parameters, e.g. # text('Hi') instead of text(50, 35, 'Hi')), then you finish # the group by calling end*(). # # You can create rows (beginRow()/endRow()) and columns # (beginColumn()/endColumn()). This application uses rows and # columns separately, but you can nest them as you want (take # a look at the 'nested-rows-columns' app). # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui import random WINDOW_NAME = 'Rows and Columns' def main(): frame = np.zeros((600, 800, 3), np.uint8) # Create variables used by some components values = [] checked = [False] checked2 = [False] value = [1.0] value2 = [1.0] value3 = [1.0] padding = 10 img = cv2.imread('lena-face.jpg', cv2.IMREAD_COLOR) imgRed = cv2.imread('lena-face-red.jpg', cv2.IMREAD_COLOR) imgGray = cv2.imread('lena-face-gray.jpg', cv2.IMREAD_COLOR) # Fill the vector with a few random values for i in range(0, 20): values.append(random.uniform(0., 300.0)) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # In a row, all added elements are # horizontally placed, one next the other (from left to right) # # Within the cvui.beginRow() and cvui.endRow(), # all elements will be automatically positioned by cvui. # # Notice that all component calls within the begin/end block # DO NOT have (x,y) coordinates. # # Let's create a row at position (10,20) with width 100 and height 50. cvui.beginRow(frame, 10, 20, 100, 50) cvui.text('This is ') cvui.printf('a row') cvui.checkbox('checkbox', checked) cvui.window(80, 80, 'window') cvui.rect(50, 50, 0x00ff00, 0xff0000); cvui.sparkline(values, 50, 50); cvui.counter(value) cvui.button(100, 30, 'Fixed') cvui.image(img) cvui.button(img, imgGray, imgRed) cvui.endRow() # Here is another row, this time with a padding of 50px among components. padding = 50; cvui.beginRow(frame, 10, 150, 100, 50, padding) cvui.text('This is ') cvui.printf('another row') cvui.checkbox('checkbox', checked2) cvui.window(80, 80, 'window') cvui.button(100, 30, 'Fixed') cvui.printf('with 50px padding.') cvui.endRow() # Another row mixing several components cvui.beginRow(frame, 10, 250, 100, 50) cvui.text('This is ') cvui.printf('another row with a trackbar ') #cvui.trackbar(150, &value2, 0., 5.); cvui.printf(' and a button ') cvui.button(100, 30, 'button') cvui.endRow() # In a column, all added elements are vertically placed, # one below the other, from top to bottom. Let's create # a column at (50, 300) with width 100 and height 200. cvui.beginColumn(frame, 50, 330, 100, 200) cvui.text('Column 1 (no padding)') cvui.button('button1') cvui.button('button2') cvui.text('End of column 1') cvui.endColumn() # Here is another column, using a padding value of 10, # which will add an space of 10px between each component. padding = 10 cvui.beginColumn(frame, 300, 330, 100, 200, padding) cvui.text('Column 2 (padding = 10)') cvui.button('button1') cvui.button('button2') #cvui.trackbar(150, &value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25); cvui.text('End of column 2') cvui.endColumn() # You can also add an arbitrary amount of space between # components by calling cvui.space(). # # cvui.space() is aware of context, so if it is used # within a beginColumn()/endColumn() block, the space will # be vertical. If it is used within a beginRow()/endRow() # block, space will be horizontal. cvui.beginColumn(frame, 550, 330, 100, 200) cvui.text('Column 3 (use space)') # Add 5 pixels of (vertical) space. cvui.space(5) cvui.button('button1 5px below') # Add 50 pixels of (vertical) space. cvui.space(50) cvui.text('Text 50px below') # Add 20 pixels of (vertical) space. cvui.space(20) cvui.button('Button 20px below') # Add 40 pixels of (vertical) space. cvui.space(40) cvui.text('End of column 2 (40px below)') cvui.endColumn() # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is a demo application to showcase the sparkline components of cvui. # Sparklines are quite useful to display data, e.g. FPS charts. # # Copyright (c) 2018 Fernando Bevilacqua <dovyski@gmail.com> # Licensed under the MIT license. # import numpy as np import cv2 import cvui import random WINDOW_NAME = 'Sparkline' def load(thePath): file = open(thePath, 'r') if file == False: print('Unable to open file: ', thePath) sys.exit(-1) data = [] line = file.readline() while line: parts = line.split('\t') data.append(float(parts[1])) line = file.readline() return data def main(): frame = np.zeros((600, 800, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) # Load some data points from a file points = load('sparkline.csv') # Create less populated sets few_points = [] no_points = [] for i in range(0, 30): few_points.append(random.uniform(0., 300.0)) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Add 3 sparklines that are displaying the same data, but with # different width/height/colors. cvui.sparkline(frame, points, 0, 0, 800, 200); cvui.sparkline(frame, points, 0, 200, 800, 100, 0xff0000); cvui.sparkline(frame, points, 0, 300, 400, 100, 0x0000ff); # Add a sparkline with few points cvui.sparkline(frame, few_points, 10, 400, 790, 80, 0xff00ff); # Add a sparkline that has no data. In that case, cvui will # render it with a visual warning. cvui.sparkline(frame, no_points, 10, 500, 750, 100, 0x0000ff); # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is a demo application to showcase the trackbar component. # # Author: # Pascal Thomet # # Contributions: # Fernando Bevilacqua <dovyski@gmail.com> # # Copyright (c) 2018 Authors and Contributors # Code licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Trackbar' def main(): intValue = [30] ucharValue = [30] charValue = [30] floatValue = [12.] doubleValue = [45.] doubleValue2 = [15.] doubleValue3 = [10.3] frame = np.zeros((770, 350, 3), np.uint8) # The width of all trackbars used in this example. width = 300 # The x position of all trackbars used in this example x = 10 # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # The trackbar component uses templates to guess the type of its arguments. # You have to be very explicit about the type of the value, the min and # the max params. For instance, if they are double, use 100.0 instead of 100. cvui.text(frame, x, 10, 'double, step 1.0 (default)') cvui.trackbar(frame, x, 40, width, doubleValue, 0., 100.) cvui.text(frame, x, 120, 'float, step 1.0 (default)') cvui.trackbar(frame, x, 150, width, floatValue, 10., 15.) # You can specify segments and custom labels. Segments are visual marks in # the trackbar scale. Internally the value for the trackbar is stored as # long double, so the custom labels must always format long double numbers, no # matter the type of the numbers being used for the trackbar. E.g. %.2Lf cvui.text(frame, x, 230, 'double, 4 segments, custom label %.2Lf') cvui.trackbar(frame, x, 260, width, doubleValue2, 0., 20., 4, '%.2Lf') # Again: you have to be very explicit about the value, the min and the max params. # Below is a uchar trackbar. Observe the uchar cast for the min, the max and # the step parameters. cvui.text(frame, x, 340, 'uchar, custom label %.0Lf') cvui.trackbar(frame, x, 370, width, ucharValue, 0, 255, 0, '%.0Lf') # You can change the behavior of any tracker by using the options parameter. # Options are defined as a bitfield, so you can combine them. # E.g. # TRACKBAR_DISCRETE # value changes are discrete # TRACKBAR_DISCRETE | TRACKBAR_HIDE_LABELS # discrete changes and no labels cvui.text(frame, x, 450, 'double, step 0.1, option TRACKBAR_DISCRETE') cvui.trackbar(frame, x, 480, width, doubleValue3, 10., 10.5, 1, '%.1Lf', cvui.TRACKBAR_DISCRETE, 0.1) # More customizations using options. options = cvui.TRACKBAR_DISCRETE | cvui.TRACKBAR_HIDE_SEGMENT_LABELS cvui.text(frame, x, 560, 'int, 3 segments, DISCRETE | HIDE_SEGMENT_LABELS') cvui.trackbar(frame, x, 590, width, intValue, 10, 50, 3, '%.0Lf', options, 2) # Trackbar using char type. cvui.text(frame, x, 670, 'char, 2 segments, custom label %.0Lf') cvui.trackbar(frame, x, 700, width, charValue, -128, 127, 2, '%.0Lf') # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This is a demo application to showcase the use of trackbars and columns. # # Author: # Pascal Thomet # # Contributions: # Fernando Bevilacqua <dovyski@gmail.com> # # Copyright (c) 2018 Authors and Contributors # Code licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Trackbar and columns' def main(): intValue = [30] ucharValue = [30] charValue = [30] floatValue = [12.] doubleValue1 = [15.] doubleValue2 = [10.3] doubleValue3 = [2.25] frame = np.zeros((650, 450, 3), np.uint8) # Size of trackbars width = 400 # Init cvui and tell it to use a value of 20 for cv2.waitKey() # because we want to enable keyboard shortcut for # all components, e.g. button with label '&Quit'. # If cvui has a value for waitKey, it will call # waitKey() automatically for us within cvui.update(). cvui.init(WINDOW_NAME, 20) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) cvui.beginColumn(frame, 20, 20, -1, -1, 6) cvui.text('int trackbar, no customization') cvui.trackbar(width, intValue, 0, 100) cvui.space(5) cvui.text('uchar trackbar, no customization') cvui.trackbar(width, ucharValue, 0, 255) cvui.space(5) cvui.text('signed char trackbar, no customization') cvui.trackbar(width, charValue, -128, 127) cvui.space(5) cvui.text('float trackbar, no customization') cvui.trackbar(width, floatValue, 10., 15.) cvui.space(5) cvui.text('float trackbar, 4 segments') cvui.trackbar(width, doubleValue1, 10., 20., 4) cvui.space(5) cvui.text('double trackbar, label %.1Lf, TRACKBAR_DISCRETE') cvui.trackbar(width, doubleValue2, 10., 10.5, 1, '%.1Lf', cvui.TRACKBAR_DISCRETE, 0.1) cvui.space(5) cvui.text('double trackbar, label %.2Lf, 2 segments, TRACKBAR_DISCRETE') cvui.trackbar(width, doubleValue3, 0., 4., 2, '%.2Lf', cvui.TRACKBAR_DISCRETE, 0.25) cvui.space(10) # Exit the application if the quit button was pressed. # It can be pressed because of a mouse click or because # the user pressed the 'q' key on the keyboard, which is # marked as a shortcut in the button label ('&Quit'). if cvui.button('&Quit'): break cvui.endColumn() # Since cvui.init() received a param regarding waitKey, # there is no need to call cv.waitKey() anymore. cvui.update() # will do it automatically. cvui.update() cv2.imshow(WINDOW_NAME, frame) if __name__ == '__main__': main()
# # This is a demo application to showcase columns, trackbars and sparklines. # # Author: # Pascal Thomet # # Contributions: # Fernando Bevilacqua <dovyski@gmail.com> # # Copyright (c) 2018 Authors and Contributors # Code licensed under the MIT license. # import numpy as np import cv2 import cvui WINDOW_NAME = 'Trackbar and sparkline' def main(): frame = np.zeros((300, 800, 3), np.uint8) value = [2.25] values = [] # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME, 20) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # In a row, all added elements are # horizontally placed, one next the other (from left to right) # # Within the cvui.beginRow() and cvui.endRow(), # all elements will be automatically positioned by cvui. # # Notice that all component calls within the begin/end block # DO NOT have (x,y) coordinates. # # Let's create a row at position (20,80) with automatic width and height, and a padding of 10 cvui.beginRow(frame, 20, 80, -1, -1, 10); # trackbar accepts a pointer to a variable that controls their value. # Here we define a double trackbar between 0. and 5. if cvui.trackbar(150, value, 0., 5.): print('Trackbar was modified, value : ', value[0]) values.append(value[0]) if len(values) > 5: cvui.text('Your edits on a sparkline ->') cvui.sparkline(values, 240, 60) if cvui.button('Clear sparkline'): values = [] else: cvui.text('<- Move the trackbar') cvui.endRow(); # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This application showcases how the window component of cvui can be # enhanced, i.e. movable anb minimizable, and used to control the # application of the Canny edge algorithm to a loaded image. # # This application uses the EnhancedWindow class available in the # "ui-enhanced-window-component" example. # # Author: # Fernando Bevilacqua <dovyski@gmail.com> # # Contributions: # Amaury Breheret - https://github.com/abreheret # ShengYu - https://github.com/shengyu7697 # # Copyright (c) 2018 Authors and Contributors # Code licensed under the MIT license. # import numpy as np import cv2 import cvui # Include the enhanced cvui window component, i.e. EnhancedWindow.py from EnhancedWindow import EnhancedWindow WINDOW_NAME = 'CVUI Canny Edge' def main(): fruits = cv2.imread('fruits.jpg', cv2.IMREAD_COLOR) frame = np.zeros(fruits.shape, np.uint8) low_threshold = [50] high_threshold = [150] use_canny = [False] # Create a settings window using the EnhancedWindow class. settings = EnhancedWindow(10, 50, 270, 180, 'Settings') # Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Should we apply Canny edge? if use_canny[0]: # Yes, we should apply it. frame = cv2.cvtColor(fruits, cv2.COLOR_BGR2GRAY) frame = cv2.Canny(frame, low_threshold[0], high_threshold[0], 3) frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) else: # No, so just copy the original image to the displaying frame. frame[:] = fruits[:] # Render the settings window and its content, if it is not minimized. settings.begin(frame) if settings.isMinimized() == False: cvui.checkbox('Use Canny Edge', use_canny) cvui.trackbar(settings.width() - 20, low_threshold, 5, 150) cvui.trackbar(settings.width() - 20, high_threshold, 80, 300) cvui.space(20); # add 20px of empty space cvui.text('Drag and minimize this settings window', 0.4, 0xff0000) settings.end() # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()
# # This application showcases how the window component of cvui can be # enhanced, i.e. make it movable anb minimizable. # # Authors: # ShengYu - https://github.com/shengyu7697 # Amaury Breheret - https://github.com/abreheret # # Contributions: # Fernando Bevilacqua <dovyski@gmail.com> # # Copyright (c) 2018 Authors and Contributors # Code licensed under the MIT license. # import numpy as np import cv2 import cvui # Include the enhanced cvui window component, i.e. EnhancedWindow.py from EnhancedWindow import EnhancedWindow WINDOW_NAME = 'CVUI Enhanced Window Component' def main(): frame = np.zeros((600, 800, 3), np.uint8) value = [50] settings = EnhancedWindow(20, 80, 200, 120, 'Settings') info = EnhancedWindow(250, 80, 330, 60, 'Info') # Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Place some instructions on the screen regarding the # settings window cvui.text(frame, 20, 20, 'The Settings and the Info windows below are movable and minimizable.') cvui.text(frame, 20, 40, 'Click and drag any window\'s title bar to move it around.') # Render a movable and minimizable settings window using # the EnhancedWindow class. settings.begin(frame) if settings.isMinimized() == False: cvui.text('Adjust something') cvui.space(10) # add 10px of space between UI components cvui.trackbar(settings.width() - 20, value, 5, 150) settings.end() # Render a movable and minimizable settings window using # the EnhancedWindow class. info.begin(frame) if info.isMinimized() == False: cvui.text('Moving and minimizable windows are awesome!') info.end() # Update all cvui internal stuff, e.g. handle mouse clicks, and show # everything on the screen. cvui.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break if __name__ == '__main__': main()