Last active
March 5, 2019 10:23
-
-
Save lusingander/24a4a4e69f6a3e9561876aa808c90182 to your computer and use it in GitHub Desktop.
cvui practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| https://github.com/Dovyski/cvui | |
| https://dovyski.github.io/cvui/ | |
| """ | |
| from abc import ABCMeta, abstractmethod | |
| import numpy as np | |
| import cv2 | |
| import cvui | |
| class CvuiComponent(metaclass=ABCMeta): | |
| def __init__(self): | |
| self.frame = None | |
| @abstractmethod | |
| def process(self): | |
| pass | |
| class ValueCvuiComponent(CvuiComponent): | |
| def __init__(self, oldVal): | |
| super().__init__() | |
| self.on_value_changed = None | |
| self._oldVal = oldVal | |
| class ClickableCvuiComponent(CvuiComponent): | |
| def __init__(self): | |
| super().__init__() | |
| self.on_clicked = None | |
| class Trackbar(ValueCvuiComponent): | |
| def __init__(self, x, y, w, defval, minval, maxval, segments, format): | |
| super().__init__(defval) | |
| self.value = [defval] | |
| self.x = x | |
| self.y = y | |
| self.w = w | |
| self.minval = minval | |
| self.maxval = maxval | |
| self.segments = segments | |
| self.format = format | |
| def val(self): | |
| return self.value[0] | |
| def process(self): | |
| changed = cvui.trackbar(self.frame, self.x, self.y, self.w, self.value, | |
| self.minval, self.maxval, self.segments, self.format) | |
| v = self.val() | |
| if changed and self.on_value_changed: | |
| self.on_value_changed(v) | |
| self._oldVal = v | |
| class Counter(ValueCvuiComponent): | |
| def __init__(self, x, y, defval=0, step=1, format='%d'): | |
| super().__init__(defval) | |
| self.value = [defval] | |
| self.x = x | |
| self.y = y | |
| self.step = step | |
| self.format = format | |
| def val(self): | |
| return self.value[0] | |
| def process(self): | |
| v = cvui.counter(self.frame, self.x, self.y, self.value, self.step, self.format) | |
| if v != self._oldVal and self.on_value_changed: | |
| self.on_value_changed(v) | |
| self._oldVal = v | |
| class Checkbox(ValueCvuiComponent): | |
| def __init__(self, x, y, label, defval=False, color=0xCECECE): | |
| super().__init__(defval) | |
| self.value = [defval] | |
| self.x = x | |
| self.y = y | |
| self.label = label | |
| self.color = color | |
| self.on_value_changed = None | |
| self.__oldVal = defval | |
| def checked(self): | |
| return self.value[0] | |
| def process(self): | |
| checked = cvui.checkbox(self.frame, self.x, self.y, self.label, self.value, self.color) | |
| if checked != self._oldVal and self.on_value_changed: | |
| self.on_value_changed(checked) | |
| self._oldVal = checked | |
| class Button(ClickableCvuiComponent): | |
| def __init__(self, x, y, label): | |
| self.x = x | |
| self.y = y | |
| self.label = label | |
| def process(self): | |
| clicked = cvui.button(self.frame, self.x, self.y, self.label) | |
| if clicked and self.on_clicked: | |
| self.on_clicked() | |
| class Text(CvuiComponent): | |
| def __init__(self, x, y, text): | |
| self.x = x | |
| self.y = y | |
| self.text = text | |
| def process(self): | |
| cvui.text(self.frame, self.x, self.y, self.text) | |
| class Window(CvuiComponent): | |
| def __init__(self, x, y, w, h, title): | |
| self.x = x | |
| self.y = y | |
| self.w = w | |
| self.h = h | |
| self.title = title | |
| def process(self): | |
| cvui.window(self.frame, self.x, self.y, self.w, self.h, self.title) | |
| class Image(CvuiComponent): | |
| def __init__(self, x, y, image, w=0, h=0, keep_ratio=True): | |
| self.x = x | |
| self.y = y | |
| self.image = image | |
| self.__resized_image = Image.__resize(image, w, h, keep_ratio) | |
| @classmethod | |
| def __resize(self, image, w, h, keep_ratio): | |
| if keep_ratio and w <= 0 and h <= 0: | |
| return None | |
| if not keep_ratio and (w <= 0 or h <= 0): | |
| return None | |
| sx = w / image.shape[1] | |
| sy = h / image.shape[0] | |
| if keep_ratio: | |
| if w > 0 and h > 0: | |
| sx = sy = min(sx, sy) | |
| elif h <= 0: | |
| sy = sx | |
| else: # w <= 0: | |
| sx = sy | |
| return cv2.resize(image, None, fx=sx, fy=sy) | |
| def process(self): | |
| if self.__resized_image is None: | |
| cvui.image(self.frame, self.x, self.y, self.image) | |
| else: | |
| cvui.image(self.frame, self.x, self.y, self.__resized_image) | |
| class CvuiMainFrame: | |
| BACKGROUND_COLOR = (49, 52, 49) | |
| def __init__(self, window_name, width, height, delay=10): | |
| self.__frame = np.zeros((height, width, 3), np.uint8) | |
| self.__window_name = window_name | |
| self.__components = [] | |
| self.__delay = delay | |
| def append(self, component): | |
| component.frame = self.__frame | |
| self.__components.append(component) | |
| def start(self): | |
| cvui.init(self.__window_name) | |
| while True: | |
| self.__frame[:] = CvuiMainFrame.BACKGROUND_COLOR | |
| for c in self.__components: | |
| c.process() | |
| cvui.update() | |
| cv2.imshow(self.__window_name, self.__frame) | |
| if cv2.waitKey(self.__delay) == 27: | |
| break | |
| def main(): | |
| mainframe = CvuiMainFrame('TEST', 800, 600) | |
| target = cv2.imread('sample.jpg') | |
| img = Image(300, 50, target, w=200) | |
| txt = Text(10, 10, 'cvui Sample') | |
| tb = Trackbar(10, 40, 250, 0, 0, 180, 1, '%d') | |
| tb.on_value_changed = lambda v: print(f"trackbar value changed: {v}") | |
| ct = Counter(20, 100) | |
| ct.on_value_changed = lambda v: print(f"counter value changed: {v}") | |
| cb = Checkbox(20, 140, 'checkbox') | |
| cb.on_value_changed = lambda v: print(f"checkbox {'checked' if v else 'unchecked'}") | |
| btn = Button(20, 180, 'button') | |
| btn.on_clicked = lambda: print("button clicked") | |
| mainframe.append(img) | |
| mainframe.append(txt) | |
| mainframe.append(tb) | |
| mainframe.append(ct) | |
| mainframe.append(cb) | |
| mainframe.append(btn) | |
| mainframe.start() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
