renamed some files to make it easier to understand

This commit is contained in:
simleek
2019-09-30 22:45:07 -07:00
parent eaa5fe13e0
commit 9c360c89a6
16 changed files with 77 additions and 80 deletions

View File

@ -1,2 +1,2 @@
# redirection, so we can use subtree like pip
from displayarray import webcam_pub, window_sub
from displayarray import frame_publising, subscriber_window

View File

@ -6,4 +6,4 @@ display is a function that displays these in their own windows.
__version__ = "0.6.6"
from .window_sub.cv_window_sub import display
from .subscriber_window.subscriber_windows import display

View File

@ -1,4 +1,4 @@
from displayarray.window_sub import winctrl
from displayarray.subscriber_window import window_commands
import numpy as np
from typing import Union
@ -13,7 +13,7 @@ def global_cv_display_callback(frame: np.ndarray, cam_id: Union[int, str]):
:param cam_id: The video or image source
:type cam_id: Union[int, str]
"""
from displayarray.window_sub import SubscriberWindows
from displayarray.subscriber_window import SubscriberWindows
SubscriberWindows.FRAME_DICT[str(cam_id) + "frame"] = frame
@ -41,11 +41,11 @@ class function_display_callback(object): # NOSONAR
def _run_finisher(self, frame, finished, *args, **kwargs):
if not callable(finish_function):
winctrl.quit()
window_commands.quit()
else:
finished = finish_function(frame, Ellipsis, finished, *args, **kwargs)
if finished:
winctrl.quit()
window_commands.quit()
def _display_internal(self, frame, *args, **kwargs):
finished = True

View File

@ -8,8 +8,8 @@ pub_cam_thread continually publishes updates to arrays, videos, and cameras
np_cam simulates numpy arrays as OpenCV cameras
"""
from . import camctrl
from .frame_handler import VideoHandlerThread
from .get_cam_ids import get_cam_ids
from .np_cam import NpCam
from .pub_cam import pub_cam_thread
from . import subscriber_dictionary
from .frame_update_thread import VideoHandlerThread
from .get_frame_ids import get_cam_ids
from .np_to_opencv import NpCam
from .frame_publishing import pub_cam_thread

View File

@ -4,8 +4,8 @@ import time
import cv2
import numpy as np
from displayarray.webcam_pub import camctrl
from .np_cam import NpCam
from displayarray.frame_publising import subscriber_dictionary
from .np_to_opencv import NpCam
from displayarray.uid import uid_for_source
from typing import Union, Tuple
@ -40,12 +40,12 @@ def pub_cam_loop(
"Only strings or ints representing cameras, or numpy arrays representing pictures supported."
)
camctrl.register_cam(name)
subscriber_dictionary.register_cam(name)
# cam.set(cv2.CAP_PROP_CONVERT_RGB, 0)
frame_counter = 0
sub = camctrl.cam_cmd_sub(name)
sub = subscriber_dictionary.cam_cmd_sub(name)
sub.return_on_no_data = ""
msg = ""
@ -56,7 +56,7 @@ def pub_cam_loop(
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, request_size[1])
if not cam.isOpened():
camctrl.CV_CAMS_DICT[name].status_pub.publish("failed")
subscriber_dictionary.CV_CAMS_DICT[name].status_pub.publish("failed")
return False
now = time.time()
while msg != "quit":
@ -65,14 +65,14 @@ def pub_cam_loop(
(ret, frame) = cam.read() # type: Tuple[bool, np.ndarray ]
if ret is False or not isinstance(frame, np.ndarray):
cam.release()
camctrl.CV_CAMS_DICT[name].status_pub.publish("failed")
subscriber_dictionary.CV_CAMS_DICT[name].status_pub.publish("failed")
return False
if cam.get(cv2.CAP_PROP_FRAME_COUNT) > 0:
frame_counter += 1
if frame_counter >= cam.get(cv2.CAP_PROP_FRAME_COUNT):
frame_counter = 0
cam = cv2.VideoCapture(cam_id)
camctrl.CV_CAMS_DICT[name].frame_pub.publish(frame)
subscriber_dictionary.CV_CAMS_DICT[name].frame_pub.publish(frame)
msg = sub.get()
sub.release()

View File

@ -5,9 +5,9 @@ import numpy as np
from displayarray.callbacks import global_cv_display_callback
from displayarray.uid import uid_for_source
from displayarray.webcam_pub import camctrl
from displayarray.webcam_pub.pub_cam import pub_cam_thread
from displayarray.window_sub import winctrl
from displayarray.frame_publising import subscriber_dictionary
from displayarray.frame_publising.frame_publishing import pub_cam_thread
from displayarray.subscriber_window import window_commands
FrameCallable = Callable[[np.ndarray], Optional[np.ndarray]]
@ -19,7 +19,7 @@ class VideoHandlerThread(threading.Thread):
self,
video_source: Union[int, str, np.ndarray] = 0,
callbacks: Optional[Union[List[FrameCallable], FrameCallable]] = None,
request_size: Tuple[int, int] = (-1, -1),
request_size: Tuple[int, int] = (99999, 99999),
high_speed: bool = True,
fps_limit: float = 240,
):
@ -38,7 +38,7 @@ class VideoHandlerThread(threading.Thread):
self.exception_raised = None
def __wait_for_cam_id(self):
while str(self.cam_id) not in camctrl.CV_CAMS_DICT:
while str(self.cam_id) not in subscriber_dictionary.CV_CAMS_DICT:
continue
def __apply_callbacks_to_frame(self, frame):
@ -54,8 +54,8 @@ class VideoHandlerThread(threading.Thread):
except Exception as e:
self.exception_raised = e
frame = frame_c = self.exception_raised
camctrl.stop_cam(self.cam_id)
winctrl.quit()
subscriber_dictionary.stop_cam(self.cam_id)
window_commands.quit()
raise e
if frame_c is not None:
global_cv_display_callback(frame_c, self.cam_id)
@ -69,8 +69,8 @@ class VideoHandlerThread(threading.Thread):
)
self.__wait_for_cam_id()
sub_cam = camctrl.cam_frame_sub(str(self.cam_id))
sub_owner = camctrl.handler_cmd_sub(str(self.cam_id))
sub_cam = subscriber_dictionary.cam_frame_sub(str(self.cam_id))
sub_owner = subscriber_dictionary.handler_cmd_sub(str(self.cam_id))
msg_owner = sub_owner.return_on_no_data = ""
while msg_owner != "quit":
frame = sub_cam.get(blocking=True, timeout=1.0) # type: np.ndarray
@ -78,7 +78,7 @@ class VideoHandlerThread(threading.Thread):
msg_owner = sub_owner.get()
sub_owner.release()
sub_cam.release()
camctrl.stop_cam(self.cam_id)
subscriber_dictionary.stop_cam(self.cam_id)
t.join()
def display(self, callbacks: List[Callable[[np.ndarray], Any]] = None):
@ -89,7 +89,7 @@ class VideoHandlerThread(threading.Thread):
:param callbacks: List of callbacks to be run on frames before displaying to the screen.
"""
from displayarray.window_sub import SubscriberWindows
from displayarray.subscriber_window import SubscriberWindows
if callbacks is None:
callbacks = []

View File

@ -1,9 +1,27 @@
from displayarray.window_sub import winctrl
from displayarray.subscriber_window import window_commands
import threading
import time
from typing import Callable
from displayarray.window_sub.mouse_event import MouseEvent
class MouseEvent(object):
"""Holds all the OpenCV mouse event information."""
def __init__(self, event, x, y, flags, param):
self.event = event
self.x = x
self.y = y
self.flags = flags
self.param = param
def __repr__(self):
return self.__str__()
def __str__(self):
return "event:{}\nx,y:{},{}\nflags:{}\nparam:{}\n".format(
self.event, self.x, self.y, self.flags, self.param
)
class _mouse_thread(object): # NOSONAR
@ -11,7 +29,7 @@ class _mouse_thread(object): # NOSONAR
def __init__(self, f):
self.f = f
self.sub_mouse = winctrl.mouse_pub.make_sub()
self.sub_mouse = window_commands.mouse_pub.make_sub()
def __call__(self, *args, **kwargs):
"""Call the function this was set up with."""
@ -23,8 +41,8 @@ class _mouse_loop_thread(object): # NOSONAR
def __init__(self, f, run_when_no_events=False, fps=60):
self.f = f
self.sub_mouse = winctrl.mouse_pub.make_sub()
self.sub_cmd = winctrl.win_cmd_pub.make_sub()
self.sub_mouse = window_commands.mouse_pub.make_sub()
self.sub_cmd = window_commands.win_cmd_pub.make_sub()
self.sub_cmd.return_on_no_data = ""
self.run_when_no_events = run_when_no_events
self.fps = fps
@ -40,7 +58,7 @@ class _mouse_loop_thread(object): # NOSONAR
self.f(None, *args, **kwargs)
msg_cmd = self.sub_cmd.get()
time.sleep(1.0 / self.fps)
winctrl.quit(force_all_read=False)
window_commands.quit(force_all_read=False)
class mouse_loop(object): # NOSONAR
@ -60,7 +78,7 @@ class _key_thread(object): # NOSONAR
def __init__(self, f):
self.f = f
self.sub_key = winctrl.key_pub.make_sub()
self.sub_key = window_commands.key_pub.make_sub()
def __call__(self, *args, **kwargs):
"""Call the function this was set up with."""
@ -72,8 +90,8 @@ class _key_loop_thread(object): # NOSONAR
def __init__(self, f, run_when_no_events=False, fps=60):
self.f = f
self.sub_key = winctrl.key_pub.make_sub()
self.sub_cmd = winctrl.win_cmd_pub.make_sub()
self.sub_key = window_commands.key_pub.make_sub()
self.sub_cmd = window_commands.win_cmd_pub.make_sub()
self.sub_cmd.return_on_no_data = ""
self.run_when_no_events = run_when_no_events
self.fps = fps
@ -89,7 +107,7 @@ class _key_loop_thread(object): # NOSONAR
self.f(None, *args, **kwargs)
msg_cmd = self.sub_cmd.get()
time.sleep(1.0 / self.fps)
winctrl.quit(force_all_read=False)
window_commands.quit(force_all_read=False)
class key_loop(object): # NOSONAR

View File

@ -4,4 +4,4 @@ Displays arrays.
SubscriberWindows displays one array per window, updating it as it's changed.
"""
from .cv_window_sub import SubscriberWindows
from .subscriber_windows import SubscriberWindows

View File

@ -8,11 +8,11 @@ from localpubsub import NoData
from displayarray.callbacks import global_cv_display_callback
from displayarray.uid import uid_for_source
from displayarray.webcam_pub import camctrl
from displayarray.webcam_pub.frame_handler import FrameCallable
from displayarray.webcam_pub.frame_handler import VideoHandlerThread
from displayarray.window_sub.mouse_event import MouseEvent
from displayarray.window_sub import winctrl
from displayarray.frame_publising import subscriber_dictionary
from displayarray.frame_publising.frame_update_thread import FrameCallable
from displayarray.frame_publising.frame_update_thread import VideoHandlerThread
from displayarray.input import MouseEvent
from displayarray.subscriber_window import window_commands
class SubscriberWindows(object):
@ -61,7 +61,7 @@ class SubscriberWindows(object):
def __stop_all_cams(self):
for c in self.source_names:
camctrl.stop_cam(c)
subscriber_dictionary.stop_cam(c)
def handle_keys(
self, key_input # type: int
@ -70,12 +70,12 @@ class SubscriberWindows(object):
if key_input in self.ESC_KEY_CODES:
for name in self.window_names:
cv2.destroyWindow(name + " (press ESC to quit)")
winctrl.quit()
window_commands.quit()
self.__stop_all_cams()
return "quit"
elif key_input not in [-1, 0]:
try:
winctrl.key_pub.publish(chr(key_input))
window_commands.key_pub.publish(chr(key_input))
except ValueError:
warnings.warn(
RuntimeWarning(
@ -88,7 +88,7 @@ class SubscriberWindows(object):
def handle_mouse(self, event, x, y, flags, param):
"""Capture mouse input for mouse control subscriber threads."""
mousey = MouseEvent(event, x, y, flags, param)
winctrl.mouse_pub.publish(mousey)
window_commands.mouse_pub.publish(mousey)
def _display_frames(self, frames, win_num, ids=None):
if isinstance(frames, Exception):
@ -137,7 +137,7 @@ class SubscriberWindows(object):
if id not in self.input_cams:
self.add_source(id)
self.add_window(id)
sub_cmd = winctrl.win_cmd_sub()
sub_cmd = window_commands.win_cmd_sub()
self.update_window_frames()
msg_cmd = sub_cmd.get()
key = self.handle_keys(cv2.waitKey(1))
@ -152,7 +152,7 @@ class SubscriberWindows(object):
def end(self):
"""Close all threads. Should be used with non-blocking mode."""
winctrl.quit(force_all_read=False)
window_commands.quit(force_all_read=False)
self.__stop_all_cams()
if self.close_threads is not None:
for t in self.close_threads:
@ -163,13 +163,13 @@ class SubscriberWindows(object):
def loop(self):
"""Continually update window frame. OpenCV only allows this in the main thread."""
sub_cmd = winctrl.win_cmd_sub()
sub_cmd = window_commands.win_cmd_sub()
msg_cmd = ""
key = ""
while msg_cmd != "quit" and key != "quit":
msg_cmd, key = self.update()
sub_cmd.release()
winctrl.quit(force_all_read=False)
window_commands.quit(force_all_read=False)
self.__stop_all_cams()

View File

@ -1,6 +1,3 @@
import threading
import logging
from localpubsub import VariablePub, VariableSub
key_pub = VariablePub()

View File

@ -1,17 +0,0 @@
class MouseEvent(object):
"""Holds all the OpenCV mouse event information."""
def __init__(self, event, x, y, flags, param):
self.event = event
self.x = x
self.y = y
self.flags = flags
self.param = param
def __repr__(self):
return self.__str__()
def __str__(self):
return "event:{}\nx,y:{},{}\nflags:{}\nparam:{}\n".format(
self.event, self.x, self.y, self.flags, self.param
)

View File

@ -1,4 +1,4 @@
import displayarray.webcam_pub as w
import displayarray.frame_publising as w
import unittest as ut
@ -8,7 +8,7 @@ class TestFrameHandler(ut.TestCase):
def test_handler(self):
def test_frame_handler(frame, cam_id):
if self.i == 200:
w.camctrl.stop_cam(cam_id)
w.subscriber_dictionary.stop_cam(cam_id)
if self.i % 100 == 0:
print(frame.shape)
self.i += 1

View File

@ -1,12 +1,11 @@
import unittest as ut
import displayarray.webcam_pub as w
from displayarray.window_sub import SubscriberWindows
import displayarray.frame_publising as w
from displayarray.subscriber_window import SubscriberWindows
from displayarray import display
from displayarray.input import mouse_loop, key_loop
from displayarray.input import mouse_loop, key_loop, MouseEvent
import numpy as np
from displayarray.window_sub.mouse_event import MouseEvent
class TestSubWin(ut.TestCase):
@ -89,7 +88,7 @@ class TestSubWin(ut.TestCase):
v.join()
def test_conway_life(self):
from displayarray.webcam_pub import VideoHandlerThread
from displayarray.frame_publising import VideoHandlerThread
from displayarray.callbacks import function_display_callback
import numpy as np
import cv2