From 147bf9aaff1ff3d968efa2765c0bce86c7e14532 Mon Sep 17 00:00:00 2001 From: SimLeek Date: Wed, 20 Feb 2019 22:08:06 -0700 Subject: [PATCH] nesting: Added ability to display tensors of any rank as multiple windows. Added to readme. lowered opencv_python requirement due to char* assertion bug. --- README.md | 10 ++++++++++ cvpubsubs/window_sub/cv_window_sub.py | 23 ++++++++++++++--------- requirements.txt | 2 +- setup.py | 2 +- tests/test_sub_win.py | 7 +++++++ 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ce013de..1426acb 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,16 @@ Python 2.7/3.5+ and PyPy. w.VideoHandlerThread(callbacks=[redden_frame_print_spam] + w.display_callbacks).display() +#### Display a tensor + + def tensor_from_image(frame, cam_id): + ten = tensor_from_pytorch_or_tensorflow(frame) + return ten + + t = wp.VideoHandlerThread(video_source=cam, callbacks=[tensor_from_image] + wp.display_callbacks) + + t.display() + #### Display multiple windows from one source import cvpubsubs.webcam_pub as w from cvpubsubs.window_sub import SubscriberWindows diff --git a/cvpubsubs/window_sub/cv_window_sub.py b/cvpubsubs/window_sub/cv_window_sub.py index eede534..5bd9c03 100644 --- a/cvpubsubs/window_sub/cv_window_sub.py +++ b/cvpubsubs/window_sub/cv_window_sub.py @@ -28,7 +28,7 @@ class SubscriberWindows(object): if isinstance(name, np.ndarray): self.source_names.append(str(hash(str(name)))) self.input_vid_global_names = [str(hash(str(name))) + "frame" for name in video_sources] - elif len(str(name))<=1000: + elif len(str(name)) <= 1000: self.source_names.append(str(name)) self.input_vid_global_names = [str(name) + "frame" for name in video_sources] else: @@ -37,7 +37,6 @@ class SubscriberWindows(object): self.callbacks = callbacks self.input_cams = video_sources - @staticmethod def set_global_frame_dict(name, *args): if len(str(name)) <= 1000: @@ -47,7 +46,6 @@ class SubscriberWindows(object): else: raise ValueError("Input window name too long.") - def __stop_all_cams(self): for c in self.source_names: CamCtrl.stop_cam(c) @@ -69,20 +67,27 @@ class SubscriberWindows(object): RuntimeWarning("Unknown key code: [{}]. Please report to cv_pubsubs issue page.".format(key_input)) ) + def _display_frames(self, frames, win_num): + for f in range(len(frames)): + if frames[f].dtype.num == 17 or len(frames[f].shape) > 3: # detect nested + self._display_frames(frames[f], win_num) + else: + cv2.imshow(self.window_names[win_num % len(self.window_names)] + " (press ESC to quit)", frames[f]) + win_num += 1 + def update_window_frames(self): win_num = 0 for i in range(len(self.input_vid_global_names)): if self.input_vid_global_names[i] in self.frame_dict and not isinstance(self.frame_dict[ - self.input_vid_global_names[i]], NoData): - if len(self.callbacks)>0 and self.callbacks[i % len(self.callbacks)] is not None: + self.input_vid_global_names[i]], + NoData): + if len(self.callbacks) > 0 and self.callbacks[i % len(self.callbacks)] is not None: frames = self.callbacks[i % len(self.callbacks)](self.frame_dict[self.input_vid_global_names[i]]) else: frames = self.frame_dict[self.input_vid_global_names[i]] - if isinstance(frames, np.ndarray) and len(frames.shape)<=3: + if isinstance(frames, np.ndarray) and len(frames.shape) <= 3: frames = [frames] - for f in range(len(frames)): - cv2.imshow(self.window_names[win_num % len(self.window_names)] + " (press ESC to quit)", frames[f]) - win_num += 1 + self._display_frames(frames, win_num) # todo: figure out how to get the red x button to work. Try: https://stackoverflow.com/a/37881722/782170 def loop(self): diff --git a/requirements.txt b/requirements.txt index d892d0b..c2474b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -opencv_python==4.0.0.21 +opencv_python==3.4.5.20 localpubsub==0.0.3 numpy==1.16.1 \ No newline at end of file diff --git a/setup.py b/setup.py index f28b1fb..319bbc2 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ with open('README.md', 'r', encoding='utf-8') as f: readme = f.read() REQUIRES = [ - 'opencv_python == 4.0.0.21', + 'opencv_python == 3.4.5.20', 'localpubsub == 0.0.3', 'numpy == 1.16.1' ] diff --git a/tests/test_sub_win.py b/tests/test_sub_win.py index 4a59bd6..594740c 100644 --- a/tests/test_sub_win.py +++ b/tests/test_sub_win.py @@ -89,3 +89,10 @@ class TestSubWin(ut.TestCase): t1.join() t1.join() + + def test_nested_frames(self): + def nest_frame(frame, cam_id): + frame = np.asarray([[[[[[frame]]]]]]) + return frame + + w.VideoHandlerThread(callbacks=[nest_frame] + w.display_callbacks).display() \ No newline at end of file