synapsefrc 2025.0.0a1__py3-none-any.whl
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.
- synapse/__init__.py +7 -0
- synapse/__version__.py +8 -0
- synapse/bcolors.py +53 -0
- synapse/callback.py +41 -0
- synapse/core/__init__.py +8 -0
- synapse/core/camera_factory.py +589 -0
- synapse/core/config.py +120 -0
- synapse/core/global_settings.py +180 -0
- synapse/core/pipeline.py +175 -0
- synapse/core/runtime_handler.py +1329 -0
- synapse/core/settings_api.py +1074 -0
- synapse/core/synapse.py +697 -0
- synapse/hardware/__init__.py +3 -0
- synapse/hardware/deviceactions.py +25 -0
- synapse/hardware/metrics.py +344 -0
- synapse/log.py +130 -0
- synapse/pipelines/__init__.py +3 -0
- synapse/pipelines/apriltag_pipeline.py +578 -0
- synapse/pipelines/color_pipeline.py +18 -0
- synapse/pipelines/system/__init__.py +3 -0
- synapse/pipelines/system/calibration_pipeline.py +289 -0
- synapse/stypes.py +33 -0
- synapse/util.py +94 -0
- synapse_installer/__init__.py +3 -0
- synapse_installer/__main__.py +118 -0
- synapse_installer/create.py +92 -0
- synapse_installer/deploy.py +204 -0
- synapse_installer/installer.py +170 -0
- synapse_installer/lockfile.py +128 -0
- synapse_installer/setup_service.py +138 -0
- synapse_installer/sync.py +188 -0
- synapse_installer/util.py +50 -0
- synapse_net/__init__.py +3 -0
- synapse_net/devicenetworking.py +158 -0
- synapse_net/nt_client.py +102 -0
- synapse_net/proto/__init__.py +3 -0
- synapse_net/proto/settings/__init__.py +3 -0
- synapse_net/proto/settings/v1/__init__.py +236 -0
- synapse_net/proto/v1/__init__.py +299 -0
- synapse_net/socketServer.py +155 -0
- synapse_ui/404.html +1 -0
- synapse_ui/__init__.py +0 -0
- synapse_ui/_next/static/chunks/254-5e0873b33d8a578c.js +1 -0
- synapse_ui/_next/static/chunks/292-f8b32671046a285a.js +1 -0
- synapse_ui/_next/static/chunks/357-680cf05e0e82dd7e.js +1 -0
- synapse_ui/_next/static/chunks/361-28c3b26cc8169b1a.js +1 -0
- synapse_ui/_next/static/chunks/378-0ffa52aec96434c6.js +1 -0
- synapse_ui/_next/static/chunks/481-8dc028d1dd6d04e3.js +1 -0
- synapse_ui/_next/static/chunks/4bd1b696-4419cc93057fcf32.js +1 -0
- synapse_ui/_next/static/chunks/538-c83627cb33d007c5.js +1 -0
- synapse_ui/_next/static/chunks/684-5141943b21dfd4f0.js +1 -0
- synapse_ui/_next/static/chunks/830-a04198f1ba05ac15.js +1 -0
- synapse_ui/_next/static/chunks/app/_not-found/page-a3cc41667356e2de.js +1 -0
- synapse_ui/_next/static/chunks/app/camera/page-7eec9bea3d3d2e37.js +1 -0
- synapse_ui/_next/static/chunks/app/dashboard/page-a7043ad99dc3711b.js +1 -0
- synapse_ui/_next/static/chunks/app/layout-f14bd68f29682176.js +1 -0
- synapse_ui/_next/static/chunks/app/page-ec236b307660d45e.js +1 -0
- synapse_ui/_next/static/chunks/app/settings/page-8ee527295a3bf763.js +1 -0
- synapse_ui/_next/static/chunks/framework-f593a28cde54158e.js +1 -0
- synapse_ui/_next/static/chunks/main-app-fd9a227689f484c5.js +1 -0
- synapse_ui/_next/static/chunks/main-e4ca48fba1998376.js +1 -0
- synapse_ui/_next/static/chunks/pages/_app-da15c11dea942c36.js +1 -0
- synapse_ui/_next/static/chunks/pages/_error-cc3f077a18ea1793.js +1 -0
- synapse_ui/_next/static/chunks/polyfills-42372ed130431b0a.js +1 -0
- synapse_ui/_next/static/chunks/webpack-3a68ca926a023bb8.js +1 -0
- synapse_ui/_next/static/css/6c6e36550f6d43fe.css +1 -0
- synapse_ui/_next/static/eZ11JDBFLGlqkpQixapSS/_buildManifest.js +1 -0
- synapse_ui/_next/static/eZ11JDBFLGlqkpQixapSS/_ssgManifest.js +1 -0
- synapse_ui/camera.html +2 -0
- synapse_ui/camera.txt +24 -0
- synapse_ui/dashboard.html +1 -0
- synapse_ui/dashboard.txt +24 -0
- synapse_ui/favicon.ico +0 -0
- synapse_ui/file.svg +1 -0
- synapse_ui/globe.svg +1 -0
- synapse_ui/index.html +1 -0
- synapse_ui/index.txt +24 -0
- synapse_ui/next.svg +1 -0
- synapse_ui/settings.html +1 -0
- synapse_ui/settings.txt +24 -0
- synapse_ui/vercel.svg +1 -0
- synapse_ui/window.svg +1 -0
- synapsefrc-2025.0.0a1.dist-info/LICENSE +674 -0
- synapsefrc-2025.0.0a1.dist-info/METADATA +36 -0
- synapsefrc-2025.0.0a1.dist-info/RECORD +88 -0
- synapsefrc-2025.0.0a1.dist-info/WHEEL +5 -0
- synapsefrc-2025.0.0a1.dist-info/entry_points.txt +2 -0
- synapsefrc-2025.0.0a1.dist-info/top_level.txt +4 -0
synapse/__init__.py
ADDED
synapse/__version__.py
ADDED
synapse/bcolors.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2025 Dan Peled
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
from enum import Enum
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TextTarget(Enum):
|
|
9
|
+
kTerminal = "terminal"
|
|
10
|
+
kHTML = "html"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class MarkupColors:
|
|
14
|
+
"""
|
|
15
|
+
Markup-style wrappers for text formatting using custom tags,
|
|
16
|
+
to be parsed for either terminal (via rich) or HTML.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
@staticmethod
|
|
20
|
+
def header(text: str) -> str:
|
|
21
|
+
return MarkupColors.okgreen(MarkupColors.bold(text))
|
|
22
|
+
|
|
23
|
+
@staticmethod
|
|
24
|
+
def okblue(text: str) -> str:
|
|
25
|
+
return f"[blue]{text}[/blue]"
|
|
26
|
+
|
|
27
|
+
@staticmethod
|
|
28
|
+
def okcyan(text: str) -> str:
|
|
29
|
+
return f"[cyan]{text}[/cyan]"
|
|
30
|
+
|
|
31
|
+
@staticmethod
|
|
32
|
+
def okgreen(text: str) -> str:
|
|
33
|
+
return f"[green]{text}[/green]"
|
|
34
|
+
|
|
35
|
+
@staticmethod
|
|
36
|
+
def warning(text: str) -> str:
|
|
37
|
+
return f"[yellow]{text}[/yellow]"
|
|
38
|
+
|
|
39
|
+
@staticmethod
|
|
40
|
+
def fail(text: str) -> str:
|
|
41
|
+
return f"[red]{text}[/red]"
|
|
42
|
+
|
|
43
|
+
@staticmethod
|
|
44
|
+
def bold(text: str) -> str:
|
|
45
|
+
return f"[bold]{text}[/bold]"
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def underline(text: str) -> str:
|
|
49
|
+
return f"[underline]{text}[/underline]"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def parseTextStyle(text: str, target: TextTarget = TextTarget.kTerminal) -> str:
|
|
53
|
+
return text
|
synapse/callback.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2025 Dan Peled
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
import traceback
|
|
6
|
+
from typing import Callable, Generic, List, ParamSpec
|
|
7
|
+
|
|
8
|
+
from .log import err
|
|
9
|
+
|
|
10
|
+
P = ParamSpec("P")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Callback(Generic[P]):
|
|
14
|
+
"""Manage a list of callbacks"""
|
|
15
|
+
|
|
16
|
+
def __init__(self) -> None:
|
|
17
|
+
self.callbacks: List[Callable[P, None]] = []
|
|
18
|
+
|
|
19
|
+
def add(self, callback: Callable[P, None]) -> None:
|
|
20
|
+
"""Add a callback"""
|
|
21
|
+
self.callbacks.append(callback)
|
|
22
|
+
|
|
23
|
+
def remove(self, callback: Callable[P, None]) -> None:
|
|
24
|
+
"""Remove a callback if present."""
|
|
25
|
+
try:
|
|
26
|
+
self.callbacks.remove(callback)
|
|
27
|
+
except ValueError:
|
|
28
|
+
err(f"Callback {callback} not found for removal.")
|
|
29
|
+
|
|
30
|
+
def call(self, *args: P.args, **kwargs: P.kwargs) -> None:
|
|
31
|
+
"""Call all callbacks"""
|
|
32
|
+
for cb in self.callbacks:
|
|
33
|
+
try:
|
|
34
|
+
cb(*args, **kwargs)
|
|
35
|
+
except Exception as error:
|
|
36
|
+
errString = f"Exception in callback {cb}:\n" + "".join(
|
|
37
|
+
traceback.format_exception(type(error), error, error.__traceback__)
|
|
38
|
+
)
|
|
39
|
+
err(errString)
|
|
40
|
+
|
|
41
|
+
__call__ = call
|
synapse/core/__init__.py
ADDED
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2025 Dan Peled
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
4
|
+
|
|
5
|
+
import queue
|
|
6
|
+
import socket
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
from abc import ABC, abstractmethod
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from enum import Enum
|
|
12
|
+
from functools import cache
|
|
13
|
+
from typing import Any, Dict, List, Optional, Tuple, Type, Union
|
|
14
|
+
|
|
15
|
+
import cv2
|
|
16
|
+
import numpy as np
|
|
17
|
+
from cscore import (CameraServer, CvSink, UsbCamera, VideoCamera, VideoMode,
|
|
18
|
+
VideoSource)
|
|
19
|
+
from cv2.typing import Size
|
|
20
|
+
from ntcore import NetworkTable, NetworkTableEntry, NetworkTableInstance
|
|
21
|
+
from synapse.log import warn
|
|
22
|
+
from synapse_net.nt_client import NtClient
|
|
23
|
+
from synapse_net.proto.v1 import CalibrationDataProto, CameraProto
|
|
24
|
+
from wpimath import geometry
|
|
25
|
+
|
|
26
|
+
from ..stypes import CameraID, Frame, PipelineID, Resolution
|
|
27
|
+
from ..util import listToTransform3d, transform3dToList
|
|
28
|
+
|
|
29
|
+
PropName = str
|
|
30
|
+
PropertMetaDict = Dict[PropName, Dict[str, Union[int, float]]]
|
|
31
|
+
ResolutionString = str
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class CameraPropKeys(Enum):
|
|
35
|
+
kBrightness = "brightness"
|
|
36
|
+
kContrast = "contrast"
|
|
37
|
+
kSaturation = "saturation"
|
|
38
|
+
kHue = "hue"
|
|
39
|
+
kGain = "gain"
|
|
40
|
+
kExposure = "exposure"
|
|
41
|
+
kWhiteBalanceTemperature = "white_balance_temperature"
|
|
42
|
+
kSharpness = "sharpness"
|
|
43
|
+
kOrientation = "orientation"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
CSCORE_TO_CV_PROPS = {
|
|
47
|
+
"brightness": cv2.CAP_PROP_BRIGHTNESS,
|
|
48
|
+
"contrast": cv2.CAP_PROP_CONTRAST,
|
|
49
|
+
"saturation": cv2.CAP_PROP_SATURATION,
|
|
50
|
+
"hue": cv2.CAP_PROP_HUE,
|
|
51
|
+
"gain": cv2.CAP_PROP_GAIN,
|
|
52
|
+
"exposure": cv2.CAP_PROP_EXPOSURE,
|
|
53
|
+
"white_balance_temperature": cv2.CAP_PROP_WHITE_BALANCE_BLUE_U,
|
|
54
|
+
"sharpness": cv2.CAP_PROP_SHARPNESS,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
CV_TO_CSCORE_PROPS = {v: k for k, v in CSCORE_TO_CV_PROPS.items()}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class CameraSettingsKeys(Enum):
|
|
61
|
+
kViewID = "view_id"
|
|
62
|
+
kRecord = "record"
|
|
63
|
+
kPipeline = "pipeline"
|
|
64
|
+
kPipelineType = "pipeline_t"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def getCameraTableName(cameraIndex: CameraID) -> str:
|
|
68
|
+
return f"camera{cameraIndex}"
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass
|
|
72
|
+
class CalibrationData:
|
|
73
|
+
matrix: List[float]
|
|
74
|
+
distCoeff: List[float]
|
|
75
|
+
meanErr: float
|
|
76
|
+
measuredRes: Resolution
|
|
77
|
+
|
|
78
|
+
def toDict(self) -> Dict[str, Any]:
|
|
79
|
+
return {
|
|
80
|
+
CameraConfigKey.kMatrix.value: self.matrix,
|
|
81
|
+
CameraConfigKey.kDistCoeff.value: self.distCoeff,
|
|
82
|
+
CameraConfigKey.kMeasuredRes.value: self.measuredRes,
|
|
83
|
+
CameraConfigKey.kMeanErr.value: self.meanErr,
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@staticmethod
|
|
87
|
+
def fromDict(data: Dict[str, Any]) -> "CalibrationData":
|
|
88
|
+
return CalibrationData(
|
|
89
|
+
matrix=data[CameraConfigKey.kMatrix.value],
|
|
90
|
+
distCoeff=data[CameraConfigKey.kDistCoeff.value],
|
|
91
|
+
measuredRes=data[CameraConfigKey.kMeasuredRes.value],
|
|
92
|
+
meanErr=data[CameraConfigKey.kMeanErr.value],
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def toProto(self, cameraIndex: CameraID) -> CalibrationDataProto:
|
|
96
|
+
return CalibrationDataProto(
|
|
97
|
+
camera_index=cameraIndex,
|
|
98
|
+
mean_error=self.meanErr,
|
|
99
|
+
resolution="x".join([str(dim) for dim in self.measuredRes]),
|
|
100
|
+
camera_matrix=self.matrix,
|
|
101
|
+
dist_coeffs=self.distCoeff,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass
|
|
106
|
+
class CameraConfig:
|
|
107
|
+
name: str
|
|
108
|
+
id: str
|
|
109
|
+
transform: geometry.Transform3d
|
|
110
|
+
calibration: Dict[ResolutionString, CalibrationData]
|
|
111
|
+
defaultPipeline: int
|
|
112
|
+
streamRes: Resolution
|
|
113
|
+
|
|
114
|
+
def toDict(self) -> Dict[str, Any]:
|
|
115
|
+
return {
|
|
116
|
+
CameraConfigKey.kName.value: self.name,
|
|
117
|
+
CameraConfigKey.kPath.value: self.id,
|
|
118
|
+
CameraConfigKey.kTransform.value: transform3dToList(self.transform),
|
|
119
|
+
CameraConfigKey.kDefaultPipeline.value: self.defaultPipeline,
|
|
120
|
+
CameraConfigKey.kStreamRes.value: list(self.streamRes),
|
|
121
|
+
CameraConfigKey.kCalibration.value: {
|
|
122
|
+
resolution: calib.toDict()
|
|
123
|
+
for resolution, calib in self.calibration.items()
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
@staticmethod
|
|
128
|
+
def fromDict(data: Dict[str, Any]) -> "CameraConfig":
|
|
129
|
+
calib = {
|
|
130
|
+
key: CalibrationData.fromDict(calib)
|
|
131
|
+
for key, calib in data.get(CameraConfigKey.kCalibration.value, {}).items()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return CameraConfig(
|
|
135
|
+
name=data[CameraConfigKey.kName.value],
|
|
136
|
+
id=data[CameraConfigKey.kPath.value],
|
|
137
|
+
streamRes=data[CameraConfigKey.kStreamRes.value],
|
|
138
|
+
transform=listToTransform3d(data[CameraConfigKey.kTransform.value]),
|
|
139
|
+
defaultPipeline=data[CameraConfigKey.kDefaultPipeline.value],
|
|
140
|
+
calibration=calib,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class CameraConfigKey(Enum):
|
|
145
|
+
kName = "name"
|
|
146
|
+
kPath = "id"
|
|
147
|
+
kDefaultPipeline = "default_pipeline"
|
|
148
|
+
kMatrix = "matrix"
|
|
149
|
+
kDistCoeff = "distCoeffs"
|
|
150
|
+
kMeasuredRes = "measured_res"
|
|
151
|
+
kStreamRes = "stream_res"
|
|
152
|
+
kTransform = "transform"
|
|
153
|
+
kCalibration = "calibration"
|
|
154
|
+
kMeanErr = "mean_err"
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
@cache
|
|
158
|
+
def getCameraTable(cameraIndex: CameraID) -> NetworkTable:
|
|
159
|
+
return (
|
|
160
|
+
NetworkTableInstance.getDefault()
|
|
161
|
+
.getTable(NtClient.NT_TABLE)
|
|
162
|
+
.getSubTable(getCameraTableName(cameraIndex))
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def cscoreToOpenCVProp(prop: str) -> Optional[int]:
|
|
167
|
+
return CSCORE_TO_CV_PROPS.get(prop)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def opencvToCscoreProp(prop: int) -> Optional[str]:
|
|
171
|
+
return CV_TO_CSCORE_PROPS.get(prop)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class SynapseCamera(ABC):
|
|
175
|
+
def __init__(self, name: str) -> None:
|
|
176
|
+
self.name: str = name
|
|
177
|
+
self.stream: str = ""
|
|
178
|
+
|
|
179
|
+
@classmethod
|
|
180
|
+
@abstractmethod
|
|
181
|
+
def create(
|
|
182
|
+
cls,
|
|
183
|
+
*_,
|
|
184
|
+
path: Union[str, int],
|
|
185
|
+
name: str = "",
|
|
186
|
+
index: CameraID,
|
|
187
|
+
) -> "SynapseCamera": ...
|
|
188
|
+
|
|
189
|
+
def setIndex(self, cameraIndex: CameraID) -> None:
|
|
190
|
+
self.cameraIndex: CameraID = cameraIndex
|
|
191
|
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
192
|
+
|
|
193
|
+
s.connect(("8.8.8.8", 80))
|
|
194
|
+
ip = s.getsockname()[0]
|
|
195
|
+
s.close()
|
|
196
|
+
self.stream = f"http://{ip}:{1181 + cameraIndex}/?action=stream/stream.mjpeg"
|
|
197
|
+
|
|
198
|
+
@abstractmethod
|
|
199
|
+
def grabFrame(self) -> Tuple[bool, Optional[Frame]]: ...
|
|
200
|
+
|
|
201
|
+
@abstractmethod
|
|
202
|
+
def close(self) -> None: ...
|
|
203
|
+
|
|
204
|
+
@abstractmethod
|
|
205
|
+
def isConnected(self) -> bool: ...
|
|
206
|
+
|
|
207
|
+
@abstractmethod
|
|
208
|
+
def setProperty(self, prop: str, value: Union[int, float]) -> None: ...
|
|
209
|
+
|
|
210
|
+
@abstractmethod
|
|
211
|
+
def getProperty(self, prop: str) -> Union[int, float, None]: ...
|
|
212
|
+
|
|
213
|
+
@abstractmethod
|
|
214
|
+
def setVideoMode(self, fps: int, width: int, height: int) -> None: ...
|
|
215
|
+
|
|
216
|
+
@abstractmethod
|
|
217
|
+
def getResolution(self) -> Size: ...
|
|
218
|
+
|
|
219
|
+
@abstractmethod
|
|
220
|
+
def getSupportedResolutions(self) -> List[Size]: ...
|
|
221
|
+
|
|
222
|
+
@abstractmethod
|
|
223
|
+
def getPropertyMeta(self) -> Optional[PropertMetaDict]: ...
|
|
224
|
+
|
|
225
|
+
@abstractmethod
|
|
226
|
+
def getMaxFPS(self) -> float: ...
|
|
227
|
+
|
|
228
|
+
def getSettingEntry(self, key: str) -> Optional[NetworkTableEntry]:
|
|
229
|
+
if hasattr(self, "cameraIndex"):
|
|
230
|
+
table: NetworkTable = getCameraTable(self.cameraIndex)
|
|
231
|
+
entry: NetworkTableEntry = table.getEntry(key)
|
|
232
|
+
return entry
|
|
233
|
+
return None
|
|
234
|
+
|
|
235
|
+
def getSetting(self, key: str, defaultValue: Any) -> Any:
|
|
236
|
+
if hasattr(self, "cameraIndex"):
|
|
237
|
+
table: NetworkTable = getCameraTable(self.cameraIndex)
|
|
238
|
+
entry: NetworkTableEntry = table.getEntry(key)
|
|
239
|
+
if not entry.exists():
|
|
240
|
+
entry.setValue(defaultValue)
|
|
241
|
+
return entry.getValue()
|
|
242
|
+
return None
|
|
243
|
+
|
|
244
|
+
def setSetting(self, key: str, value: Any) -> None:
|
|
245
|
+
if hasattr(self, "cameraIndex"):
|
|
246
|
+
table: NetworkTable = getCameraTable(self.cameraIndex)
|
|
247
|
+
entry: NetworkTableEntry = table.getEntry(key)
|
|
248
|
+
entry.setValue(value)
|
|
249
|
+
|
|
250
|
+
@property
|
|
251
|
+
def viewID(self) -> str:
|
|
252
|
+
entry = self.getSettingEntry(CameraSettingsKeys.kViewID.value)
|
|
253
|
+
if entry is not None:
|
|
254
|
+
return entry.getString("")
|
|
255
|
+
return ""
|
|
256
|
+
|
|
257
|
+
@viewID.setter
|
|
258
|
+
def viewID(self, value: str) -> None:
|
|
259
|
+
self.setSetting(CameraSettingsKeys.kViewID.value, value)
|
|
260
|
+
|
|
261
|
+
@property
|
|
262
|
+
def record(self) -> bool:
|
|
263
|
+
entry = self.getSettingEntry(CameraSettingsKeys.kRecord.value)
|
|
264
|
+
if entry is not None:
|
|
265
|
+
return entry.getBoolean(False)
|
|
266
|
+
return False
|
|
267
|
+
|
|
268
|
+
@record.setter
|
|
269
|
+
def record(self, value: bool) -> None:
|
|
270
|
+
self.setSetting(CameraSettingsKeys.kViewID.value, value)
|
|
271
|
+
|
|
272
|
+
@property
|
|
273
|
+
def pipeline(self) -> int:
|
|
274
|
+
entry = self.getSettingEntry(CameraSettingsKeys.kPipeline.value)
|
|
275
|
+
if entry is not None:
|
|
276
|
+
return entry.getInteger(0)
|
|
277
|
+
return 0
|
|
278
|
+
|
|
279
|
+
@pipeline.setter
|
|
280
|
+
def pipeline(self, value: int) -> None:
|
|
281
|
+
self.setSetting(CameraSettingsKeys.kPipeline.value, value)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
class OpenCvCamera(SynapseCamera):
|
|
285
|
+
def __init__(self, name: str) -> None:
|
|
286
|
+
super().__init__(name=name)
|
|
287
|
+
self.cap: cv2.VideoCapture
|
|
288
|
+
|
|
289
|
+
@classmethod
|
|
290
|
+
def create(
|
|
291
|
+
cls,
|
|
292
|
+
*_,
|
|
293
|
+
name: str = "",
|
|
294
|
+
path: Union[str, int],
|
|
295
|
+
index: CameraID,
|
|
296
|
+
) -> "OpenCvCamera":
|
|
297
|
+
inst = OpenCvCamera(name)
|
|
298
|
+
assert isinstance(path, int) or isinstance(path, str), (
|
|
299
|
+
f"No valid path for camera {index}"
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
if isinstance(path, int):
|
|
303
|
+
inst.cap = cv2.VideoCapture(path)
|
|
304
|
+
if isinstance(path, str):
|
|
305
|
+
inst.cap = cv2.VideoCapture(path, cv2.CAP_V4L2)
|
|
306
|
+
|
|
307
|
+
return inst
|
|
308
|
+
|
|
309
|
+
def getSupportedResolutions(self) -> List[Size]:
|
|
310
|
+
return [self.getResolution()]
|
|
311
|
+
|
|
312
|
+
def getPropertyMeta(self) -> Optional[PropertMetaDict]:
|
|
313
|
+
return None
|
|
314
|
+
|
|
315
|
+
def grabFrame(self) -> Tuple[bool, Optional[Frame]]:
|
|
316
|
+
return self.cap.read()
|
|
317
|
+
|
|
318
|
+
def isConnected(self) -> bool:
|
|
319
|
+
return self.cap.isOpened()
|
|
320
|
+
|
|
321
|
+
def close(self) -> None:
|
|
322
|
+
self.cap.release()
|
|
323
|
+
|
|
324
|
+
def setProperty(self, prop: str, value: Union[int, float]) -> None:
|
|
325
|
+
if isinstance(prop, int) and self.cap:
|
|
326
|
+
propInt = cscoreToOpenCVProp(prop)
|
|
327
|
+
if propInt is not None:
|
|
328
|
+
self.cap.set(propInt, value)
|
|
329
|
+
|
|
330
|
+
def getProperty(self, prop: str) -> Union[int, float, None]:
|
|
331
|
+
if isinstance(prop, int) and self.cap:
|
|
332
|
+
propInt = cscoreToOpenCVProp(prop)
|
|
333
|
+
if propInt is not None:
|
|
334
|
+
return self.cap.get(propInt)
|
|
335
|
+
else:
|
|
336
|
+
return None
|
|
337
|
+
return None
|
|
338
|
+
|
|
339
|
+
def setVideoMode(self, fps: int, width: int, height: int) -> None:
|
|
340
|
+
if self.cap:
|
|
341
|
+
self.cap.set(cv2.CAP_PROP_FPS, fps)
|
|
342
|
+
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
|
|
343
|
+
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
|
|
344
|
+
|
|
345
|
+
def getResolution(self) -> Size:
|
|
346
|
+
return (
|
|
347
|
+
int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
|
348
|
+
int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
def getMaxFPS(self) -> float:
|
|
352
|
+
desired_fps = 120
|
|
353
|
+
self.cap.set(cv2.CAP_PROP_FPS, desired_fps)
|
|
354
|
+
actual_fps = self.cap.get(cv2.CAP_PROP_FPS)
|
|
355
|
+
return actual_fps
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
class CsCoreCamera(SynapseCamera):
|
|
359
|
+
def __init__(self, name: str) -> None:
|
|
360
|
+
super().__init__(name)
|
|
361
|
+
self.camera: VideoCamera
|
|
362
|
+
self.frameBuffer: np.ndarray
|
|
363
|
+
self.sink: CvSink
|
|
364
|
+
self.propertyMeta: PropertMetaDict = {}
|
|
365
|
+
self._properties: Dict[str, Any] = {}
|
|
366
|
+
self._videoModes: List[Any] = []
|
|
367
|
+
self._validVideoModes: List[VideoMode] = []
|
|
368
|
+
|
|
369
|
+
self._frameQueue: queue.Queue[Tuple[bool, Optional[np.ndarray]]] = queue.Queue(
|
|
370
|
+
maxsize=5
|
|
371
|
+
)
|
|
372
|
+
self._running: bool = False
|
|
373
|
+
self._thread: Optional[threading.Thread] = None
|
|
374
|
+
self._lock = threading.Lock() # Add this
|
|
375
|
+
|
|
376
|
+
@classmethod
|
|
377
|
+
def create(
|
|
378
|
+
cls,
|
|
379
|
+
*_,
|
|
380
|
+
path: Union[str, int],
|
|
381
|
+
name: str = "",
|
|
382
|
+
index: CameraID,
|
|
383
|
+
) -> "CsCoreCamera":
|
|
384
|
+
inst = CsCoreCamera(name)
|
|
385
|
+
|
|
386
|
+
if isinstance(path, int):
|
|
387
|
+
inst.camera = UsbCamera(f"USB Camera {index}", path)
|
|
388
|
+
elif isinstance(path, str):
|
|
389
|
+
inst.camera = UsbCamera(f"USB Camera {index}", path)
|
|
390
|
+
|
|
391
|
+
inst.sink = CameraServer.getVideo(inst.camera)
|
|
392
|
+
inst.sink.getProperty("auto_exposure").set(0)
|
|
393
|
+
|
|
394
|
+
# Cache properties and metadata
|
|
395
|
+
props = inst.camera.enumerateProperties()
|
|
396
|
+
inst._properties = {prop.getName(): prop for prop in props}
|
|
397
|
+
inst.propertyMeta = {
|
|
398
|
+
name: {
|
|
399
|
+
"min": prop.getMin(),
|
|
400
|
+
"max": prop.getMax(),
|
|
401
|
+
"default": prop.getDefault(),
|
|
402
|
+
}
|
|
403
|
+
for name, prop in inst._properties.items()
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
# Cache video modes and valid resolutions
|
|
407
|
+
inst._videoModes = inst.camera.enumerateVideoModes()
|
|
408
|
+
inst._validVideoModes = [mode for mode in inst._videoModes]
|
|
409
|
+
inst.setVideoMode(1000, 1920, 1080)
|
|
410
|
+
|
|
411
|
+
# Initialize frame buffer to current resolution
|
|
412
|
+
mode = inst.camera.getVideoMode()
|
|
413
|
+
inst.frameBuffer = np.zeros((mode.height, mode.width, 3), dtype=np.uint8)
|
|
414
|
+
|
|
415
|
+
# Start background frame grabbing thread
|
|
416
|
+
inst._startFrameThread()
|
|
417
|
+
|
|
418
|
+
return inst
|
|
419
|
+
|
|
420
|
+
def getPropertyMeta(self) -> Optional[PropertMetaDict]:
|
|
421
|
+
return self.propertyMeta
|
|
422
|
+
|
|
423
|
+
def _startFrameThread(self) -> None:
|
|
424
|
+
if self._running:
|
|
425
|
+
return
|
|
426
|
+
self._running = True
|
|
427
|
+
self._thread = threading.Thread(target=self._frameGrabberLoop, daemon=True)
|
|
428
|
+
self._thread.start()
|
|
429
|
+
|
|
430
|
+
def _frameGrabberLoop(self) -> None:
|
|
431
|
+
while not self.isConnected():
|
|
432
|
+
time.sleep(0.1)
|
|
433
|
+
while self._running:
|
|
434
|
+
with self._lock: # Protect frame operations
|
|
435
|
+
result = self.sink.grabFrame(self.frameBuffer)
|
|
436
|
+
if len(result) > 0:
|
|
437
|
+
ret, frame = result
|
|
438
|
+
hasFrame = ret != 0
|
|
439
|
+
if hasFrame:
|
|
440
|
+
frame_copy = frame.copy() # no lock needed here; copying is safe
|
|
441
|
+
try:
|
|
442
|
+
self._frameQueue.put_nowait((hasFrame, frame_copy))
|
|
443
|
+
except queue.Full:
|
|
444
|
+
try:
|
|
445
|
+
self._frameQueue.get_nowait()
|
|
446
|
+
except queue.Empty:
|
|
447
|
+
pass
|
|
448
|
+
self._frameQueue.put_nowait((hasFrame, frame_copy))
|
|
449
|
+
else:
|
|
450
|
+
self._waitForNextFrame()
|
|
451
|
+
self._waitForNextFrame()
|
|
452
|
+
|
|
453
|
+
def _waitForNextFrame(self):
|
|
454
|
+
if self.isConnected():
|
|
455
|
+
if self.camera.getVideoMode().fps > 0:
|
|
456
|
+
time.sleep(
|
|
457
|
+
1.0 / self.camera.getVideoMode().fps / 2.0
|
|
458
|
+
) # Half the expected frame interval
|
|
459
|
+
|
|
460
|
+
def grabFrame(self) -> Tuple[bool, Optional[np.ndarray]]:
|
|
461
|
+
with self._lock: # Protect frame buffer access
|
|
462
|
+
try:
|
|
463
|
+
return self._frameQueue.get_nowait()
|
|
464
|
+
except queue.Empty:
|
|
465
|
+
return False, None
|
|
466
|
+
|
|
467
|
+
def isConnected(self) -> bool:
|
|
468
|
+
return self.camera.isConnected()
|
|
469
|
+
|
|
470
|
+
def close(self) -> None:
|
|
471
|
+
self._running = False
|
|
472
|
+
if self._thread is not None:
|
|
473
|
+
self._thread.join(timeout=1.0)
|
|
474
|
+
# Properly close camera connection
|
|
475
|
+
self.camera.setConnectionStrategy(
|
|
476
|
+
VideoSource.ConnectionStrategy.kConnectionForceClose
|
|
477
|
+
)
|
|
478
|
+
|
|
479
|
+
def setProperty(self, prop: str, value: Union[int, float, str]) -> None:
|
|
480
|
+
if prop == "resolution" and isinstance(value, str):
|
|
481
|
+
resolution = value.split("x")
|
|
482
|
+
width = int(resolution[0])
|
|
483
|
+
height = int(resolution[1])
|
|
484
|
+
self.setVideoMode(int(self.getMaxFPS()), width, height)
|
|
485
|
+
elif prop in self._properties:
|
|
486
|
+
meta = self.propertyMeta[prop]
|
|
487
|
+
value = int(np.clip(value, meta["min"], meta["max"]))
|
|
488
|
+
self._properties[prop].set(value)
|
|
489
|
+
|
|
490
|
+
def getProperty(self, prop: str) -> Union[int, float, None]:
|
|
491
|
+
if prop in self._properties:
|
|
492
|
+
return self._properties[prop].get()
|
|
493
|
+
return None
|
|
494
|
+
|
|
495
|
+
def setVideoMode(self, fps: int, width: int, height: int, _fallback=False) -> None:
|
|
496
|
+
pixelFormat = VideoMode.PixelFormat.kMJPEG
|
|
497
|
+
|
|
498
|
+
for mode in self._validVideoModes:
|
|
499
|
+
if (
|
|
500
|
+
width == mode.width
|
|
501
|
+
and height == mode.height
|
|
502
|
+
and mode.pixelFormat == pixelFormat
|
|
503
|
+
):
|
|
504
|
+
self.camera.setVideoMode(
|
|
505
|
+
width=width,
|
|
506
|
+
height=height,
|
|
507
|
+
fps=mode.fps,
|
|
508
|
+
pixelFormat=pixelFormat,
|
|
509
|
+
)
|
|
510
|
+
self.frameBuffer = np.zeros((height, width, 3), dtype=np.uint8)
|
|
511
|
+
return
|
|
512
|
+
|
|
513
|
+
if not _fallback:
|
|
514
|
+
# Find the largest available resolution and try again
|
|
515
|
+
largest_mode = max(
|
|
516
|
+
(m for m in self._validVideoModes if m.pixelFormat == pixelFormat),
|
|
517
|
+
key=lambda m: m.width * m.height,
|
|
518
|
+
default=None,
|
|
519
|
+
)
|
|
520
|
+
|
|
521
|
+
if largest_mode:
|
|
522
|
+
warn(
|
|
523
|
+
f"Invalid video mode (width={width}, height={height}). "
|
|
524
|
+
f"Falling back to largest available mode ({largest_mode.width}x{largest_mode.height})."
|
|
525
|
+
)
|
|
526
|
+
self.setVideoMode(
|
|
527
|
+
fps, largest_mode.width, largest_mode.height, _fallback=True
|
|
528
|
+
)
|
|
529
|
+
return
|
|
530
|
+
|
|
531
|
+
warn(
|
|
532
|
+
f"No valid video modes found for pixelFormat={pixelFormat}. "
|
|
533
|
+
f"Camera default settings will be used."
|
|
534
|
+
)
|
|
535
|
+
|
|
536
|
+
def getResolution(self) -> Resolution:
|
|
537
|
+
videoMode = self.camera.getVideoMode()
|
|
538
|
+
return (videoMode.width, videoMode.height)
|
|
539
|
+
|
|
540
|
+
def getMaxFPS(self) -> float:
|
|
541
|
+
return self.camera.getVideoMode().fps
|
|
542
|
+
|
|
543
|
+
def getSupportedResolutions(self) -> List[Size]:
|
|
544
|
+
resolutions = []
|
|
545
|
+
for videomode in self._validVideoModes:
|
|
546
|
+
resolutions.append((videomode.width, videomode.height))
|
|
547
|
+
return resolutions
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
class CameraFactory:
|
|
551
|
+
kOpenCV: Type[SynapseCamera] = OpenCvCamera
|
|
552
|
+
kCameraServer: Type[SynapseCamera] = CsCoreCamera
|
|
553
|
+
kDefault: Type[SynapseCamera] = kCameraServer
|
|
554
|
+
|
|
555
|
+
@classmethod
|
|
556
|
+
def create(
|
|
557
|
+
cls,
|
|
558
|
+
*_,
|
|
559
|
+
cameraType: Type[SynapseCamera] = kDefault,
|
|
560
|
+
cameraIndex: CameraID,
|
|
561
|
+
path: Union[str, int],
|
|
562
|
+
name: str = "",
|
|
563
|
+
) -> "SynapseCamera":
|
|
564
|
+
cam: SynapseCamera = cameraType.create(
|
|
565
|
+
path=path,
|
|
566
|
+
name=name,
|
|
567
|
+
index=cameraIndex,
|
|
568
|
+
)
|
|
569
|
+
cam.setIndex(cameraIndex)
|
|
570
|
+
return cam
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
def cameraToProto(
|
|
574
|
+
camid: CameraID,
|
|
575
|
+
name: str,
|
|
576
|
+
camera: SynapseCamera,
|
|
577
|
+
pipelineIndex: PipelineID,
|
|
578
|
+
defaultPipeline: PipelineID,
|
|
579
|
+
kind: str,
|
|
580
|
+
) -> CameraProto:
|
|
581
|
+
return CameraProto(
|
|
582
|
+
name=name,
|
|
583
|
+
index=camid,
|
|
584
|
+
stream_path=camera.stream,
|
|
585
|
+
kind=kind,
|
|
586
|
+
pipeline_index=pipelineIndex,
|
|
587
|
+
default_pipeline=defaultPipeline,
|
|
588
|
+
max_fps=int(camera.getMaxFPS()),
|
|
589
|
+
)
|