Skip to content

Commit a2496fc

Browse files
authored
Merge pull request #43 from SimLeek/same_cam
One camera can now be accessed by two seperate pieces of code. Update…
2 parents 2bb608b + 96fcaa7 commit a2496fc

File tree

7 files changed

+53
-31
lines changed

7 files changed

+53
-31
lines changed

displayarray/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
display is a function that displays these in their own windows.
55
"""
66

7-
__version__ = "1.0.0"
7+
__version__ = "1.1.0"
88

99
from .window.subscriber_windows import display, breakpoint_display, read_updates
1010
from .frame.frame_publishing import publish_updates_zero_mq, publish_updates_ros

displayarray/frame/frame_publishing.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Publish frames so any function within this program can find them."""
22

3+
import asyncio
4+
import sys
35
import threading
46
import time
5-
import asyncio
6-
import cv2
77
import warnings
8-
import sys
8+
9+
import cv2
910

1011
using_pyv4l2cam = False
1112
try:
@@ -123,6 +124,7 @@ def pub_cam_loop_opencv(
123124
request_size: Tuple[int, int] = (-1, -1),
124125
high_speed: bool = True,
125126
fps_limit: float = float("inf"),
127+
extra: Optional[List[Tuple[int, int]]] = None,
126128
) -> bool:
127129
"""
128130
Publish whichever camera you select to CVCams.<cam_id>.Vid.
@@ -186,6 +188,9 @@ def pub_cam_loop_opencv(
186188
return True
187189

188190

191+
uid_dict: Dict[str, threading.Thread] = {}
192+
193+
189194
def pub_cam_thread(
190195
cam_id: Union[int, str],
191196
request_ize: Tuple[int, int] = (-1, -1),
@@ -194,22 +199,27 @@ def pub_cam_thread(
194199
) -> threading.Thread:
195200
"""Run pub_cam_loop in a new thread. Starts on creation."""
196201

197-
if (
198-
sys.platform == "linux"
199-
and using_pyv4l2cam
200-
and (
201-
isinstance(cam_id, int)
202-
or (isinstance(cam_id, str) and "/dev/video" in cam_id)
203-
)
204-
):
205-
pub_cam_loop = pub_cam_loop_pyv4l2
202+
name = uid_for_source(cam_id)
203+
if name in uid_dict.keys():
204+
t = uid_dict[name]
206205
else:
207-
pub_cam_loop = pub_cam_loop_opencv
206+
if (
207+
sys.platform == "linux"
208+
and using_pyv4l2cam
209+
and (
210+
isinstance(cam_id, int)
211+
or (isinstance(cam_id, str) and "/dev/video" in cam_id)
212+
)
213+
):
214+
pub_cam_loop = pub_cam_loop_pyv4l2
215+
else:
216+
pub_cam_loop = pub_cam_loop_opencv
208217

209-
t = threading.Thread(
210-
target=pub_cam_loop, args=(cam_id, request_ize, high_speed, fps_limit)
211-
)
212-
t.start()
218+
t = threading.Thread(
219+
target=pub_cam_loop, args=(cam_id, request_ize, high_speed, fps_limit)
220+
)
221+
uid_dict[name] = t
222+
t.start()
213223
return t
214224

215225

displayarray/frame/frame_updater.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,18 @@ def loop(self):
9191
sub_cam = subscriber_dictionary.cam_frame_sub(str(self.cam_id))
9292
sub_owner = subscriber_dictionary.handler_cmd_sub(str(self.cam_id))
9393
msg_owner = sub_owner.return_on_no_data = ""
94-
while msg_owner != "quit":
95-
frame = sub_cam.get(blocking=True, timeout=1.0) # type: np.ndarray
96-
self.__apply_callbacks_to_frame(frame)
97-
msg_owner = sub_owner.get()
98-
sub_owner.release()
99-
sub_cam.release()
100-
subscriber_dictionary.stop_cam(self.cam_id)
101-
t.join()
94+
try:
95+
while msg_owner != "quit":
96+
frame = sub_cam.get(blocking=True, timeout=1.0) # type: np.ndarray
97+
self.__apply_callbacks_to_frame(frame)
98+
msg_owner = sub_owner.get()
99+
except Exception as e:
100+
raise e
101+
finally:
102+
sub_owner.release()
103+
sub_cam.release()
104+
subscriber_dictionary.stop_cam(self.cam_id)
105+
t.join()
102106

103107
def display(self, callbacks: List[Callable[[np.ndarray], Any]] = None):
104108
"""

displayarray/window/subscriber_windows.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ def update_frames(self):
211211
self.frames[self.input_vid_global_names[i]][-1] = frame
212212
if not self.silent:
213213
self.__check_too_many_channels()
214-
self.FRAME_DICT[self.input_vid_global_names[i]] = NoData()
215214
if not self.silent:
216215
self.display_frames(self.frames)
217216

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from displayarray import read_updates
2+
3+
with read_updates(0) as a, read_updates(0) as b:
4+
for i in range(1000):
5+
a.update()
6+
b.update()
7+
try:
8+
print(a.frames == b.frames)
9+
except ValueError:
10+
print(f"frame comparison: {(a.frames['0'][0] == b.frames['0'][0]).all()}")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = 'displayarray'
3-
version = '1.0.0'
3+
version = '1.1.0'
44
description = 'Tool for displaying numpy arrays.'
55
authors = ['SimLeek <simulator.leek@gmail.com>']
66
license = 'MIT'

tests/frame/test_frame_updater.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def redden_frame_print_spam(frame):
7575
frame[:, :, 2] = 1 / 0
7676

7777
with pytest.raises(ZeroDivisionError) as e:
78-
v = fup.FrameUpdater(np.zeros((1, 1, 3)), callbacks=redden_frame_print_spam)
78+
v = fup.FrameUpdater(np.zeros((1, 2, 3)), callbacks=redden_frame_print_spam)
7979
v.loop()
8080
assert e.errisinstance(ZeroDivisionError)
8181

@@ -92,8 +92,6 @@ def test_display():
9292

9393
mock_sub_win.assert_called_once_with(video_sources=["0"], callbacks=[])
9494
mock_sub_win_instance.loop.assert_called_once()
95-
f.start.assert_called_once()
96-
f.join.assert_called_once()
9795

9896

9997
def test_display_exception():
@@ -109,6 +107,7 @@ def redden_frame_print_spam(frame):
109107
v = fup.FrameUpdater(np.zeros((1, 1, 3)), callbacks=redden_frame_print_spam)
110108
v.display()
111109
assert e.errisinstance(ZeroDivisionError)
110+
# todo: clear the frame dict so that these don't hang forever
112111

113112

114113
from displayarray.window.window_commands import win_cmd_pub

0 commit comments

Comments
 (0)