pyobs-v4l 2.0.0.dev3__tar.gz → 2.0.0.dev5__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.
- pyobs_v4l-2.0.0.dev5/.github/dependabot.yml +15 -0
- pyobs_v4l-2.0.0.dev5/.github/workflows/pyrefly.yml +23 -0
- pyobs_v4l-2.0.0.dev5/.github/workflows/pytest.yml +25 -0
- pyobs_v4l-2.0.0.dev5/PKG-INFO +12 -0
- pyobs_v4l-2.0.0.dev5/pyobs_v4l/v4lcamera.py +112 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/pyproject.toml +13 -6
- pyobs_v4l-2.0.0.dev5/tests/test_import.py +19 -0
- pyobs_v4l-2.0.0.dev5/tests/test_v4lcamera.py +45 -0
- pyobs_v4l-2.0.0.dev5/uv.lock +3154 -0
- pyobs_v4l-2.0.0.dev3/PKG-INFO +0 -12
- pyobs_v4l-2.0.0.dev3/pyobs_v4l/v4lcamera.py +0 -45
- pyobs_v4l-2.0.0.dev3/uv.lock +0 -2464
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/.github/workflows/pypi.yml +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/.github/workflows/ruff.yml +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/.pre-commit-config.yaml +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/.readthedocs.yml +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/LICENSE +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/README.md +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/docs/Makefile +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/docs/make.bat +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/docs/requirements.txt +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/docs/source/_static/pyobs.gif +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/docs/source/conf.py +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/docs/source/index.rst +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/pyobs_v4l/__init__.py +0 -0
- {pyobs_v4l-2.0.0.dev3 → pyobs_v4l-2.0.0.dev5}/pyobs_v4l/gui.py +0 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
name: pyrefly
|
|
2
|
+
on: push
|
|
3
|
+
|
|
4
|
+
jobs:
|
|
5
|
+
pyrefly:
|
|
6
|
+
runs-on: ubuntu-latest
|
|
7
|
+
timeout-minutes: 10
|
|
8
|
+
|
|
9
|
+
steps:
|
|
10
|
+
- name: Check out repository code
|
|
11
|
+
uses: actions/checkout@v4
|
|
12
|
+
|
|
13
|
+
- name: Set up Python
|
|
14
|
+
uses: astral-sh/setup-uv@v5
|
|
15
|
+
with:
|
|
16
|
+
enable-cache: true
|
|
17
|
+
python-version: "3.13"
|
|
18
|
+
|
|
19
|
+
- name: Install packages
|
|
20
|
+
run: uv sync --all-extras --dev
|
|
21
|
+
|
|
22
|
+
- name: Run pyrefly
|
|
23
|
+
run: uv run pyrefly check
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
name: PyTest
|
|
2
|
+
on: push
|
|
3
|
+
|
|
4
|
+
jobs:
|
|
5
|
+
test:
|
|
6
|
+
runs-on: ubuntu-latest
|
|
7
|
+
timeout-minutes: 10
|
|
8
|
+
|
|
9
|
+
steps:
|
|
10
|
+
- name: Check out repository code
|
|
11
|
+
uses: actions/checkout@v4
|
|
12
|
+
|
|
13
|
+
- name: Set up Python
|
|
14
|
+
uses: astral-sh/setup-uv@v5
|
|
15
|
+
with:
|
|
16
|
+
enable-cache: true
|
|
17
|
+
python-version: "3.13"
|
|
18
|
+
|
|
19
|
+
- name: Install packages
|
|
20
|
+
run: uv sync --all-extras --dev
|
|
21
|
+
|
|
22
|
+
- name: Run tests
|
|
23
|
+
run: uv run pytest tests
|
|
24
|
+
env:
|
|
25
|
+
CI: true
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pyobs-v4l
|
|
3
|
+
Version: 2.0.0.dev5
|
|
4
|
+
Summary: pyobs module for V4L cameras
|
|
5
|
+
Author-email: Tim-Oliver Husser <thusser@uni-goettingen.de>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Requires-Dist: opencv-python<6,>=4.0
|
|
10
|
+
Requires-Dist: pyobs-core<3,>=2.0.0.dev48
|
|
11
|
+
Provides-Extra: gui
|
|
12
|
+
Requires-Dist: pyobs-core[gui]<3,>=2.0.0.dev48; extra == 'gui'
|
|
@@ -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,18 +1,18 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pyobs-v4l"
|
|
3
|
-
version = "2.0.0.
|
|
3
|
+
version = "2.0.0.dev5"
|
|
4
4
|
description = "pyobs module for V4L cameras"
|
|
5
5
|
authors = [{ name = "Tim-Oliver Husser", email = "thusser@uni-goettingen.de" }]
|
|
6
|
-
requires-python = ">=3.11
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
7
|
license = "MIT"
|
|
8
8
|
dependencies = [
|
|
9
|
-
"opencv-python>=4.0,<
|
|
10
|
-
"pyobs-core>=2.0.0.
|
|
9
|
+
"opencv-python>=4.0,<6",
|
|
10
|
+
"pyobs-core>=2.0.0.dev48,<3",
|
|
11
11
|
]
|
|
12
12
|
|
|
13
13
|
[project.optional-dependencies]
|
|
14
14
|
gui = [
|
|
15
|
-
"pyobs-core[gui]>=2.0.0.
|
|
15
|
+
"pyobs-core[gui]>=2.0.0.dev48,<3",
|
|
16
16
|
]
|
|
17
17
|
|
|
18
18
|
[project.scripts]
|
|
@@ -20,10 +20,12 @@ v4l-gui = "pyobs_v4l.gui:main"
|
|
|
20
20
|
|
|
21
21
|
[dependency-groups]
|
|
22
22
|
dev = [
|
|
23
|
-
"black>=25.1.0,<
|
|
23
|
+
"black>=25.1.0,<27",
|
|
24
24
|
"ruff>=0.9.0",
|
|
25
25
|
"pyrefly>=1.1.1",
|
|
26
26
|
"pre-commit>=4.2.0,<5",
|
|
27
|
+
"pytest>=8.0.0",
|
|
28
|
+
"pytest-asyncio>=0.24.0",
|
|
27
29
|
]
|
|
28
30
|
|
|
29
31
|
[build-system]
|
|
@@ -43,3 +45,8 @@ select = ["E", "F", "W", "I", "UP", "G"]
|
|
|
43
45
|
|
|
44
46
|
[tool.pyrefly]
|
|
45
47
|
python-version = "3.11"
|
|
48
|
+
replace-imports-with-any = ["cv2"]
|
|
49
|
+
project-excludes = ["pyobs_v4l/gui.py"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
asyncio_mode = "strict"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Smoke tests: import the driver and instantiate it without hardware, asserting it
|
|
2
|
+
advertises the interfaces it claims.
|
|
3
|
+
|
|
4
|
+
The V4L2 device is only opened inside the capture loop (after open()), so
|
|
5
|
+
instantiation is safe. ``pyobs_v4l.gui`` is deliberately not imported here -- it pulls
|
|
6
|
+
in PySide6/qasync at module load and needs a display.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from pyobs.interfaces import IImageType, IVideo
|
|
10
|
+
from pyobs.modules import Module
|
|
11
|
+
|
|
12
|
+
from pyobs_v4l import v4lCamera
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_instantiate_camera() -> None:
|
|
16
|
+
camera = v4lCamera()
|
|
17
|
+
assert isinstance(camera, Module)
|
|
18
|
+
assert isinstance(camera, IVideo)
|
|
19
|
+
assert isinstance(camera, IImageType)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Unit tests for the non-hardware logic in v4lCamera: constructor state and the
|
|
2
|
+
_run_blocking timeout wrapper.
|
|
3
|
+
|
|
4
|
+
Opening the V4L2 device and reading frames is out of scope here.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import threading
|
|
9
|
+
|
|
10
|
+
import pytest
|
|
11
|
+
|
|
12
|
+
from pyobs_v4l import v4lCamera
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_constructor_defaults() -> None:
|
|
16
|
+
camera = v4lCamera()
|
|
17
|
+
assert camera._device == 0
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_constructor_device() -> None:
|
|
21
|
+
camera = v4lCamera(device=3)
|
|
22
|
+
assert camera._device == 3
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@pytest.mark.asyncio
|
|
26
|
+
async def test_run_blocking_runs_func_and_returns_true() -> None:
|
|
27
|
+
ran: list[bool] = []
|
|
28
|
+
|
|
29
|
+
def fast() -> None:
|
|
30
|
+
ran.append(True)
|
|
31
|
+
|
|
32
|
+
assert await v4lCamera._run_blocking(fast) is True
|
|
33
|
+
assert ran == [True]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@pytest.mark.asyncio
|
|
37
|
+
async def test_run_blocking_times_out() -> None:
|
|
38
|
+
done = threading.Event()
|
|
39
|
+
|
|
40
|
+
def slow() -> None:
|
|
41
|
+
done.wait()
|
|
42
|
+
|
|
43
|
+
assert await v4lCamera._run_blocking(slow, timeout=0.01) is False
|
|
44
|
+
done.set()
|
|
45
|
+
await asyncio.sleep(0.05) # let the daemon thread drain before the loop closes
|