-
Notifications
You must be signed in to change notification settings - Fork 0
/
lincameras.py
663 lines (552 loc) · 27.4 KB
/
lincameras.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
import sys
import platform
import time
import numpy as np
from PIL import Image, ImageDraw
from libcamera import Transform # @UnresolvedImport
from pprint import pformat
import constants
from cameras import BaseCamera, USBCamera
from forms.settings import LusbSettings, PiSettings
import vis_lib
from timesheet import Timesheet
from utilities import await_elapsed
class CamRes():
def __init__(self, res_str='0x0'):
res_vals = res_str.split('x')
self.width = int(res_vals[0])
self.height = int(res_vals[1])
def __getitem__(self, item):
return self.width if item == 0 else self.height
def __repr__(self):
return '{0:.0f}x{1:.0f}'.format(self.width, self.height)
class OpticalLusb(USBCamera):
def __init__(self, picam2, debug=False, logger=None):
self.settings = LusbSettings()
self.local_config_string = None
super().__init__()
self.debug = debug
self.logger = logger
self.revision = 'Picamera II USB'
# Windows | Linux | Darwin(mac)
self.linux = (platform.system() == 'Linux')
self._resolution = CamRes()
# Flip Image?
self.hflip = False
self.vflip = False
self.picam2 = picam2
@property
def resolution(self):
return self._resolution
@resolution.setter
def resolution(self, value):
if isinstance(value, str):
self._resolution = CamRes(value)
elif isinstance(value, tuple):
self._resolution = CamRes(
'{0:.0f}x{1:.0f}'.format(value[0], value[1]))
@property
def metadata(self):
return self.picam2.capture_metadata()
def __str__(self):
result = ''
result += 'resolution: ' + str(self._resolution) + '\n'
result += 'hflip: ' + str(self.hflip) + '\n'
result += 'vflip: ' + str(self.vflip) + '\n'
return result
def snap(self, cap_fmt):
'''
Capture an image from the usb camera
'''
try:
# snap to rgb array
img_array = self.capture(cap_fmt)
chans = len(img_array.shape)
if self.debug:
if chans == 3:
self.logger.debug(
'lusb snap {2}x{1}x{0}'.format(*img_array.shape))
else:
self.logger.debug(
'lusb snap {0}x{1}'.format(*img_array.shape))
except Exception as e:
err_line = sys.exc_info()[-1].tb_lineno
print('Error in picamera II usb snap: ' +
str(e) + ' on line ' + str(err_line))
return img_array
def capture(self, fmt=None):
try:
start = time.time()
timesheet = Timesheet('Optical-USB-Capture')
local_config_string = ''.join({k: str(v) for k, v in self.settings.get_settings_as_dict(
).items() if k not in ['client', 'queue']}.values())
timesheet.add('local config string assembled')
if self.debug and self.logger:
self.logger.debug('Current config:' + str(local_config_string))
if self.debug and self.logger:
self.logger.debug('Previous config:' +
str(self.local_config_string))
# Calculate the actual image size (accounting for rounding of the resolution)
width_px = self.resolution.width
height_px = self.resolution.height
array_width_px = width_px # (width_px + 31) // 32 * 32
array_height_px = height_px # (height_px + 15) // 16 * 16
# Take a frame
try:
start = time.time()
if local_config_string != self.local_config_string:
# Take image in requested format
capture_config = self.picam2.create_still_configuration(
main={
"size": (width_px, height_px)
}
)
timesheet.add('still config created')
# optimise config
if self.debug and self.logger:
self.logger.debug(
'Requested config:' + str(capture_config['main']))
if self.debug and self.logger:
self.logger.debug(
'Revised config:' + str(capture_config['main']))
if self.debug and self.logger:
self.logger.info(
'Stopping picamera2 to change configuration/controls')
self.picam2.stop() # in case left running
timesheet.add('camera stopped')
# apply modified config
self.picam2.configure(capture_config)
timesheet.add('modified config applied')
if self.debug and self.logger:
self.logger.debug(pformat(self.picam2.camera_controls))
# set controls
if self.debug and self.logger:
self.logger.info(
'Starting picamera2 following change to configuration/controls')
self.picam2.start()
timesheet.add('camera started')
pc2_cap_arr = self.picam2.capture_array()
timesheet.add('config change array captured')
self.local_config_string = local_config_string
else:
pc2_cap_arr = self.picam2.capture_array()
timesheet.add('no config change array captured')
if self.debug:
self.logger.debug('pc2_cap_arr sample:' +
str(pc2_cap_arr[0:10, 0:10]))
if self.debug:
cap_img = Image.fromarray(pc2_cap_arr)
cap_img.save(
'/dev/shm/pc2-usb-captured-{0}.jpg'.format(fmt))
if self.debug and self.logger:
self.logger.debug(
'size requested: {0},{1}'.format(width_px, height_px))
if self.debug and self.logger:
self.logger.debug('array predicted: {0},{1}'.format(
array_height_px, array_width_px))
if self.debug and self.logger:
self.logger.debug('pc2 captured: {} {} {} {}'.format(
pc2_cap_arr.shape,
pc2_cap_arr[:height_px, :width_px].shape,
pc2_cap_arr[:height_px,
:width_px - 1].flatten().shape,
pc2_cap_arr.dtype
))
# usb cameras can't do transforms
if self.hflip and not self.vflip:
pc2_cap_arr = np.fliplr(pc2_cap_arr)
timesheet.add('hflip')
if self.vflip and not self.hflip:
pc2_cap_arr = np.flipud(pc2_cap_arr)
timesheet.add('vflip')
if self.vflip and self.hflip:
pc2_cap_arr = np.fliplr(np.flipud(pc2_cap_arr))
timesheet.add('h and v flip')
# usb cameras only support rgb, not yuv
if fmt == 'rgb':
pc2_cap_arr = pc2_cap_arr.astype(np.uint8)
timesheet.add('array converted to uint8')
else:
pc2_cap_arr = vis_lib.rgb_to_gray(
pc2_cap_arr).astype(np.uint8)
timesheet.add('array converted to grayscale')
# annotation
if self.annotate:
reference_time_ms = (time.time() - time.monotonic()) * 1000
md = self.picam2.capture_metadata()
if self.debug and self.logger:
self.logger.debug(
'pc2 usb capture metadata: {0}'.format(md))
exposure_elapsed_time_ms = md['SensorTimestamp'] / 1000000
exposure_real_time_ms = reference_time_ms + exposure_elapsed_time_ms
exposure_real_time_fmtd = time.strftime(
'%Y-%m-%d %H:%M:%S', time.gmtime(exposure_real_time_ms / 1000))
self.annotate_text = '{0}.{1:0>3}'.format(
exposure_real_time_fmtd, int((exposure_real_time_ms % 1) * 1000))
banner_height_px = height_px // 16
margin_px = banner_height_px // 2
overlay_img = Image.new(
'L', (width_px - margin_px, banner_height_px))
overlay_img_draw = ImageDraw.Draw(overlay_img)
overlay_img_draw.text(
(0, 0), self.annotate_text, fill=255, font_size=banner_height_px)
if fmt == 'rgb':
pc2_cap_arr[margin_px:banner_height_px + margin_px,
margin_px:width_px, 0] = np.array(overlay_img)
pc2_cap_arr[margin_px:banner_height_px + margin_px,
margin_px:width_px, 1] = np.array(overlay_img)
pc2_cap_arr[margin_px:banner_height_px + margin_px,
margin_px:width_px, 2] = np.array(overlay_img)
else:
pc2_cap_arr[margin_px:banner_height_px + margin_px,
margin_px:width_px] = np.array(overlay_img)
timesheet.add('array annotated')
end = time.time()
elapsed = end - start
if self.debug and self.logger:
self.logger.debug('Camera Snapshot time: {0:.2f} secs'.format(
elapsed)) # Time in seconds, e.g. 5.38091952400282
self.logger.debug(timesheet)
except Exception as e1:
err_line = sys.exc_info()[-1].tb_lineno
if self.debug and self.logger:
self.logger.error(
'Error in OpticalLusb capture: ' + str(e1) + ' on line ' + str(err_line))
gray_img_arr = np.full((height_px, width_px), 127)
dummy_img = Image.fromarray(gray_img_arr)
dummy_draw = ImageDraw.Draw(dummy_img)
dummy_draw.text((10, 10), 'Picamera II USB', font_size=32)
if not self.linux:
dummy_draw.text(
(20, 60), 'Picamera II is only available on Linux!', font_size=20)
dummy_draw.text((20, 110), '{0} on line {1}'.format(
e1, err_line), font_size=20)
gray_img_arr = np.array(dummy_img)
(gray_height_px, gray_width_px) = gray_img_arr.shape
if self.debug and self.logger:
self.logger.debug(
'OpticalLusb usb capture - gray shape: {}x{}'.format(
(gray_width_px, gray_height_px)
)
)
# create a yuv plain grey image
y_array_size = array_width_px * array_height_px
yuv_array_size = y_array_size * 1.5
if self.debug and self.logger:
self.logger.debug(
'OpticalLusb capture - yuv_array_size: {0}'.format(yuv_array_size))
lum_img = np.zeros((array_height_px, array_width_px), np.uint8)
lum_img[0:gray_height_px, 0:gray_width_px] = gray_img_arr
l_img = lum_img.reshape(y_array_size)
if fmt == 'rgb':
if self.debug and self.logger:
self.logger.debug('3 channel output - colour')
pc2_cap_arr = l_img.reshape(
array_height_px, array_width_px)
else:
pc2_cap_arr = l_img.reshape(
array_height_px, array_width_px)
if self.debug and self.logger:
self.logger.debug('1 channel output - gray')
end = time.time()
elapsed = round(end - start, 3) # seconds
if self.debug and self.logger:
self.logger.debug(
'OpticalLusb capture - captured in {0} seconds'.format(elapsed))
return pc2_cap_arr
except Exception as e2:
err_line = sys.exc_info()[-1].tb_lineno
msg = 'Error in OpticalLusb usb capture: ' + \
str(e2) + ' on line ' + str(err_line)
if self.logger:
self.logger.error(msg)
else:
print(msg)
class OpticalPi(BaseCamera):
awb_mode_list = ["Auto", "Incandescant", "Tungsten",
"Flourescent", "Indoor", "Daylight", "Cloudy", "Custom"]
def __init__(self, picam2, device_index=0, debug=False, logger=None):
try:
self.settings = PiSettings()
self.local_config_string = None
BaseCamera.__init__(self)
self.device_index = device_index
self.debug = debug
self.logger = logger
self.revision = 'Pi Camera II'
self.framerate = 0
self._resolution = CamRes()
# Flip Image?
self.hflip = False
self.vflip = False
# set automatic white balance
self.awb_mode = 'off'
self.picam2 = picam2 # Picamera2()
except Exception as e:
err_line = sys.exc_info()[-1].tb_lineno
err_msg = 'Error in OpticalPi.__init__: ' + \
str(e) + ' on line ' + str(err_line)
if self.logger:
self.logger.error(err_msg)
else:
print(err_msg)
@property
def resolution(self):
return self._resolution
@resolution.setter
def resolution(self, value):
if isinstance(value, str):
self._resolution = CamRes(value)
elif isinstance(value, tuple):
self._resolution = CamRes(
'{0:.0f}x{1:.0f}'.format(value[0], value[1]))
@property
def metadata(self):
return self.picam2.capture_metadata()
def __str__(self):
result = ''
result += 'resolution: ' + str(self._resolution) + '\n'
result += 'hflip: ' + str(self.hflip) + '\n'
result += 'vflip: ' + str(self.vflip) + '\n'
result += 'awb_mode: {0}'.format(self.awb_mode) + '\n'
return result
def snap(self, cap_fmt):
'''
capture an image
handle the rounding-up of image size, and cropping back down
'''
cam_array = None
img_array = None
try:
if self.debug and self.logger:
self.logger.debug('snap format {0}'.format(cap_fmt))
if self.debug and self.logger:
self.logger.debug('res: {0} type: {1}'.format(
self.resolution, type(self.resolution)))
img_width_px = self.resolution.width
img_height_px = self.resolution.height
if self.debug and self.logger:
self.logger.debug('snap camera resolution {0}x{1}'.format(
img_width_px, img_height_px))
if cap_fmt == 'rgb':
chans = 3
else:
chans = 1
if self.debug and self.logger:
self.logger.debug('snap {2}x{1}x{0}'.format(
img_height_px, img_width_px, chans))
# snap to array
cam_array = self.capture(cap_fmt)
# finally crop to discard extra uninitialised pixels
img_array = cam_array[:img_height_px, :img_width_px]
if self.debug:
if chans == 3:
self.logger.debug(
'snap {2}x{1}x{0}'.format(*img_array.shape))
else:
self.logger.debug('snap {0}x{1}'.format(*img_array.shape))
except Exception as e:
err_line = sys.exc_info()[-1].tb_lineno
err_msg = 'Error in OpticalPi.snap: ' + \
str(e) + ' on line ' + str(err_line)
if self.logger is not None:
self.logger.error(err_msg)
else:
print(err_msg)
return img_array
def capture(self, fmt=None):
try:
start = time.time()
local_config_string = ''.join({k: str(v) for k, v in self.settings.get_settings_as_dict(
).items() if k not in ['client', 'queue']}.values())
if self.debug and self.logger:
self.logger.debug('Current config:' + str(local_config_string))
if self.debug and self.logger:
self.logger.debug('Previous config:' +
str(self.local_config_string))
width_px = self.resolution.width
height_px = self.resolution.height
# Take a frame
try:
start = time.time()
if local_config_string != self.local_config_string:
# Take image in requested format
trfrm = Transform(hflip=self.hflip, vflip=self.vflip)
capture_config = self.picam2.create_still_configuration(
main={
"size": (width_px, height_px),
"format": "YUV420" if fmt == 'yuv' else "BGR888"
},
transform=trfrm,
buffer_count=1 # must be one to restrict lag!
)
# optimise config
if self.debug and self.logger:
self.logger.debug(
'Requested config:' + str(capture_config['main']))
self.picam2.align_configuration(capture_config)
if self.debug and self.logger:
self.logger.debug(
'Revised config:' + str(capture_config['main']))
if self.debug and self.logger:
self.logger.info(
'Stopping picamera2 to change configuration/controls')
self.picam2.stop() # in case left running
# apply modified config
self.picam2.configure(capture_config)
# set controls
# Automatic White Balance
try:
if self.awb_mode is not None and self.awb_mode.lower() == 'off' and (self.redgain > 0 or self.bluegain > 0):
if self.debug and self.logger:
self.logger.debug(
'picamera2 capture - enabling manual gain, disabling awb')
self.picam2.controls.ColourGains = (
self.redgain, self.bluegain)
self.picam2.controls.AwbEnable = 0
self.awb_mode = 'off'
pc2_awb_mode = 0 # auto
elif self.awb_mode is not None and self.awb_mode.lower() == 'off':
if self.debug and self.logger:
self.logger.debug(
'picamera2 capture - disabling awb')
self.picam2.controls.AwbEnable = 0
pc2_awb_mode = 0 # auto
else:
if self.debug and self.logger:
self.logger.debug(
'picamera2 capture - enabling awb, disabling manual gain')
self.picam2.controls.AwbEnable = 1
if self.awb_mode in self.awb_mode_list:
pc2_awb_mode = self.awb_mode_list.index(
self.awb_mode)
if self.debug and self.logger:
self.logger.debug(
'picamera2 capture - using awb mode {0}'.format(pc2_awb_mode))
else:
pc2_awb_mode = 0 # auto
if self.debug and self.logger:
self.logger.debug(
'picamera2 capture - no mapping, using awb mode auto')
self.picam2.controls.AwbMode = pc2_awb_mode
except Exception:
pass
if self.debug and self.logger:
self.logger.info(
'Starting picamera2 following change to configuration/controls')
self.picam2.start()
pc2_cap_arr = self.picam2.capture_array() # "main")
if self.debug:
cap_img = Image.fromarray(pc2_cap_arr)
cap_img.save('/dev/shm/captured-{0}.jpg'.format(fmt))
if self.debug and self.logger:
self.logger.debug(
'size requested: {0},{1}'.format(width_px, height_px))
if self.debug and self.logger:
self.logger.debug('pc2 captured: {0} {1} {2}'.format(
pc2_cap_arr.shape,
pc2_cap_arr[:height_px, :width_px].shape,
pc2_cap_arr[:height_px,
:width_px - 1].flatten().shape
))
self.local_config_string = local_config_string
else:
pc2_cap_arr = self.picam2.capture_array() # "main")
# annotation
if self.annotate:
reference_time_ms = (time.time() - time.monotonic()) * 1000
md = self.picam2.capture_metadata()
exposure_elapsed_time_ms = (
md['SensorTimestamp'] - 1000 * md['ExposureTime']) / 1000000
exposure_real_time_ms = reference_time_ms + exposure_elapsed_time_ms
exposure_real_time_fmtd = time.strftime(
'%Y-%m-%d %H:%M:%S', time.gmtime(exposure_real_time_ms / 1000))
self.annotate_text = '{0}.{1:0>3}'.format(
exposure_real_time_fmtd, int((exposure_real_time_ms % 1) * 1000))
banner_height_px = height_px // 16
margin_px = banner_height_px // 2
overlay_img = Image.new(
'L', (width_px - margin_px, banner_height_px))
overlay_img_draw = ImageDraw.Draw(overlay_img)
overlay_img_draw.text(
(0, 0), self.annotate_text, fill=255, font_size=banner_height_px)
if fmt == 'rgb':
pc2_cap_arr[margin_px:banner_height_px + margin_px,
margin_px:width_px, 0] = np.array(overlay_img)
pc2_cap_arr[margin_px:banner_height_px + margin_px,
margin_px:width_px, 1] = np.array(overlay_img)
pc2_cap_arr[margin_px:banner_height_px + margin_px,
margin_px:width_px, 2] = np.array(overlay_img)
else:
pc2_cap_arr[margin_px:banner_height_px + margin_px,
margin_px:width_px] = np.array(overlay_img)
# artificially extend camera capture time...
extra_delay_secs = await_elapsed(
time.time(), start + constants.THROTTLE_CAMERA_SNAP_SECS) # blocks
if self.debug:
if extra_delay_secs > 0:
self.logger.debug(
'OpticalPi capture throttle - slept for {0} seconds'.format(extra_delay_secs))
else:
self.logger.debug(
'OpticalPi capture throttling not required')
end = time.time()
elapsed = round(end - start, 3) # seconds
if self.debug:
self.logger.debug(
'OpticalPi capture - captured in {0:.2f} seconds'.format(elapsed))
except Exception as e1:
err_line = sys.exc_info()[-1].tb_lineno
if self.debug and self.logger:
self.logger.error(
'Error in picamera2 capture: ' + str(e1) + ' on line ' + str(err_line))
gray_img_arr = np.full((height_px, width_px), 127)
dummy_img = Image.fromarray(gray_img_arr)
dummy_draw = ImageDraw.Draw(dummy_img)
dummy_draw.text((10, 10), 'Picamera II', font_size=32)
if not self.linux:
dummy_draw.text(
(20, 60), 'Picamera is only available on Linux!', font_size=20)
dummy_draw.text((20, 110), '{0} on line {1}'.format(
e1, err_line), font_size=20)
gray_img_arr = np.array(dummy_img)
(gray_height_px, gray_width_px) = gray_img_arr.shape
if self.debug:
print('picamera2 capture - gray shape:',
(gray_width_px, gray_height_px))
# create a yuv plain grey image
# Estimate the actual array size (accounting for rounding of the resolution)
array_width_px = (width_px + 31) // 32 * 32
array_height_px = (height_px + 15) // 16 * 16
y_array_size = array_width_px * array_height_px
yuv_array_size = y_array_size * 1.5
if self.debug and self.logger:
self.logger.debug(
'picamera2 capture - yuv_array_size: {0}'.format(yuv_array_size))
lum_img = np.zeros((array_height_px, array_width_px), np.uint8)
lum_img[0:gray_height_px, 0:gray_width_px] = gray_img_arr
l_img = lum_img.reshape(y_array_size)
if fmt == 'rgb':
if self.debug and self.logger:
self.logger.debug('3 channel output - colour')
pc2_cap_arr = l_img.reshape(
array_height_px, array_width_px)
else:
pc2_cap_arr = l_img.reshape(
array_height_px, array_width_px)
if self.debug and self.logger:
self.logger.debug('1 channel output - gray')
end = time.time()
elapsed = round(end - start, 3) # seconds
if self.debug and self.logger:
self.logger.debug(
'picamera2 capture - captured in {0} seconds'.format(elapsed))
return pc2_cap_arr
except Exception as e2:
err_line = sys.exc_info()[-1].tb_lineno
msg = 'Error in OpticalPi capture: ' + \
str(e2) + ' on line ' + str(err_line)
if self.logger:
self.logger.error(msg)
else:
print(msg)