GHIElectronics.Endpoint.Devices.Camera 0.5.1__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.
- GHIElectronics/Endpoint/Devices/Camera/__init__.py +283 -0
- ghielectronics_endpoint_devices_camera-0.5.1.dist-info/METADATA +20 -0
- ghielectronics_endpoint_devices_camera-0.5.1.dist-info/RECORD +5 -0
- ghielectronics_endpoint_devices_camera-0.5.1.dist-info/WHEEL +5 -0
- ghielectronics_endpoint_devices_camera-0.5.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
"""Camera of Endpoint.
|
|
2
|
+
|
|
3
|
+
UsbCamera: any USB Video Class webcam (the DeviceName of a Webcam from
|
|
4
|
+
UsbHostController). Pictures come as JPEG files, whether the camera sends
|
|
5
|
+
MJPEG or raw YUYV pictures:
|
|
6
|
+
|
|
7
|
+
from GHIElectronics.Endpoint.Devices.Camera import UsbCamera
|
|
8
|
+
|
|
9
|
+
with UsbCamera(e.DeviceName) as camera:
|
|
10
|
+
camera.Capture("/media/usb0/photo.jpg") # a JPEG
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import ctypes
|
|
14
|
+
import os
|
|
15
|
+
from enum import IntEnum
|
|
16
|
+
|
|
17
|
+
from GHIElectronics.Endpoint.Core._event import Event
|
|
18
|
+
from GHIElectronics.Endpoint.Core._native import CameraFrameCallback, UnpluggedCallback, native
|
|
19
|
+
|
|
20
|
+
__all__ = ["UsbCamera", "CameraControl", "CameraControlValue", "CameraFormat", "CameraPixelFormat",
|
|
21
|
+
"FrameReceivedEventArgs"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class CameraPixelFormat(IntEnum):
|
|
25
|
+
"""What a camera sends (pictures always come out as JPEG)"""
|
|
26
|
+
|
|
27
|
+
Mjpeg = 0
|
|
28
|
+
"""Motion JPEG: small and fast, the way most webcams send large pictures"""
|
|
29
|
+
Yuyv = 1
|
|
30
|
+
"""Raw YUYV pictures: every webcam has them; the library makes the JPEG"""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class CameraFormat:
|
|
34
|
+
"""A picture size a camera has, what it sends for it, and its highest frame rate"""
|
|
35
|
+
|
|
36
|
+
def __init__(self, width, height, pixelFormat, maxFrameRate):
|
|
37
|
+
self.Width = width
|
|
38
|
+
self.Height = height
|
|
39
|
+
self.PixelFormat = pixelFormat
|
|
40
|
+
self.MaxFrameRate = maxFrameRate
|
|
41
|
+
|
|
42
|
+
def __repr__(self):
|
|
43
|
+
return f"{self.Width}x{self.Height} {self.PixelFormat.name} {self.MaxFrameRate} fps"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class CameraControl(IntEnum):
|
|
47
|
+
"""A camera's picture controls (each camera has some of them)"""
|
|
48
|
+
|
|
49
|
+
Brightness = 0
|
|
50
|
+
Contrast = 1
|
|
51
|
+
Saturation = 2
|
|
52
|
+
Hue = 3
|
|
53
|
+
Gamma = 4
|
|
54
|
+
Gain = 5
|
|
55
|
+
Sharpness = 6
|
|
56
|
+
BacklightCompensation = 7
|
|
57
|
+
AutoWhiteBalance = 8
|
|
58
|
+
"""1: on, 0: off (then WhiteBalanceTemperature counts)"""
|
|
59
|
+
WhiteBalanceTemperature = 9
|
|
60
|
+
AutoExposure = 10
|
|
61
|
+
"""1: on, 0: off (then Exposure counts)"""
|
|
62
|
+
Exposure = 11
|
|
63
|
+
PowerLineFrequency = 12
|
|
64
|
+
"""0: off, 1: 50 Hz, 2: 60 Hz (against flicker from lights)"""
|
|
65
|
+
AutoFocus = 13
|
|
66
|
+
"""1: on, 0: off (then Focus counts)"""
|
|
67
|
+
Focus = 14
|
|
68
|
+
Zoom = 15
|
|
69
|
+
Pan = 16
|
|
70
|
+
Tilt = 17
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class CameraControlValue:
|
|
74
|
+
"""A camera control's range and value, in the camera's own units"""
|
|
75
|
+
|
|
76
|
+
def __init__(self, control, minimum, maximum, step, defaultValue, value):
|
|
77
|
+
self.Control = control
|
|
78
|
+
self.Minimum = minimum
|
|
79
|
+
self.Maximum = maximum
|
|
80
|
+
self.Step = step
|
|
81
|
+
self.DefaultValue = defaultValue
|
|
82
|
+
self.Value = value
|
|
83
|
+
|
|
84
|
+
def __repr__(self):
|
|
85
|
+
return f"{self.Control.name} {self.Value} ({self.Minimum} to {self.Maximum}, default {self.DefaultValue})"
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class FrameReceivedEventArgs:
|
|
89
|
+
"""A camera's frame while streaming: a JPEG (bytes)"""
|
|
90
|
+
|
|
91
|
+
def __init__(self, jpeg, width, height):
|
|
92
|
+
self.Jpeg = jpeg
|
|
93
|
+
self.Width = width
|
|
94
|
+
self.Height = height
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class _EventProperty:
|
|
98
|
+
def __init__(self, attribute):
|
|
99
|
+
self.attribute = attribute
|
|
100
|
+
|
|
101
|
+
def __get__(self, target, owner=None):
|
|
102
|
+
return self if target is None else getattr(target, self.attribute)
|
|
103
|
+
|
|
104
|
+
def __set__(self, target, value):
|
|
105
|
+
if value is not getattr(target, self.attribute, None):
|
|
106
|
+
raise TypeError("Use += and -= to add and remove an event handler")
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class UsbCamera:
|
|
110
|
+
"""A webcam: any USB Video Class camera, opened with the DeviceName of a Webcam
|
|
111
|
+
from UsbHostController ("/dev/video0"; the number can change when it is
|
|
112
|
+
plugged in again). The first picture waits until the camera's automatic
|
|
113
|
+
exposure has settled (about a second); while pictures are taken the camera
|
|
114
|
+
keeps running, so the next ones come at once. At first the size is 640x480.
|
|
115
|
+
|
|
116
|
+
with UsbCamera(e.DeviceName) as camera:
|
|
117
|
+
camera.SetResolution(1280, 720)
|
|
118
|
+
camera.Capture("/media/usb0/photo.jpg")
|
|
119
|
+
|
|
120
|
+
camera.FrameReceived += lambda sender, frame: send_to_browser(frame.Jpeg)
|
|
121
|
+
camera.StartStreaming()
|
|
122
|
+
|
|
123
|
+
The events come on the camera's own thread. A camera stays alive until it is disposed.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
FrameReceived = _EventProperty("_frame_event")
|
|
127
|
+
"""A frame while streaming: handler(sender, e) with e.Jpeg (bytes), e.Width, e.Height"""
|
|
128
|
+
Disconnected = _EventProperty("_disconnected_event")
|
|
129
|
+
"""The camera was unplugged: handler(sender, e)"""
|
|
130
|
+
|
|
131
|
+
def __init__(self, deviceName):
|
|
132
|
+
self._frame_event = Event()
|
|
133
|
+
self._disconnected_event = Event()
|
|
134
|
+
self._handle = None
|
|
135
|
+
self._streaming = False
|
|
136
|
+
self.DeviceName = str(deviceName)
|
|
137
|
+
# The callbacks are kept: the library calls them
|
|
138
|
+
self._frame_callback = CameraFrameCallback(self._frame)
|
|
139
|
+
self._unplugged_callback = UnpluggedCallback(lambda context: self._disconnected_event(self, None))
|
|
140
|
+
handle = native.out_handle()
|
|
141
|
+
native.camera_open(self.DeviceName.encode(), self._frame_callback, self._unplugged_callback, None,
|
|
142
|
+
native.ref(handle))
|
|
143
|
+
self._handle = handle
|
|
144
|
+
native.keep_alive(self)
|
|
145
|
+
name, connected, count = ctypes.create_string_buffer(256), native.out_int(), native.out_int()
|
|
146
|
+
native.camera_info(handle, name, len(name), native.ref(connected), native.ref(count))
|
|
147
|
+
self.Name = name.value.decode(errors="replace")
|
|
148
|
+
"""The camera's name ("USB 2.0 Camera: USB Camera")"""
|
|
149
|
+
formats = []
|
|
150
|
+
for index in range(count.value):
|
|
151
|
+
width, height, pixel, rate = (native.out_int() for _ in range(4))
|
|
152
|
+
native.camera_format(handle, index, native.ref(width), native.ref(height), native.ref(pixel), native.ref(rate))
|
|
153
|
+
formats.append(CameraFormat(width.value, height.value, CameraPixelFormat(pixel.value), rate.value))
|
|
154
|
+
self.SupportedFormats = tuple(formats)
|
|
155
|
+
"""The sizes the camera has, with what it sends for each"""
|
|
156
|
+
|
|
157
|
+
def _h(self):
|
|
158
|
+
if self._handle is None:
|
|
159
|
+
raise native.disposed("UsbCamera")
|
|
160
|
+
return self._handle
|
|
161
|
+
|
|
162
|
+
def _frame(self, context, jpeg, length, width, height):
|
|
163
|
+
if len(self._frame_event):
|
|
164
|
+
self._frame_event(self, FrameReceivedEventArgs(ctypes.string_at(jpeg, length), width, height))
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def Connected(self):
|
|
168
|
+
if self._handle is None:
|
|
169
|
+
return False
|
|
170
|
+
connected, count = native.out_int(), native.out_int()
|
|
171
|
+
native.camera_info(self._handle, None, 0, native.ref(connected), native.ref(count))
|
|
172
|
+
return bool(connected.value)
|
|
173
|
+
|
|
174
|
+
def _resolution(self):
|
|
175
|
+
values = [native.out_int() for _ in range(4)]
|
|
176
|
+
native.camera_get_resolution(self._h(), *[native.ref(v) for v in values])
|
|
177
|
+
return [v.value for v in values]
|
|
178
|
+
|
|
179
|
+
@property
|
|
180
|
+
def Width(self):
|
|
181
|
+
return self._resolution()[0]
|
|
182
|
+
|
|
183
|
+
@property
|
|
184
|
+
def Height(self):
|
|
185
|
+
return self._resolution()[1]
|
|
186
|
+
|
|
187
|
+
@property
|
|
188
|
+
def PixelFormat(self):
|
|
189
|
+
return CameraPixelFormat(self._resolution()[2])
|
|
190
|
+
|
|
191
|
+
@property
|
|
192
|
+
def FrameRate(self):
|
|
193
|
+
return self._resolution()[3]
|
|
194
|
+
|
|
195
|
+
@property
|
|
196
|
+
def JpegQuality(self):
|
|
197
|
+
"""The JPEG quality (1 to 100) of pictures the library makes from raw YUYV pictures (85 at first)"""
|
|
198
|
+
quality = native.out_int()
|
|
199
|
+
native.camera_get_quality(self._h(), native.ref(quality))
|
|
200
|
+
return quality.value
|
|
201
|
+
|
|
202
|
+
@JpegQuality.setter
|
|
203
|
+
def JpegQuality(self, value):
|
|
204
|
+
native.camera_set_quality(self._h(), int(value))
|
|
205
|
+
|
|
206
|
+
@property
|
|
207
|
+
def IsStreaming(self):
|
|
208
|
+
return self._streaming
|
|
209
|
+
|
|
210
|
+
def SetResolution(self, width, height=None, frameRate=0):
|
|
211
|
+
"""A size the camera has: SetResolution(1280, 720) (MJPEG if the camera has it at that size,
|
|
212
|
+
else YUYV), or one of SupportedFormats as it is: SetResolution(format). frameRate 0: the
|
|
213
|
+
camera's highest for that size."""
|
|
214
|
+
if isinstance(width, CameraFormat):
|
|
215
|
+
if height is not None:
|
|
216
|
+
frameRate = height
|
|
217
|
+
native.camera_set_resolution(self._h(), width.Width, width.Height, int(frameRate), int(width.PixelFormat))
|
|
218
|
+
else:
|
|
219
|
+
native.camera_set_resolution(self._h(), int(width), int(height), int(frameRate), -1)
|
|
220
|
+
|
|
221
|
+
def Capture(self, path=None, timeout=5000):
|
|
222
|
+
"""Takes a picture: a JPEG (bytes), or saved in a file when a path is given. A
|
|
223
|
+
TimeoutException if the camera sends none in timeout milliseconds."""
|
|
224
|
+
data, length = ctypes.POINTER(ctypes.c_uint8)(), native.out_int()
|
|
225
|
+
native.camera_capture(self._h(), int(timeout), ctypes.byref(data), native.ref(length))
|
|
226
|
+
try:
|
|
227
|
+
jpeg = ctypes.string_at(data, length.value)
|
|
228
|
+
finally:
|
|
229
|
+
native.camera_free(data)
|
|
230
|
+
if path is None:
|
|
231
|
+
return jpeg
|
|
232
|
+
with open(os.fspath(path), "wb") as file:
|
|
233
|
+
file.write(jpeg)
|
|
234
|
+
return None
|
|
235
|
+
|
|
236
|
+
def StartStreaming(self):
|
|
237
|
+
"""Every frame comes as a JPEG in FrameReceived"""
|
|
238
|
+
native.camera_start_streaming(self._h())
|
|
239
|
+
self._streaming = True
|
|
240
|
+
|
|
241
|
+
def StopStreaming(self):
|
|
242
|
+
native.camera_stop_streaming(self._h())
|
|
243
|
+
self._streaming = False
|
|
244
|
+
|
|
245
|
+
def _get(self, control):
|
|
246
|
+
values = [native.out_int() for _ in range(6)]
|
|
247
|
+
native.camera_get_control(self._h(), int(control), *[native.ref(v) for v in values])
|
|
248
|
+
supported, minimum, maximum, step, default, value = (v.value for v in values)
|
|
249
|
+
return CameraControlValue(CameraControl(control), minimum, maximum, step, default, value) if supported else None
|
|
250
|
+
|
|
251
|
+
@property
|
|
252
|
+
def SupportedControls(self):
|
|
253
|
+
"""The controls this camera has"""
|
|
254
|
+
return [control for control in CameraControl if self._get(control) is not None]
|
|
255
|
+
|
|
256
|
+
def GetControl(self, control):
|
|
257
|
+
"""A control's range and value; a NotSupportedException if the camera does not have it"""
|
|
258
|
+
value = self._get(control)
|
|
259
|
+
if value is None:
|
|
260
|
+
from GHIElectronics.Endpoint.Core import NotSupportedException
|
|
261
|
+
raise NotSupportedException(f"The camera has no {CameraControl(control).name} control")
|
|
262
|
+
return value
|
|
263
|
+
|
|
264
|
+
def SetControl(self, control, value):
|
|
265
|
+
"""Sets a control, in the camera's own units (GetControl gives its range)"""
|
|
266
|
+
native.camera_set_control(self._h(), int(control), int(value))
|
|
267
|
+
|
|
268
|
+
def Dispose(self):
|
|
269
|
+
"""Stops the camera (a frame being delivered finishes first) and closes it"""
|
|
270
|
+
if self._handle is not None:
|
|
271
|
+
native.camera_close(self._handle)
|
|
272
|
+
self._handle = None
|
|
273
|
+
self._streaming = False
|
|
274
|
+
native.let_go(self)
|
|
275
|
+
|
|
276
|
+
def __enter__(self):
|
|
277
|
+
return self
|
|
278
|
+
|
|
279
|
+
def __exit__(self, *exception):
|
|
280
|
+
self.Dispose()
|
|
281
|
+
|
|
282
|
+
def __repr__(self):
|
|
283
|
+
return f"UsbCamera({self.DeviceName!r})"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: GHIElectronics.Endpoint.Devices.Camera
|
|
3
|
+
Version: 0.5.1
|
|
4
|
+
Summary: USB webcams and shared camera types for GHI Electronics Endpoint.
|
|
5
|
+
Author-email: "GHI Electronics, LLC" <support@ghielectronics.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://www.ghielectronics.com/
|
|
8
|
+
Project-URL: Repository, https://github.com/ghi-electronics/Endpoint-Libraries
|
|
9
|
+
Project-URL: Issues, https://github.com/ghi-electronics/Endpoint-Libraries/issues
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Requires-Dist: GHIElectronics.Endpoint.Core<0.6,>=0.5.1
|
|
15
|
+
|
|
16
|
+
# GHIElectronics.Endpoint.Devices.Camera
|
|
17
|
+
|
|
18
|
+
USB webcams and shared camera types for GHI Electronics Endpoint.
|
|
19
|
+
|
|
20
|
+
Part of the [GHI Electronics Endpoint](https://github.com/ghi-electronics/Endpoint-Libraries) libraries. See the repository README for the full package list and getting-started guide.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
GHIElectronics/Endpoint/Devices/Camera/__init__.py,sha256=tOiCBEGus_Q6rl3bFftC6rWJwD3cKfVmaomYppHlsls,10582
|
|
2
|
+
ghielectronics_endpoint_devices_camera-0.5.1.dist-info/METADATA,sha256=T5jMwnmgtxHFH4b9HLT1A7sKu1cthpPXDLt_Jj0oJhs,991
|
|
3
|
+
ghielectronics_endpoint_devices_camera-0.5.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
4
|
+
ghielectronics_endpoint_devices_camera-0.5.1.dist-info/top_level.txt,sha256=NpOZNgd0iquwrRPrTOhUDxc2V9TtNFLv5Z_Dx3YZtIU,15
|
|
5
|
+
ghielectronics_endpoint_devices_camera-0.5.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
GHIElectronics
|