From 746a9cfab0be742f3b47e8bffe67f46abf4df410 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 3 Apr 2018 19:39:18 -0700 Subject: [PATCH] I have added functionality of entering letters during runtime, they are displayed for you viewing pleasure. Additionally, I have began working making the item listed above be pubsub'd and there more useful to all of mankind --- cv_pubsubs/webcam_pub/camctrl.py | 8 ++++++- cv_pubsubs/window_sub/cv_window_sub.py | 31 +++++++++++++++++++++++++- tests_interactive/test_sub_win.py | 9 ++++++++ testt.py | 8 +++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 testt.py diff --git a/cv_pubsubs/webcam_pub/camctrl.py b/cv_pubsubs/webcam_pub/camctrl.py index 8fdff7d..541ac5c 100644 --- a/cv_pubsubs/webcam_pub/camctrl.py +++ b/cv_pubsubs/webcam_pub/camctrl.py @@ -1,4 +1,5 @@ import pubsub + if False: from typing import Union @@ -12,5 +13,10 @@ class CamCtrl: @staticmethod def reset_vid(cam_id # type: Union[int, str] - ): + ): pubsub.publish("cvcamhandlers." + str(cam_id) + ".cmd", 'r') + + @staticmethod + def key_stroke(key_entered): + pubsub.publish("cvKeyStroke.", key_entered) + diff --git a/cv_pubsubs/window_sub/cv_window_sub.py b/cv_pubsubs/window_sub/cv_window_sub.py index c1b2b6f..ec3b054 100644 --- a/cv_pubsubs/window_sub/cv_window_sub.py +++ b/cv_pubsubs/window_sub/cv_window_sub.py @@ -1,11 +1,24 @@ import cv2 from ..webcam_pub.camctrl import CamCtrl + if False: from typing import List frame_dict = {} +def triangle_seen(): + print("a triangle was seen") +def square_seen(): + print("a square was seen") +def nothing_seen(): + print("nothing was seen") + +command_dict = { + "t": triangle_seen, + "s": square_seen, + " ": nothing_seen +} # todo: figure out how to get the red x button to work. Try: https://stackoverflow.com/a/37881722/782170 def sub_win_loop( @@ -25,11 +38,27 @@ def sub_win_loop( frames = frame_dict[input_vid_global_names[i]] for f in range(len(frames)): cv2.imshow(names[f % len(names)]+" (press q to quit)", frames[f]) + if cv2.getWindowProperty(names[f % len(names)]+" (press q to quit)", 0) != 0: + print("X was pressed") + for name in names: + cv2.destroyWindow(name) + for c in input_cams: + CamCtrl.stop_cam(c) - if cv2.waitKey(1) & 0xFF == ord('q'): + + key_criteria = cv2.waitKey(1) & 0xFF + + if key_criteria == ord("q"): for name in names: cv2.destroyWindow(name) for c in input_cams: CamCtrl.stop_cam(c) return + if chr(key_criteria) in command_dict: + command_dict[chr(key_criteria)]() + CamCtrl.key_stroke(chr(key_criteria)) + elif chr(key_criteria) != "ΓΏ": + print(chr(key_criteria)) + + diff --git a/tests_interactive/test_sub_win.py b/tests_interactive/test_sub_win.py index c64f3c4..e3d4960 100644 --- a/tests_interactive/test_sub_win.py +++ b/tests_interactive/test_sub_win.py @@ -2,6 +2,15 @@ import unittest as ut import cv_pubsubs.webcam_pub as w from cv_pubsubs.window_sub import frame_dict, sub_win_loop +def subscribe_to_key_command(cam_id, # type: Union[int, str] + frame_handler, # type: Callable[[int, np.ndarray], Any] + request_size=(1280, 720), # type: Tuple[int, int] + high_speed=False, # type: bool + fps_limit=240 # type: float + ): # type: (...) -> threading.Thread + t = threading.Thread(target=frame_handler_loop, args=(cam_id, frame_handler, request_size, high_speed, fps_limit)) + t.start() + return t class TestSubWin(ut.TestCase): diff --git a/testt.py b/testt.py new file mode 100644 index 0000000..eb39d42 --- /dev/null +++ b/testt.py @@ -0,0 +1,8 @@ +def hi(): + print("hi") + +if __name__ == '__main__': + dic = {} + dic['a'] = hi + + dic['a']() \ No newline at end of file