-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
198 lines (159 loc) · 4.92 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from dataclasses import dataclass
from functools import cache
from pathlib import Path
from typing import Annotated
from typing import Literal
from pydantic import AfterValidator
from pydantic import BaseModel
from pydantic import DirectoryPath
from pydantic import Field
from pydantic import UrlConstraints
from pydantic_core import Url
from pydantic_settings import BaseSettings
from pydantic_settings import SettingsConfigDict
@dataclass(slots=True)
class RequireParts:
username: bool = False
password: bool = False
def __call__(self, v: Url) -> Url:
for field in self.__slots__:
if getattr(v, field) is None:
raise ValueError(
f'{field.capitalize()} is required in url '
'"schema://username:password@host:port/path?'
'query#fragment".'
)
return v
OpcUaUrl = Annotated[
Url,
UrlConstraints(
allowed_schemes=['opc.tcp', 'http', 'https'],
host_required=True,
default_port=4840,
),
AfterValidator(RequireParts(username=True, password=True)),
]
class OPCUASettings(BaseModel):
"""Settings for OPC UA communication."""
url: OpcUaUrl = Field(
...,
description='Url of the form '
'"[opc.tcp|http|https]://[user]:[pass]@[host]:[port]/".',
)
nodeid: str = Field(
...,
description='NodeID of the form "ns=<ns>;s=<s>.',
min_length=1,
)
_static = Path(__file__).parent / 'static'
class Settings(BaseSettings):
STATIC_DIR: DirectoryPath = Field(
_static,
description='Path to static dir.',
frozen=True,
)
EXECUTION_PROVIDER: Literal['CPU'] = Field(
'CPU',
description='Execution provider to run the ONNX model. Default: CPU.',
)
CAMERA_WIDTH: int = Field(
900,
ge=1,
description='Width of the camera image. Cropped from the orignal '
'image. Use OffsetX and OffsetY to change the crop position.',
)
CAMERA_HEIGHT: int = Field(
900,
ge=1,
description='Height of the camera image. Cropped from the orignal '
'Use OffsetX and OffsetY to change the crop position.',
)
CAMERA_PIXEL_FORMAT: Literal['BayerRG8', 'BGR8'] = Field(
'BayerRG8',
description='Pixel format. Controls how colors are represented and '
'with how many bits.',
)
CAMERA_EXPOSURE_TIME: float = Field(
10000,
description='Default value for the exposure time. Can be controlled '
'on the frontend.',
)
CAMERA_DIGITAL_SHIFT: int = Field(
0,
description='Default value for the digital shift. Can be controlled '
'on the frontend.',
)
CAMERA_GAMMA: float = Field(
2,
description='Default value for the gamma. Can be controlled on the '
'frontend.',
)
CAMERA_ROTATION: Literal[0, 90, 180, 270, '0', '90', '180', '270'] = Field(
180,
description='Rotate the image by some degrees.',
)
CAMERA_RING_LIGHT: bool = Field(
True,
description='Whether to turn on the connected light.',
)
CAMERA_RING_LIGHT_BRIGHTNESS: int = Field(
45, description='Ring light brigthness.'
)
BANDWIDTH_UNIT: Literal['MB', 'Mbit'] = Field(
'Mbit',
description='Unit to use for displayed bandwidth.',
)
MODE_MAX_SIZE: bool = Field(
False,
description='Whether to set the camera to send the maximum image '
'size.',
)
IMAGE_WIDTH: int = Field(
300,
ge=1,
description='Width of the images. Default: 300.',
)
IMAGE_HEIGHT: int = Field(
300,
ge=1,
description='Height of the images. Default: 300.',
)
MODEL: Literal['cnn'] = Field(
'cnn',
description='Model to use. Default: ["cnn"].',
)
MODEL_VERSION: str = Field(
'72',
description='Model version.',
)
MODEL_SAMPLING_FACTOR: int = Field(
5,
description='Sampling factor for the model evaluation. '
'This value sets the sampling rate to evaluate every n-th image. '
'Default: 5 (every 5th frame).',
)
ANNOTATION_FONT_SCALE: float = Field(
2e-3,
description='Font scale for annotations. Default: 2e-3.',
)
ANNOTATION_THICKNESS_SCALE: float = Field(
3e-3,
description='Thickness scale for annotations. Default: 2e-3.',
)
AFTER_CAPTURE_TIMER: float = Field(
5,
description='Time in s to show the image after capture. Default: 5.',
)
OPCUA: OPCUASettings | None = Field(
None,
description='Settings for OPC UA connection.',
)
model_config = SettingsConfigDict(
env_file=['.env', _static / 'env/.env'],
extra='allow',
case_sensitive=False,
env_nested_delimiter='__',
)
@cache
def get_settings() -> Settings:
return Settings()