hex-driver-camera 0.0.1a1__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.
- hex_driver_camera-0.0.1a1/LICENSE +201 -0
- hex_driver_camera-0.0.1a1/MANIFEST.in +9 -0
- hex_driver_camera-0.0.1a1/PKG-INFO +37 -0
- hex_driver_camera-0.0.1a1/README.md +2 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/__init__.py +47 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/base.py +113 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/cam_berxel/__init__.py +22 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/cam_berxel/cam.py +284 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/cam_dummy/__init__.py +27 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/cam_dummy/cam.py +60 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/cam_realsense/__init__.py +22 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/cam_realsense/cam.py +136 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/cam_rgb/__init__.py +14 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera/cam_rgb/cam.py +106 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera.egg-info/PKG-INFO +37 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera.egg-info/SOURCES.txt +19 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera.egg-info/dependency_links.txt +1 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera.egg-info/requires.txt +12 -0
- hex_driver_camera-0.0.1a1/hex_driver_camera.egg-info/top_level.txt +1 -0
- hex_driver_camera-0.0.1a1/pyproject.toml +40 -0
- hex_driver_camera-0.0.1a1/setup.cfg +4 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hex_driver_camera
|
|
3
|
+
Version: 0.0.1a1
|
|
4
|
+
Summary: HEXFELLOW Camera Driver
|
|
5
|
+
Author-email: Dong Zhaorui <dzr159@gmail.com>
|
|
6
|
+
Maintainer-email: jecjune <zejun.chen@hexfellow.com>, Dong Zhaorui <dzr159@gmail.com>
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
Project-URL: Homepage, https://github.com/hexfellow/hex_driver_camera
|
|
9
|
+
Project-URL: Repository, https://github.com/hexfellow/hex_driver_camera.git
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/hexfellow/hex_driver_camera/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/hexfellow/hex_driver_camera/wiki
|
|
12
|
+
Keywords: hex,driver,camera
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: hex_util_runtime<0.1.0,>=0.0.0
|
|
26
|
+
Requires-Dist: opencv-python>=4.10.0
|
|
27
|
+
Provides-Extra: berxel
|
|
28
|
+
Requires-Dist: berxel_py_wrapper>=2.0.182; extra == "berxel"
|
|
29
|
+
Provides-Extra: realsense
|
|
30
|
+
Requires-Dist: pyrealsense2>=2.56.5.9235; extra == "realsense"
|
|
31
|
+
Provides-Extra: all
|
|
32
|
+
Requires-Dist: berxel_py_wrapper>=2.0.182; extra == "all"
|
|
33
|
+
Requires-Dist: pyrealsense2>=2.56.5.9235; extra == "all"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# hex_driver_camera
|
|
37
|
+
Hex camera driver
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
from .cam_dummy import HexCamDummy, HexCamDummyParams
|
|
10
|
+
from .cam_rgb import HexCamRgb, HexCamRgbParams
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
# dummy camera
|
|
14
|
+
"HexCamDummy",
|
|
15
|
+
"HexCamDummyParams",
|
|
16
|
+
|
|
17
|
+
# rgb camera
|
|
18
|
+
"HexCamRgb",
|
|
19
|
+
"HexCamRgbParams",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
import pyrealsense2 as _pyrealsense2 # noqa: F401
|
|
24
|
+
except ImportError:
|
|
25
|
+
_pyrealsense2 = None
|
|
26
|
+
|
|
27
|
+
if _pyrealsense2 is not None:
|
|
28
|
+
from .cam_realsense import HexCamRealsense, HexCamRealsenseParams
|
|
29
|
+
|
|
30
|
+
__all__ += [
|
|
31
|
+
# realsense camera (requires pyrealsense2)
|
|
32
|
+
"HexCamRealsense",
|
|
33
|
+
"HexCamRealsenseParams",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
import berxel_py_wrapper as _berxel_py_wrapper # noqa: F401
|
|
38
|
+
except ImportError:
|
|
39
|
+
_berxel_py_wrapper = None
|
|
40
|
+
|
|
41
|
+
if _berxel_py_wrapper is not None:
|
|
42
|
+
from .cam_berxel import HexCamBerxel, HexCamBerxelParams
|
|
43
|
+
|
|
44
|
+
__all__ += [
|
|
45
|
+
"HexCamBerxel",
|
|
46
|
+
"HexCamBerxelParams",
|
|
47
|
+
]
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
import cv2
|
|
10
|
+
import numpy as np
|
|
11
|
+
import multiprocessing as mp
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from abc import ABC, abstractmethod
|
|
14
|
+
from multiprocessing.synchronize import Event as MpEvent
|
|
15
|
+
from typing import Optional, Callable
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _hex_cam_worker_main(cam: "HexCamBase") -> None:
|
|
19
|
+
try:
|
|
20
|
+
cam.init_camera()
|
|
21
|
+
cam.work_loop()
|
|
22
|
+
finally:
|
|
23
|
+
cam.deinit_camera()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class HexCamBaseParams:
|
|
28
|
+
frame_rate: float = 30
|
|
29
|
+
height: int = 480
|
|
30
|
+
width: int = 640
|
|
31
|
+
queue_max: int = 10
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
HexFrameCallback = Callable[[int, np.ndarray], None]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class HexCamBase(ABC):
|
|
38
|
+
|
|
39
|
+
def __init__(self) -> None:
|
|
40
|
+
self._worker_process: Optional[mp.Process] = None
|
|
41
|
+
self._stop_event = mp.Event()
|
|
42
|
+
self._init_event = mp.Event()
|
|
43
|
+
|
|
44
|
+
def __del__(self):
|
|
45
|
+
self.deinit_camera()
|
|
46
|
+
|
|
47
|
+
def is_working(self) -> bool:
|
|
48
|
+
p = self._worker_process
|
|
49
|
+
return p is not None and p.is_alive()
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def stop_event(self) -> MpEvent:
|
|
53
|
+
"""Set by `stop_background_worker`; subclasses should exit `work_loop` when this is set."""
|
|
54
|
+
return self._stop_event
|
|
55
|
+
|
|
56
|
+
def start(self, *, daemon: bool = True) -> None:
|
|
57
|
+
"""Spawn a subprocess that runs `work_loop` until `stop_event` is set (or process ends)."""
|
|
58
|
+
if self._worker_process is not None and self._worker_process.is_alive(
|
|
59
|
+
):
|
|
60
|
+
return
|
|
61
|
+
self._stop_event.clear()
|
|
62
|
+
self._worker_process = mp.Process(
|
|
63
|
+
target=_hex_cam_worker_main,
|
|
64
|
+
args=(self, ),
|
|
65
|
+
daemon=daemon,
|
|
66
|
+
)
|
|
67
|
+
self._worker_process.start()
|
|
68
|
+
self._init_event.wait()
|
|
69
|
+
|
|
70
|
+
def stop(self, timeout: Optional[float] = 5.0) -> None:
|
|
71
|
+
"""Signal the worker to stop, then join. Uses `terminate` if join times out."""
|
|
72
|
+
self._stop_event.set()
|
|
73
|
+
proc = self._worker_process
|
|
74
|
+
if proc is None:
|
|
75
|
+
return
|
|
76
|
+
proc.join(timeout=timeout)
|
|
77
|
+
if proc.is_alive():
|
|
78
|
+
proc.terminate()
|
|
79
|
+
proc.join()
|
|
80
|
+
self._worker_process = None
|
|
81
|
+
|
|
82
|
+
@abstractmethod
|
|
83
|
+
def init_vars(self):
|
|
84
|
+
raise NotImplementedError(
|
|
85
|
+
"`get_info` should be implemented by the child class")
|
|
86
|
+
|
|
87
|
+
@abstractmethod
|
|
88
|
+
def init_camera(self):
|
|
89
|
+
raise NotImplementedError(
|
|
90
|
+
"`get_info` should be implemented by the child class")
|
|
91
|
+
|
|
92
|
+
@abstractmethod
|
|
93
|
+
def deinit_camera(self):
|
|
94
|
+
raise NotImplementedError(
|
|
95
|
+
"`deinit_camera` should be implemented by the child class")
|
|
96
|
+
|
|
97
|
+
@abstractmethod
|
|
98
|
+
def work_loop(self):
|
|
99
|
+
"""Long-running camera read loop. Must return when `stop_event` is set (cooperative shutdown)."""
|
|
100
|
+
raise NotImplementedError(
|
|
101
|
+
"`work_loop` should be implemented by the child class")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def depth_to_cmap(
|
|
105
|
+
depth: np.ndarray,
|
|
106
|
+
range: tuple[int, int] = (70, 1000),
|
|
107
|
+
) -> np.ndarray:
|
|
108
|
+
depth_values = depth.astype(np.float32)
|
|
109
|
+
depth_norm = np.clip((depth_values - range[0]) / (range[1] - range[0]),
|
|
110
|
+
0.0, 1.0)
|
|
111
|
+
depth_8u = (depth_norm * 255.0).astype(np.uint8)
|
|
112
|
+
depth_cmap = cv2.applyColorMap(depth_8u, cv2.COLORMAP_JET)
|
|
113
|
+
return depth_cmap
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
import berxel_py_wrapper as _berxel_py_wrapper # noqa: F401
|
|
11
|
+
except ImportError:
|
|
12
|
+
_berxel_py_wrapper = None
|
|
13
|
+
|
|
14
|
+
if _berxel_py_wrapper is not None:
|
|
15
|
+
from .cam import HexCamBerxel, HexCamBerxelParams
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"HexCamBerxel",
|
|
19
|
+
"HexCamBerxelParams",
|
|
20
|
+
]
|
|
21
|
+
else:
|
|
22
|
+
__all__ = []
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
import multiprocessing as mp
|
|
10
|
+
import time
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from typing import Any, Optional
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
from berxel_py_wrapper import *
|
|
16
|
+
from hex_util_runtime import ns_now
|
|
17
|
+
|
|
18
|
+
from ..base import HexCamBase, HexCamBaseParams, HexFrameCallback
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class HexCamBerxelParams(HexCamBaseParams):
|
|
23
|
+
serial_number: Optional[str] = None
|
|
24
|
+
exposure: int = 10000
|
|
25
|
+
gain: int = 100
|
|
26
|
+
sens_ts: bool = True
|
|
27
|
+
on_rgb: Optional[HexFrameCallback] = field(default=None)
|
|
28
|
+
on_depth: Optional[HexFrameCallback] = field(default=None)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class HexCamBerxel(HexCamBase):
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
params: HexCamBerxelParams = HexCamBerxelParams(),
|
|
36
|
+
) -> None:
|
|
37
|
+
super().__init__()
|
|
38
|
+
self._params = params
|
|
39
|
+
self._intri_shared = mp.Array("d", 4)
|
|
40
|
+
self.__context: Any = None
|
|
41
|
+
self.__device: Any = None
|
|
42
|
+
self.init_vars()
|
|
43
|
+
|
|
44
|
+
def init_vars(self) -> None:
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
def init_camera(self) -> None:
|
|
48
|
+
"""Open device, start streams, fill intrinsics. Runs in the worker process."""
|
|
49
|
+
if not self.__open_device():
|
|
50
|
+
raise RuntimeError("failed to open Berxel device")
|
|
51
|
+
if not self.__start_stream():
|
|
52
|
+
raise RuntimeError("failed to start Berxel streams")
|
|
53
|
+
self._init_event.set()
|
|
54
|
+
|
|
55
|
+
def get_intri(self) -> np.ndarray:
|
|
56
|
+
return np.array(self._intri_shared[:], dtype=np.float64)
|
|
57
|
+
|
|
58
|
+
def deinit_camera(self) -> None:
|
|
59
|
+
main = mp.current_process().name == "MainProcess"
|
|
60
|
+
if main and self.is_working():
|
|
61
|
+
self.stop()
|
|
62
|
+
self.__stop_stream()
|
|
63
|
+
self.__close_device()
|
|
64
|
+
|
|
65
|
+
def work_loop(self) -> None:
|
|
66
|
+
p = self._params
|
|
67
|
+
on_rgb = p.on_rgb
|
|
68
|
+
on_depth = p.on_depth
|
|
69
|
+
|
|
70
|
+
clean_cnt = 0
|
|
71
|
+
while clean_cnt < 5:
|
|
72
|
+
hawk_rgb_frame = self.__device.readColorFrame(40)
|
|
73
|
+
hawk_depth_frame = self.__device.readDepthFrame(40)
|
|
74
|
+
if hawk_rgb_frame is not None:
|
|
75
|
+
self.__device.releaseFrame(hawk_rgb_frame)
|
|
76
|
+
if hawk_depth_frame is not None:
|
|
77
|
+
self.__device.releaseFrame(hawk_depth_frame)
|
|
78
|
+
clean_cnt += 1
|
|
79
|
+
time.sleep(0.01)
|
|
80
|
+
|
|
81
|
+
bias_ns = np.int64(0)
|
|
82
|
+
for _ in range(30):
|
|
83
|
+
f = self.__device.readColorFrame(40)
|
|
84
|
+
if f is None:
|
|
85
|
+
time.sleep(0.001)
|
|
86
|
+
continue
|
|
87
|
+
sensor_ns = np.int64(int(f.getTimeStamp() * 1_000))
|
|
88
|
+
bias_ns = np.int64(ns_now()) - sensor_ns
|
|
89
|
+
self.__device.releaseFrame(f)
|
|
90
|
+
break
|
|
91
|
+
|
|
92
|
+
while not self.stop_event.is_set():
|
|
93
|
+
hawk_rgb_frame = self.__device.readColorFrame(40)
|
|
94
|
+
hawk_depth_frame = self.__device.readDepthFrame(40)
|
|
95
|
+
cur_ns = ns_now()
|
|
96
|
+
|
|
97
|
+
if hawk_rgb_frame is not None:
|
|
98
|
+
ts_ns, frame = self.__unpack_frame(
|
|
99
|
+
hawk_rgb_frame,
|
|
100
|
+
False,
|
|
101
|
+
bias_ns,
|
|
102
|
+
cur_ns,
|
|
103
|
+
)
|
|
104
|
+
if on_rgb is not None:
|
|
105
|
+
on_rgb(ts_ns, frame)
|
|
106
|
+
self.__device.releaseFrame(hawk_rgb_frame)
|
|
107
|
+
|
|
108
|
+
if hawk_depth_frame is not None:
|
|
109
|
+
ts_ns, frame = self.__unpack_frame(
|
|
110
|
+
hawk_depth_frame,
|
|
111
|
+
True,
|
|
112
|
+
bias_ns,
|
|
113
|
+
cur_ns,
|
|
114
|
+
)
|
|
115
|
+
if on_depth is not None:
|
|
116
|
+
on_depth(ts_ns, frame)
|
|
117
|
+
self.__device.releaseFrame(hawk_depth_frame)
|
|
118
|
+
|
|
119
|
+
def __unpack_frame(
|
|
120
|
+
self,
|
|
121
|
+
hawk_frame: BerxelHawkFrame,
|
|
122
|
+
depth: bool,
|
|
123
|
+
bias_ns: np.int64,
|
|
124
|
+
cur_ns: int,
|
|
125
|
+
) -> tuple[int, np.ndarray]:
|
|
126
|
+
berxel_ts_ns = int(bias_ns) + int(hawk_frame.getTimeStamp() * 1_000)
|
|
127
|
+
if self._params.sens_ts:
|
|
128
|
+
ts_ns = berxel_ts_ns
|
|
129
|
+
if cur_ns < ts_ns:
|
|
130
|
+
ts_ns = cur_ns
|
|
131
|
+
else:
|
|
132
|
+
ts_ns = cur_ns
|
|
133
|
+
|
|
134
|
+
width = hawk_frame.getWidth()
|
|
135
|
+
height = hawk_frame.getHeight()
|
|
136
|
+
|
|
137
|
+
if depth:
|
|
138
|
+
frame_buffer = hawk_frame.getDataAsUint16()
|
|
139
|
+
frame = np.ndarray(
|
|
140
|
+
shape=(height, width),
|
|
141
|
+
dtype=np.uint16,
|
|
142
|
+
buffer=frame_buffer,
|
|
143
|
+
)
|
|
144
|
+
pixel_type = hawk_frame.getPixelType()
|
|
145
|
+
if pixel_type == BerxelHawkPixelType.forward_dict[
|
|
146
|
+
'BERXEL_HAWK_PIXEL_TYPE_DEP_16BIT_12I_4D']:
|
|
147
|
+
frame = frame // 16
|
|
148
|
+
elif pixel_type == BerxelHawkPixelType.forward_dict[
|
|
149
|
+
'BERXEL_HAWK_PIXEL_TYPE_DEP_16BIT_13I_3D']:
|
|
150
|
+
frame = frame // 8
|
|
151
|
+
else:
|
|
152
|
+
raise ValueError(f"pixel_type: {pixel_type} not supported")
|
|
153
|
+
else:
|
|
154
|
+
frame_buffer = hawk_frame.getDataAsUint8()
|
|
155
|
+
frame = np.ndarray(
|
|
156
|
+
shape=(height, width, 3),
|
|
157
|
+
dtype=np.uint8,
|
|
158
|
+
buffer=frame_buffer,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
return ts_ns, frame
|
|
162
|
+
|
|
163
|
+
def __open_device(self) -> bool:
|
|
164
|
+
self.__context = BerxelHawkContext()
|
|
165
|
+
if self.__context is None:
|
|
166
|
+
print("init failed")
|
|
167
|
+
return False
|
|
168
|
+
self.__context.initCamera()
|
|
169
|
+
|
|
170
|
+
device_list = self.__context.getDeviceList()
|
|
171
|
+
if len(device_list) < 1:
|
|
172
|
+
print("can not find device")
|
|
173
|
+
return False
|
|
174
|
+
if self._params.serial_number is not None:
|
|
175
|
+
|
|
176
|
+
def same_serial(tar_serial, device_serial):
|
|
177
|
+
|
|
178
|
+
def norm_serial(x):
|
|
179
|
+
if x is None:
|
|
180
|
+
return None
|
|
181
|
+
if isinstance(x, (bytes, bytearray)):
|
|
182
|
+
x = x.decode('utf-8', 'ignore')
|
|
183
|
+
x = x.replace('\x00', '').strip()
|
|
184
|
+
return x.upper()
|
|
185
|
+
|
|
186
|
+
return norm_serial(tar_serial) == norm_serial(device_serial)
|
|
187
|
+
|
|
188
|
+
device_idx = -1
|
|
189
|
+
for idx, device in enumerate(device_list):
|
|
190
|
+
if same_serial(self._params.serial_number,
|
|
191
|
+
device.serialNumber):
|
|
192
|
+
print(
|
|
193
|
+
f"find device with serial number: {self._params.serial_number}"
|
|
194
|
+
)
|
|
195
|
+
device_idx = idx
|
|
196
|
+
break
|
|
197
|
+
if device_idx == -1:
|
|
198
|
+
print(
|
|
199
|
+
f"can not find device with serial number: {self._params.serial_number}"
|
|
200
|
+
)
|
|
201
|
+
print("available device serial numbers:")
|
|
202
|
+
for device in device_list:
|
|
203
|
+
print(f"{device.serialNumber}")
|
|
204
|
+
return False
|
|
205
|
+
self.__device = self.__context.openDevice(device_list[device_idx])
|
|
206
|
+
self._serial_number = str(self._params.serial_number)
|
|
207
|
+
else:
|
|
208
|
+
print("No serial number, use first device")
|
|
209
|
+
self.__device = self.__context.openDevice(device_list[0])
|
|
210
|
+
sn = device_list[0].serialNumber
|
|
211
|
+
if isinstance(sn, (bytes, bytearray)):
|
|
212
|
+
sn = sn.decode('utf-8', 'ignore')
|
|
213
|
+
self._serial_number = str(sn).replace('\x00', '').strip()
|
|
214
|
+
|
|
215
|
+
if self.__device is None:
|
|
216
|
+
print("open device failed")
|
|
217
|
+
return False
|
|
218
|
+
|
|
219
|
+
return True
|
|
220
|
+
|
|
221
|
+
def __start_stream(self) -> bool:
|
|
222
|
+
p = self._params
|
|
223
|
+
if self._serial_number.startswith('P008'):
|
|
224
|
+
self.__device.setSonixAEStatus(False)
|
|
225
|
+
self.__device.setSonixExposureTime(int(p.exposure // 100))
|
|
226
|
+
else:
|
|
227
|
+
self.__device.setColorExposureGain(p.exposure, p.gain)
|
|
228
|
+
self.__device.setDepthElectricCurrent(700)
|
|
229
|
+
self.__device.setDepthAE(False)
|
|
230
|
+
self.__device.setDepthExposure(43)
|
|
231
|
+
self.__device.setDepthGain(1)
|
|
232
|
+
self.__device.setRegistrationEnable(True)
|
|
233
|
+
self.__device.setFrameSync(True)
|
|
234
|
+
while self.__device.setSystemClock() != 0:
|
|
235
|
+
print("set system clock failed")
|
|
236
|
+
time.sleep(0.1)
|
|
237
|
+
|
|
238
|
+
intrinsic_params = self.__device.getDeviceIntriscParams()
|
|
239
|
+
self._intri_shared[0] = intrinsic_params.colorIntrinsicParams.fx / 2
|
|
240
|
+
self._intri_shared[1] = intrinsic_params.colorIntrinsicParams.fy / 2
|
|
241
|
+
self._intri_shared[2] = intrinsic_params.colorIntrinsicParams.cx / 2
|
|
242
|
+
self._intri_shared[3] = intrinsic_params.colorIntrinsicParams.cy / 2
|
|
243
|
+
|
|
244
|
+
color_frame_mode = self.__device.getCurrentFrameMode(
|
|
245
|
+
BerxelHawkStreamType.forward_dict['BERXEL_HAWK_COLOR_STREAM'])
|
|
246
|
+
depth_frame_mode = self.__device.getCurrentFrameMode(
|
|
247
|
+
BerxelHawkStreamType.forward_dict['BERXEL_HAWK_DEPTH_STREAM'])
|
|
248
|
+
color_frame_mode.framerate = int(p.frame_rate)
|
|
249
|
+
depth_frame_mode.framerate = int(p.frame_rate)
|
|
250
|
+
self.__device.setFrameMode(
|
|
251
|
+
BerxelHawkStreamType.forward_dict['BERXEL_HAWK_COLOR_STREAM'],
|
|
252
|
+
color_frame_mode)
|
|
253
|
+
self.__device.setFrameMode(
|
|
254
|
+
BerxelHawkStreamType.forward_dict['BERXEL_HAWK_DEPTH_STREAM'],
|
|
255
|
+
depth_frame_mode)
|
|
256
|
+
ret = self.__device.startStreams(
|
|
257
|
+
BerxelHawkStreamType.forward_dict['BERXEL_HAWK_DEPTH_STREAM']
|
|
258
|
+
| BerxelHawkStreamType.forward_dict['BERXEL_HAWK_COLOR_STREAM'])
|
|
259
|
+
if ret == 0:
|
|
260
|
+
print("start stream succeed")
|
|
261
|
+
return True
|
|
262
|
+
print("start stream failed")
|
|
263
|
+
return False
|
|
264
|
+
|
|
265
|
+
def __stop_stream(self) -> bool:
|
|
266
|
+
if self.__device is None:
|
|
267
|
+
return False
|
|
268
|
+
ret = self.__device.stopStream(
|
|
269
|
+
BerxelHawkStreamType.forward_dict['BERXEL_HAWK_DEPTH_STREAM']
|
|
270
|
+
| BerxelHawkStreamType.forward_dict['BERXEL_HAWK_COLOR_STREAM'])
|
|
271
|
+
return ret == 0
|
|
272
|
+
|
|
273
|
+
def __close_device(self) -> None:
|
|
274
|
+
if self.__context is None:
|
|
275
|
+
return
|
|
276
|
+
if self.__device is None:
|
|
277
|
+
return
|
|
278
|
+
|
|
279
|
+
ret = self.__context.closeDevice(self.__device)
|
|
280
|
+
if ret == 0:
|
|
281
|
+
print("close device succeed")
|
|
282
|
+
else:
|
|
283
|
+
print("close device Failed")
|
|
284
|
+
self.__context.destroyCamera()
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
from .cam import HexCamDummy, HexCamDummyParams
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"HexCamDummy",
|
|
13
|
+
"HexCamDummyParams",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
import pyrealsense2 as _pyrealsense2 # noqa: F401
|
|
18
|
+
except ImportError:
|
|
19
|
+
_pyrealsense2 = None
|
|
20
|
+
|
|
21
|
+
if _pyrealsense2 is not None:
|
|
22
|
+
from ..cam_realsense import HexCamRealsense, HexCamRealsenseParams
|
|
23
|
+
|
|
24
|
+
__all__ += [
|
|
25
|
+
"HexCamRealsense",
|
|
26
|
+
"HexCamRealsenseParams",
|
|
27
|
+
]
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
import multiprocessing as mp
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from typing import Optional
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
from hex_util_runtime import HexRate, ns_now
|
|
15
|
+
|
|
16
|
+
from ..base import HexCamBase, HexCamBaseParams, HexFrameCallback
|
|
17
|
+
|
|
18
|
+
# (sequence, ts_ns, rgb_uint8_hwc) — called from the worker after each frame; keep it fast.
|
|
19
|
+
# Set on params before ``start()`` so the child sees it (fork / spawn).
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class HexCamDummyParams(HexCamBaseParams):
|
|
24
|
+
on_rgb: Optional[HexFrameCallback] = field(default=None)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class HexCamDummy(HexCamBase):
|
|
28
|
+
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
params: HexCamDummyParams = HexCamDummyParams(),
|
|
32
|
+
) -> None:
|
|
33
|
+
super().__init__()
|
|
34
|
+
self._params = params
|
|
35
|
+
self.init_vars()
|
|
36
|
+
|
|
37
|
+
def init_vars(self):
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
def init_camera(self) -> None:
|
|
41
|
+
self._init_event.set()
|
|
42
|
+
|
|
43
|
+
def deinit_camera(self) -> None:
|
|
44
|
+
main = mp.current_process().name == "MainProcess"
|
|
45
|
+
if main and self.is_working():
|
|
46
|
+
self.stop()
|
|
47
|
+
|
|
48
|
+
def work_loop(self) -> None:
|
|
49
|
+
h = int(self._params.height)
|
|
50
|
+
w = int(self._params.width)
|
|
51
|
+
fps = float(self._params.frame_rate)
|
|
52
|
+
cb = self._params.on_rgb
|
|
53
|
+
|
|
54
|
+
rate = HexRate(fps)
|
|
55
|
+
while not self.stop_event.is_set():
|
|
56
|
+
rate.sleep()
|
|
57
|
+
rgb_img = np.random.randint(0, 255, (h, w, 3), dtype=np.uint8)
|
|
58
|
+
ts = ns_now()
|
|
59
|
+
if cb is not None:
|
|
60
|
+
cb(ts, rgb_img)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
import pyrealsense2 as _pyrealsense2 # noqa: F401
|
|
11
|
+
except ImportError:
|
|
12
|
+
_pyrealsense2 = None
|
|
13
|
+
|
|
14
|
+
if _pyrealsense2 is not None:
|
|
15
|
+
from .cam import HexCamRealsense, HexCamRealsenseParams
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"HexCamRealsense",
|
|
19
|
+
"HexCamRealsenseParams",
|
|
20
|
+
]
|
|
21
|
+
else:
|
|
22
|
+
__all__ = []
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
import multiprocessing as mp
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from typing import Any, Optional
|
|
12
|
+
|
|
13
|
+
import cv2
|
|
14
|
+
import numpy as np
|
|
15
|
+
import pyrealsense2 as rs
|
|
16
|
+
from hex_util_runtime import ns_now
|
|
17
|
+
|
|
18
|
+
from ..base import HexCamBase, HexCamBaseParams, HexFrameCallback
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class HexCamRealsenseParams(HexCamBaseParams):
|
|
23
|
+
serial_number: str = "243422073194"
|
|
24
|
+
sens_ts: bool = True
|
|
25
|
+
on_rgb: Optional[HexFrameCallback] = field(default=None)
|
|
26
|
+
on_depth: Optional[HexFrameCallback] = field(default=None)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class HexCamRealsense(HexCamBase):
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
params: HexCamRealsenseParams = HexCamRealsenseParams(),
|
|
34
|
+
) -> None:
|
|
35
|
+
super().__init__()
|
|
36
|
+
self._params = params
|
|
37
|
+
# Shared with the worker so get_intri() is valid after streaming starts.
|
|
38
|
+
self._intri_shared = mp.Array("d", 4)
|
|
39
|
+
self._pipeline: Any = None
|
|
40
|
+
self._align: Any = None
|
|
41
|
+
self.init_vars()
|
|
42
|
+
|
|
43
|
+
def get_intri(self) -> np.ndarray:
|
|
44
|
+
return np.array(self._intri_shared[:], dtype=np.float64)
|
|
45
|
+
|
|
46
|
+
def init_vars(self) -> None:
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
def init_camera(self) -> None:
|
|
50
|
+
"""Enumerate device, start color+depth streams, fill intrinsics. Must run in the worker process."""
|
|
51
|
+
p = self._params
|
|
52
|
+
ctx = rs.context()
|
|
53
|
+
serial_numbers = []
|
|
54
|
+
for dev in ctx.query_devices():
|
|
55
|
+
serial = dev.get_info(rs.camera_info.serial_number)
|
|
56
|
+
name = dev.get_info(rs.camera_info.name)
|
|
57
|
+
print(f" - Device: {name}, Serial: {serial}")
|
|
58
|
+
serial_numbers.append(serial)
|
|
59
|
+
if p.serial_number not in serial_numbers:
|
|
60
|
+
raise RuntimeError(
|
|
61
|
+
f"cannot find device with serial number: {p.serial_number}")
|
|
62
|
+
|
|
63
|
+
self._pipeline = rs.pipeline()
|
|
64
|
+
config = rs.config()
|
|
65
|
+
config.enable_device(self._params.serial_number)
|
|
66
|
+
config.enable_stream(
|
|
67
|
+
rs.stream.color,
|
|
68
|
+
p.width,
|
|
69
|
+
p.height,
|
|
70
|
+
rs.format.bgr8,
|
|
71
|
+
int(p.frame_rate),
|
|
72
|
+
)
|
|
73
|
+
config.enable_stream(
|
|
74
|
+
rs.stream.depth,
|
|
75
|
+
p.width,
|
|
76
|
+
p.height,
|
|
77
|
+
rs.format.z16,
|
|
78
|
+
int(p.frame_rate),
|
|
79
|
+
)
|
|
80
|
+
profile = self._pipeline.start(config)
|
|
81
|
+
color_profile = profile.get_stream(rs.stream.color)
|
|
82
|
+
color_intrinsics = color_profile.as_video_stream_profile(
|
|
83
|
+
).get_intrinsics()
|
|
84
|
+
self._intri_shared[0] = float(color_intrinsics.fx)
|
|
85
|
+
self._intri_shared[1] = float(color_intrinsics.fy)
|
|
86
|
+
self._intri_shared[2] = float(color_intrinsics.ppx)
|
|
87
|
+
self._intri_shared[3] = float(color_intrinsics.ppy)
|
|
88
|
+
self._align = rs.align(rs.stream.color)
|
|
89
|
+
self._init_event.set()
|
|
90
|
+
|
|
91
|
+
def deinit_camera(self) -> None:
|
|
92
|
+
main = mp.current_process().name == "MainProcess"
|
|
93
|
+
if main and self.is_working():
|
|
94
|
+
self.stop()
|
|
95
|
+
if self._pipeline is not None:
|
|
96
|
+
try:
|
|
97
|
+
self._pipeline.stop()
|
|
98
|
+
except RuntimeError:
|
|
99
|
+
pass
|
|
100
|
+
self._pipeline = None
|
|
101
|
+
self._align = None
|
|
102
|
+
|
|
103
|
+
def work_loop(self) -> None:
|
|
104
|
+
p = self._params
|
|
105
|
+
on_rgb = p.on_rgb
|
|
106
|
+
on_depth = p.on_depth
|
|
107
|
+
|
|
108
|
+
aligned_frames = self._align.process(self._pipeline.wait_for_frames())
|
|
109
|
+
bias_ns = np.int64(ns_now()) - np.int64(
|
|
110
|
+
aligned_frames.get_frame_metadata(
|
|
111
|
+
rs.frame_metadata_value.sensor_timestamp) * 1_000)
|
|
112
|
+
|
|
113
|
+
while not self.stop_event.is_set():
|
|
114
|
+
aligned_frames = self._align.process(
|
|
115
|
+
self._pipeline.wait_for_frames())
|
|
116
|
+
cur_ns = ns_now()
|
|
117
|
+
if p.sens_ts:
|
|
118
|
+
sen_ts = bias_ns + np.int64(
|
|
119
|
+
aligned_frames.get_frame_metadata(
|
|
120
|
+
rs.frame_metadata_value.sensor_timestamp) * 1_000)
|
|
121
|
+
if cur_ns < sen_ts:
|
|
122
|
+
sen_ts = cur_ns
|
|
123
|
+
ts_ns = sen_ts
|
|
124
|
+
else:
|
|
125
|
+
ts_ns = cur_ns
|
|
126
|
+
|
|
127
|
+
color_frame = aligned_frames.get_color_frame()
|
|
128
|
+
if color_frame is not None and on_rgb is not None:
|
|
129
|
+
bgr = np.asanyarray(color_frame.get_data())
|
|
130
|
+
rgb_img = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
|
|
131
|
+
on_rgb(ts_ns, rgb_img)
|
|
132
|
+
|
|
133
|
+
depth_frame = aligned_frames.get_depth_frame()
|
|
134
|
+
if depth_frame is not None and on_depth is not None:
|
|
135
|
+
depth = np.asanyarray(depth_frame.get_data())
|
|
136
|
+
on_depth(ts_ns, depth)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
from .cam import HexCamRgb, HexCamRgbParams
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"HexCamRgb",
|
|
13
|
+
"HexCamRgbParams",
|
|
14
|
+
]
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
################################################################
|
|
4
|
+
# Copyright 2026 Dong Zhaorui. All rights reserved.
|
|
5
|
+
# Author: Dong Zhaorui 847235539@qq.com
|
|
6
|
+
# Date : 2026-03-23
|
|
7
|
+
################################################################
|
|
8
|
+
|
|
9
|
+
import multiprocessing as mp
|
|
10
|
+
import platform
|
|
11
|
+
import cv2
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from typing import Optional
|
|
14
|
+
|
|
15
|
+
from hex_util_runtime import HexRate, ns_now
|
|
16
|
+
|
|
17
|
+
from ..base import HexCamBase, HexCamBaseParams, HexFrameCallback
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class HexCamRgbParams(HexCamBaseParams):
|
|
22
|
+
cam_path: str = "/dev/video0"
|
|
23
|
+
width_crop: tuple[int, int] = (0, 640)
|
|
24
|
+
height_crop: tuple[int, int] = (0, 480)
|
|
25
|
+
exposure: int = 100
|
|
26
|
+
temperature: int = 4000
|
|
27
|
+
sens_ts: bool = True
|
|
28
|
+
on_rgb: Optional[HexFrameCallback] = field(default=None)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class HexCamRgb(HexCamBase):
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
params: HexCamRgbParams = HexCamRgbParams(),
|
|
36
|
+
) -> None:
|
|
37
|
+
super().__init__()
|
|
38
|
+
self._params = params
|
|
39
|
+
self.init_vars()
|
|
40
|
+
|
|
41
|
+
def init_vars(self):
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
def init_camera(self) -> None:
|
|
45
|
+
self._cap = self._open_capture()
|
|
46
|
+
if not self._cap.isOpened():
|
|
47
|
+
raise RuntimeError(
|
|
48
|
+
f"Failed to open camera: {self._params.cam_path}")
|
|
49
|
+
self._init_event.set()
|
|
50
|
+
|
|
51
|
+
def deinit_camera(self) -> None:
|
|
52
|
+
main = mp.current_process().name == "MainProcess"
|
|
53
|
+
if main and self.is_working():
|
|
54
|
+
self.stop()
|
|
55
|
+
if self._cap is not None:
|
|
56
|
+
self._cap.release()
|
|
57
|
+
self._cap = None
|
|
58
|
+
|
|
59
|
+
def _open_capture(self) -> cv2.VideoCapture:
|
|
60
|
+
p = self._params
|
|
61
|
+
if platform.system() == "Linux":
|
|
62
|
+
cap = cv2.VideoCapture(p.cam_path, cv2.CAP_V4L2)
|
|
63
|
+
else:
|
|
64
|
+
cap = cv2.VideoCapture(p.cam_path)
|
|
65
|
+
if not cap.isOpened():
|
|
66
|
+
return cap
|
|
67
|
+
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
|
|
68
|
+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, float(p.width))
|
|
69
|
+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, float(p.height))
|
|
70
|
+
cap.set(cv2.CAP_PROP_FPS, float(p.frame_rate))
|
|
71
|
+
ae_open = 1.0 if platform.system() == "Windows" else 3.0
|
|
72
|
+
ae_close = 0.0 if platform.system() == "Windows" else 1.0
|
|
73
|
+
ae_value = 10000 * (2**p.exposure) if platform.system(
|
|
74
|
+
) == "Windows" else float(p.exposure)
|
|
75
|
+
cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, ae_open)
|
|
76
|
+
if p.exposure != 0:
|
|
77
|
+
cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, ae_close)
|
|
78
|
+
cap.set(cv2.CAP_PROP_EXPOSURE, ae_value)
|
|
79
|
+
cap.set(cv2.CAP_PROP_AUTO_WB, 1)
|
|
80
|
+
if p.temperature != 0:
|
|
81
|
+
cap.set(cv2.CAP_PROP_AUTO_WB, 0)
|
|
82
|
+
cap.set(cv2.CAP_PROP_WB_TEMPERATURE, float(p.temperature))
|
|
83
|
+
|
|
84
|
+
# print camera info
|
|
85
|
+
print(
|
|
86
|
+
f"Camera info: {cap.get(cv2.CAP_PROP_FRAME_WIDTH)}x{cap.get(cv2.CAP_PROP_FRAME_HEIGHT)}@{cap.get(cv2.CAP_PROP_FPS)}"
|
|
87
|
+
)
|
|
88
|
+
return cap
|
|
89
|
+
|
|
90
|
+
def work_loop(self) -> None:
|
|
91
|
+
fps = float(self._params.frame_rate)
|
|
92
|
+
w_crop = self._params.width_crop
|
|
93
|
+
h_crop = self._params.height_crop
|
|
94
|
+
cb = self._params.on_rgb
|
|
95
|
+
|
|
96
|
+
rate = HexRate(fps)
|
|
97
|
+
while not self.stop_event.is_set():
|
|
98
|
+
rate.sleep()
|
|
99
|
+
ret, frame = self._cap.read()
|
|
100
|
+
if not ret or frame is None:
|
|
101
|
+
continue
|
|
102
|
+
frame = frame[h_crop[0]:h_crop[1], w_crop[0]:w_crop[1]]
|
|
103
|
+
rgb_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
104
|
+
ts = ns_now()
|
|
105
|
+
if cb is not None:
|
|
106
|
+
cb(ts, rgb_img)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hex_driver_camera
|
|
3
|
+
Version: 0.0.1a1
|
|
4
|
+
Summary: HEXFELLOW Camera Driver
|
|
5
|
+
Author-email: Dong Zhaorui <dzr159@gmail.com>
|
|
6
|
+
Maintainer-email: jecjune <zejun.chen@hexfellow.com>, Dong Zhaorui <dzr159@gmail.com>
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
Project-URL: Homepage, https://github.com/hexfellow/hex_driver_camera
|
|
9
|
+
Project-URL: Repository, https://github.com/hexfellow/hex_driver_camera.git
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/hexfellow/hex_driver_camera/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/hexfellow/hex_driver_camera/wiki
|
|
12
|
+
Keywords: hex,driver,camera
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: hex_util_runtime<0.1.0,>=0.0.0
|
|
26
|
+
Requires-Dist: opencv-python>=4.10.0
|
|
27
|
+
Provides-Extra: berxel
|
|
28
|
+
Requires-Dist: berxel_py_wrapper>=2.0.182; extra == "berxel"
|
|
29
|
+
Provides-Extra: realsense
|
|
30
|
+
Requires-Dist: pyrealsense2>=2.56.5.9235; extra == "realsense"
|
|
31
|
+
Provides-Extra: all
|
|
32
|
+
Requires-Dist: berxel_py_wrapper>=2.0.182; extra == "all"
|
|
33
|
+
Requires-Dist: pyrealsense2>=2.56.5.9235; extra == "all"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# hex_driver_camera
|
|
37
|
+
Hex camera driver
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
hex_driver_camera/__init__.py
|
|
6
|
+
hex_driver_camera/base.py
|
|
7
|
+
hex_driver_camera.egg-info/PKG-INFO
|
|
8
|
+
hex_driver_camera.egg-info/SOURCES.txt
|
|
9
|
+
hex_driver_camera.egg-info/dependency_links.txt
|
|
10
|
+
hex_driver_camera.egg-info/requires.txt
|
|
11
|
+
hex_driver_camera.egg-info/top_level.txt
|
|
12
|
+
hex_driver_camera/cam_berxel/__init__.py
|
|
13
|
+
hex_driver_camera/cam_berxel/cam.py
|
|
14
|
+
hex_driver_camera/cam_dummy/__init__.py
|
|
15
|
+
hex_driver_camera/cam_dummy/cam.py
|
|
16
|
+
hex_driver_camera/cam_realsense/__init__.py
|
|
17
|
+
hex_driver_camera/cam_realsense/cam.py
|
|
18
|
+
hex_driver_camera/cam_rgb/__init__.py
|
|
19
|
+
hex_driver_camera/cam_rgb/cam.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hex_driver_camera
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools >= 61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "hex_driver_camera"
|
|
7
|
+
version = "0.0.1-alpha.1"
|
|
8
|
+
description = "HEXFELLOW Camera Driver"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
authors = [{ name = "Dong Zhaorui", email = "dzr159@gmail.com" }]
|
|
12
|
+
maintainers = [
|
|
13
|
+
{ name = "jecjune", email = "zejun.chen@hexfellow.com" },
|
|
14
|
+
{ name = "Dong Zhaorui", email = "dzr159@gmail.com" },
|
|
15
|
+
]
|
|
16
|
+
keywords = ["hex", "driver", "camera"]
|
|
17
|
+
requires-python = ">=3.10"
|
|
18
|
+
dependencies = ["hex_util_runtime>=0.0.0,<0.1.0", "opencv-python>=4.10.0"]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 4 - Beta",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"Intended Audience :: Science/Research",
|
|
23
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Operating System :: POSIX :: Linux",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
berxel = ["berxel_py_wrapper>=2.0.182"]
|
|
33
|
+
realsense = ["pyrealsense2>=2.56.5.9235"]
|
|
34
|
+
all = ["berxel_py_wrapper>=2.0.182", "pyrealsense2>=2.56.5.9235"]
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/hexfellow/hex_driver_camera"
|
|
38
|
+
Repository = "https://github.com/hexfellow/hex_driver_camera.git"
|
|
39
|
+
"Bug Tracker" = "https://github.com/hexfellow/hex_driver_camera/issues"
|
|
40
|
+
Documentation = "https://github.com/hexfellow/hex_driver_camera/wiki"
|