pyobs-v4l 2.0.0.dev3__tar.gz → 2.0.0.dev4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyobs-v4l
3
- Version: 2.0.0.dev3
3
+ Version: 2.0.0.dev4
4
4
  Summary: pyobs module for V4L cameras
5
5
  Author-email: Tim-Oliver Husser <thusser@uni-goettingen.de>
6
6
  License-Expression: MIT
@@ -0,0 +1,112 @@
1
+ import asyncio
2
+ import logging
3
+ import threading
4
+ import time
5
+ from collections.abc import Callable
6
+ from typing import Any
7
+
8
+ import cv2
9
+ from pyobs.modules.camera import BaseVideo
10
+
11
+ log = logging.getLogger(__name__)
12
+
13
+ # cv2's V4L2 backend calls are blocking and are made directly on the event loop thread (see
14
+ # _run_blocking). If the camera has gone unresponsive, they can hang indefinitely, so they're
15
+ # bounded with a timeout rather than let a single dead camera freeze the whole module.
16
+ _SDK_CALL_TIMEOUT = 5.0
17
+
18
+ # read() waits for the next frame, which can legitimately take a while depending on the camera's
19
+ # own frame rate -- a more generous timeout than _SDK_CALL_TIMEOUT so normal operation never trips it.
20
+ _FRAME_WAIT_TIMEOUT = 30.0
21
+
22
+
23
+ class v4lCamera(BaseVideo):
24
+ def __init__(self, device: int = 0, **kwargs: Any):
25
+ BaseVideo.__init__(self, **kwargs)
26
+
27
+ # store
28
+ self._device = device
29
+
30
+ # thread
31
+ self.add_background_task(self._capture)
32
+
33
+ @staticmethod
34
+ async def _run_blocking(func: Callable[[], None], timeout: float = _SDK_CALL_TIMEOUT) -> bool:
35
+ """Run a blocking cv2/V4L2 call in a daemon thread, so a hung call can't freeze the module.
36
+
37
+ A plain executor isn't used here, since its worker threads are non-daemon and Python joins
38
+ them on interpreter shutdown -- a hung call would then just move the freeze to process exit.
39
+
40
+ Returns:
41
+ True if func completed within timeout, False if it's still running in the background.
42
+ """
43
+ loop = asyncio.get_running_loop()
44
+ future: asyncio.Future[None] = loop.create_future()
45
+
46
+ def _wrapper() -> None:
47
+ try:
48
+ func()
49
+ finally:
50
+ loop.call_soon_threadsafe(future.set_result, None)
51
+
52
+ threading.Thread(target=_wrapper, daemon=True).start()
53
+ try:
54
+ await asyncio.wait_for(future, timeout=timeout)
55
+ return True
56
+ except TimeoutError:
57
+ return False
58
+
59
+ async def _open_camera(self) -> Any:
60
+ """Open the V4L2 camera device, without blocking the event loop."""
61
+ result: list[Any] = []
62
+
63
+ def _open() -> None:
64
+ result.append(cv2.VideoCapture(self._device))
65
+
66
+ if not await self._run_blocking(_open):
67
+ raise TimeoutError(f"Timed out opening camera device after {_SDK_CALL_TIMEOUT}s.")
68
+ return result[0]
69
+
70
+ async def _read_frame(self, camera: Any) -> Any:
71
+ """Read the next frame from the camera, without blocking the event loop.
72
+
73
+ Returns:
74
+ The next frame, or None if the read timed out.
75
+ """
76
+ result: list[Any] = []
77
+
78
+ def _read() -> None:
79
+ _, frame = camera.read()
80
+ result.append(frame)
81
+
82
+ if not await self._run_blocking(_read, timeout=_FRAME_WAIT_TIMEOUT):
83
+ log.error("Timed out reading frame after %.1fs.", _FRAME_WAIT_TIMEOUT)
84
+ return None
85
+ return result[0]
86
+
87
+ async def _capture(self) -> None:
88
+ # open camera
89
+ camera = await self._open_camera()
90
+
91
+ # loop until closing
92
+ last = time.time()
93
+ while True:
94
+ # read frame
95
+ frame = await self._read_frame(camera)
96
+ if frame is None:
97
+ continue
98
+
99
+ # if time since last image is too short, wait a little
100
+ if time.time() - last < self._interval:
101
+ await asyncio.sleep(0.01)
102
+ continue
103
+ last = time.time()
104
+
105
+ # process it
106
+ await self._set_image(frame)
107
+
108
+ # release camera
109
+ camera.release()
110
+
111
+
112
+ __all__ = ["v4lCamera"]
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pyobs-v4l"
3
- version = "2.0.0.dev3"
3
+ version = "2.0.0.dev4"
4
4
  description = "pyobs module for V4L cameras"
5
5
  authors = [{ name = "Tim-Oliver Husser", email = "thusser@uni-goettingen.de" }]
6
6
  requires-python = ">=3.11,<3.14"
@@ -1837,7 +1837,7 @@ gui = [
1837
1837
 
1838
1838
  [[package]]
1839
1839
  name = "pyobs-v4l"
1840
- version = "2.0.0.dev3"
1840
+ version = "2.0.0.dev4"
1841
1841
  source = { editable = "." }
1842
1842
  dependencies = [
1843
1843
  { name = "opencv-python" },
@@ -1,45 +0,0 @@
1
- import asyncio
2
- import logging
3
- import time
4
- from typing import Any
5
-
6
- import cv2
7
- from pyobs.modules.camera import BaseVideo
8
-
9
- log = logging.getLogger(__name__)
10
-
11
-
12
- class v4lCamera(BaseVideo):
13
- def __init__(self, device: int = 0, **kwargs: Any):
14
- BaseVideo.__init__(self, **kwargs)
15
-
16
- # store
17
- self._device = device
18
-
19
- # thread
20
- self.add_background_task(self._capture)
21
-
22
- async def _capture(self) -> None:
23
- # open camera
24
- camera = cv2.VideoCapture(self._device)
25
-
26
- # loop until closing
27
- last = time.time()
28
- while True:
29
- # read frame
30
- _, frame = camera.read()
31
-
32
- # if time since last image is too short, wait a little
33
- if time.time() - last < self._interval:
34
- await asyncio.sleep(0.01)
35
- continue
36
- last = time.time()
37
-
38
- # process it
39
- await self._set_image(frame)
40
-
41
- # release camera
42
- camera.release()
43
-
44
-
45
- __all__ = ["v4lCamera"]
File without changes
File without changes