-
Notifications
You must be signed in to change notification settings - Fork 0
/
picam.py
315 lines (296 loc) · 9.34 KB
/
picam.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import logging
import struct
from io import BytesIO
from socket import socket, AF_INET, SOCK_DGRAM
from typing import Union
import networkzero as nw0
from PIL import Image
from create_logger import create_logger
logger = create_logger(name=__name__, level=logging.DEBUG)
class RemotePiCam:
"""
A class to manage connecting, getting images, and controlling a remote
PiCam.
"""
def __init__(self, cam_name: str, port: int):
"""
Initiate the PiCam. This does not actually connect to the PiCam until
you call connect().
:param cam_name: The name of the PiCamera. This is used to discover
the camera.
:param port: The port to listen on.
"""
self._cam_name = cam_name
self._cam_address = None
self._port = port
self._server_socket = None
self._connection = None
self._connected = False
self.settings = {
"awb_mode": {
"selected": "auto",
"available": [
"off",
"auto",
"sunlight",
"cloudy",
"shade",
"tungsten",
"fluorescent",
"incandescent",
"flash",
"horizon"
]
},
"brightness": {
"min": 0,
"max": 100,
"value": 50
},
"contrast": {
"min": -100,
"max": 100,
"value": 0
},
"effect": {
"selected": "none",
"available": [
"none",
"negative",
"solarize",
"sketch",
"denoise",
"emboss",
"oilpaint",
"hatch",
"gpen",
"pastel",
"watercolor",
"film",
"blur",
"saturation",
"colorswap",
"washedout",
"posterise",
"colorpoint",
"colorbalance",
"cartoon",
"deinterlace1",
"deinterlace2"
]
},
"iso": {
"selected": 0,
"available": [
0,
100,
200,
320,
400,
500,
640,
800
]
},
"resolution": {
"selected": (720, 480),
"available": [
"128x96",
"160x120",
"160x144",
"176x144",
"180x132",
"180x135",
"192x144",
"234x60",
"256x192",
"320x200",
"320x240",
"320x288",
"320x400",
"352x288",
"352x240",
"384x256",
"384x288",
"392x72",
"400x300",
"460x55",
"480x320",
"468x32",
"468x60",
"512x342",
"512x384",
"544x372",
"640x350",
"640x480",
"640x576",
"704x576",
"720x350",
"720x400",
"720x480",
"720x483",
"720x484",
"720x486",
"720x540",
"720x576",
"729x348",
"768x576",
"800x600",
"832x624",
"856x480",
"896x600",
"960x720",
"1024x576",
"1024x768",
"1080x720",
"1152x768",
"1152x864",
"1152x870",
"1152x900",
"1280x720",
"1280x800",
"1280x854",
"1280x960",
"1280x992",
"1280x1024",
"1360x766",
"1365x768",
"1366x768",
"1365x1024",
"1400x788",
"1400x1050",
"1440x900",
"1520x856",
"1536x1536",
"1600x900",
"1600x1024",
"1600x1200",
"1792x1120",
"1792x1344",
"1824x1128",
"1824x1368",
"1856x1392",
"1920x1080",
"1920x1200",
"1920x1440",
"2000x1280",
"2048x1152",
"2048x1536",
"2048x2048",
"2500x1340",
"2560x1600",
"3072x2252",
"3600x2400"
]
},
"saturation": {
"min": -100,
"max": 100,
"value": 0
},
"servos": {
"enable": True,
"pan": {
"min": 0,
"max": 180,
"value": 90
},
"tilt": {
"min": 0,
"max": 60,
"value": 30
}
}
}
def _get_ip_addr(self) -> str:
"""
Get the IP address of this machine.
:return: A str.
"""
s = socket(AF_INET, SOCK_DGRAM)
try:
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
finally:
s.close()
def connect(self, timeout: int = 30) -> bool:
"""
Actually connect to the PiCam.
:param timeout: Wait up to x amount of seconds before giving up.
:return: A bool on whether we successfully connected or not.
"""
logger.debug(f"Attempting to connect to a PiCam with name "
f"{self._cam_name}")
try:
service = nw0.discover(self._cam_name, timeout)
except nw0.core.SocketTimedOutError:
return False
if service is None:
logger.warning("Failed to find PiCam")
return False
else:
logger.info(f"Successfully connected to PiCam '{self._cam_name}' "
f"at address {service}")
logger.debug(f"Opening socket on port {self._port}")
self._server_socket = socket()
self._server_socket.bind(("0.0.0.0", self._port))
self._server_socket.listen(0)
self.settings = nw0.send_message_to(service, self._get_ip_addr())
self._cam_address = service
self._connection = self._server_socket.accept()[0].makefile("rb")
self._connected = True
return True
def get_image(self) -> Union[tuple[Image.Image, int, int], None]:
"""
Get an image from the PiCam.
:return: A tuple of a PIL.Image, the size, and the frame's unix time in
milliseconds, or None if disconnected.
"""
failed = False
if not self.is_connected:
raise ValueError("Not connected")
try:
ull_size = struct.calcsize("<Q")
frame_time = struct.unpack("<Q",
self._connection.read(ull_size))[0]
ul_size = struct.calcsize("<L")
img_len = struct.unpack("<L", self._connection.read(ul_size))[0]
if img_len == 0:
raise ValueError("No more data is being sent, closing")
img_stream = BytesIO()
img_stream.write(self._connection.read(img_len))
img_stream.seek(0)
img_pil = Image.open(img_stream)
return img_pil, img_len, frame_time
except Exception:
failed = True
finally:
if failed:
self._connection.close()
self._server_socket.close()
self._connected = False
return None
def update_settings(self) -> bool:
"""
Update the settings.
:return: A bool on whether the settings were set or not.
"""
result = nw0.send_message_to(self._cam_address, self.settings)
self.settings = result[1]
return result[0]
def disconnect(self) -> None:
"""
Disconnect.
:return: None.
"""
logger.warning("Disconnecting")
self._connection.close()
self._server_socket.close()
self._connected = False
@property
def is_connected(self) -> bool:
"""
Get whether we are currently connected to a PiCam or not.
:return: A bool.
"""
return self._connected