forked from rwb27/usaf_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicamera_array.py
1065 lines (914 loc) · 43.9 KB
/
picamera_array.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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# vim: set et sw=4 sts=4 fileencoding=utf-8:
#
# Python camera library for the Rasperry-Pi camera module
# Copyright (c) 2013-2017 Dave Jones <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import (
unicode_literals,
print_function,
division,
absolute_import,
)
# Make Py2's str and range equivalent to Py3's
native_str = str
str = type('')
try:
range = xrange
except NameError:
pass
import io
import ctypes as ct
import warnings
import numpy as np
from numpy.lib.stride_tricks import as_strided
#from . import mmalobj as mo, mmal
#from .exc import (
# mmal_check,
# PiCameraValueError,
# PiCameraDeprecated,
# PiCameraPortDisabled,
# )
PiCameraValueError = ValueError
import mo_stub as mo
motion_dtype = np.dtype([
(native_str('x'), np.int8),
(native_str('y'), np.int8),
(native_str('sad'), np.uint16),
])
def raw_resolution(resolution, splitter=False):
"""
Round a (width, height) tuple up to the nearest multiple of 32 horizontally
and 16 vertically (as this is what the Pi's camera module does for
unencoded output).
"""
width, height = resolution
if splitter:
fwidth = (width + 15) & ~15
else:
fwidth = (width + 31) & ~31
fheight = (height + 15) & ~15
return fwidth, fheight
def bytes_to_yuv(data, resolution):
"""
Converts a bytes object containing YUV data to a `numpy`_ array.
"""
width, height = resolution
fwidth, fheight = raw_resolution(resolution)
y_len = fwidth * fheight
uv_len = (fwidth // 2) * (fheight // 2)
if len(data) != (y_len + 2 * uv_len):
raise PiCameraValueError(
'Incorrect buffer length for resolution %dx%d' % (width, height))
# Separate out the Y, U, and V values from the array
a = np.frombuffer(data, dtype=np.uint8)
Y = a[:y_len].reshape((fheight, fwidth))
Uq = a[y_len:-uv_len].reshape((fheight // 2, fwidth // 2))
Vq = a[-uv_len:].reshape((fheight // 2, fwidth // 2))
# Reshape the values into two dimensions, and double the size of the
# U and V values (which only have quarter resolution in YUV4:2:0)
U = np.empty_like(Y)
V = np.empty_like(Y)
U[0::2, 0::2] = Uq
U[0::2, 1::2] = Uq
U[1::2, 0::2] = Uq
U[1::2, 1::2] = Uq
V[0::2, 0::2] = Vq
V[0::2, 1::2] = Vq
V[1::2, 0::2] = Vq
V[1::2, 1::2] = Vq
# Stack the channels together and crop to the actual resolution
return np.dstack((Y, U, V))[:height, :width]
def bytes_to_rgb(data, resolution):
"""
Converts a bytes objects containing RGB/BGR data to a `numpy`_ array.
"""
width, height = resolution
fwidth, fheight = raw_resolution(resolution)
# Workaround: output from the video splitter is rounded to 16x16 instead
# of 32x16 (but only for RGB, and only when a resizer is not used)
if len(data) != (fwidth * fheight * 3):
fwidth, fheight = raw_resolution(resolution, splitter=True)
if len(data) != (fwidth * fheight * 3):
raise PiCameraValueError(
'Incorrect buffer length for resolution %dx%d' % (width, height))
# Crop to the actual resolution
return np.frombuffer(data, dtype=np.uint8).\
reshape((fheight, fwidth, 3))[:height, :width, :]
class PiArrayOutput(io.BytesIO):
"""
Base class for capture arrays.
This class extends :class:`io.BytesIO` with a `numpy`_ array which is
intended to be filled when :meth:`~io.IOBase.flush` is called (i.e. at the
end of capture).
.. attribute:: array
After :meth:`~io.IOBase.flush` is called, this attribute contains the
frame's data as a multi-dimensional `numpy`_ array. This is typically
organized with the dimensions ``(rows, columns, plane)``. Hence, an
RGB image with dimensions *x* and *y* would produce an array with shape
``(y, x, 3)``.
"""
def __init__(self, camera, size=None):
super(PiArrayOutput, self).__init__()
self.camera = camera
self.size = size
self.array = None
def close(self):
super(PiArrayOutput, self).close()
self.array = None
def truncate(self, size=None):
"""
Resize the stream to the given size in bytes (or the current position
if size is not specified). This resizing can extend or reduce the
current file size. The new file size is returned.
In prior versions of picamera, truncation also changed the position of
the stream (because prior versions of these stream classes were
non-seekable). This functionality is now deprecated; scripts should
use :meth:`~io.IOBase.seek` and :meth:`truncate` as one would with
regular :class:`~io.BytesIO` instances.
"""
if size is not None:
warnings.warn(
PiCameraDeprecated(
'This method changes the position of the stream to the '
'truncated length; this is deprecated functionality and '
'you should not rely on it (seek before or after truncate '
'to ensure position is consistent)'))
super(PiArrayOutput, self).truncate(size)
if size is not None:
self.seek(size)
class PiRGBArray(PiArrayOutput):
"""
Produces a 3-dimensional RGB array from an RGB capture.
This custom output class can be used to easily obtain a 3-dimensional numpy
array, organized (rows, columns, colors), from an unencoded RGB capture.
The array is accessed via the :attr:`~PiArrayOutput.array` attribute. For
example::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
with picamera.array.PiRGBArray(camera) as output:
camera.capture(output, 'rgb')
print('Captured %dx%d image' % (
output.array.shape[1], output.array.shape[0]))
You can re-use the output to produce multiple arrays by emptying it with
``truncate(0)`` between captures::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
with picamera.array.PiRGBArray(camera) as output:
camera.resolution = (1280, 720)
camera.capture(output, 'rgb')
print('Captured %dx%d image' % (
output.array.shape[1], output.array.shape[0]))
output.truncate(0)
camera.resolution = (640, 480)
camera.capture(output, 'rgb')
print('Captured %dx%d image' % (
output.array.shape[1], output.array.shape[0]))
If you are using the GPU resizer when capturing (with the *resize*
parameter of the various :meth:`~PiCamera.capture` methods), specify the
resized resolution as the optional *size* parameter when constructing the
array output::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
camera.resolution = (1280, 720)
with picamera.array.PiRGBArray(camera, size=(640, 360)) as output:
camera.capture(output, 'rgb', resize=(640, 360))
print('Captured %dx%d image' % (
output.array.shape[1], output.array.shape[0]))
"""
def flush(self):
super(PiRGBArray, self).flush()
self.array = bytes_to_rgb(self.getvalue(), self.size or self.camera.resolution)
class PiYUVArray(PiArrayOutput):
"""
Produces 3-dimensional YUV & RGB arrays from a YUV capture.
This custom output class can be used to easily obtain a 3-dimensional numpy
array, organized (rows, columns, channel), from an unencoded YUV capture.
The array is accessed via the :attr:`~PiArrayOutput.array` attribute. For
example::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
with picamera.array.PiYUVArray(camera) as output:
camera.capture(output, 'yuv')
print('Captured %dx%d image' % (
output.array.shape[1], output.array.shape[0]))
The :attr:`rgb_array` attribute can be queried for the equivalent RGB
array (conversion is performed using the `ITU-R BT.601`_ matrix)::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
with picamera.array.PiYUVArray(camera) as output:
camera.resolution = (1280, 720)
camera.capture(output, 'yuv')
print(output.array.shape)
print(output.rgb_array.shape)
If you are using the GPU resizer when capturing (with the *resize*
parameter of the various :meth:`~picamera.PiCamera.capture` methods),
specify the resized resolution as the optional *size* parameter when
constructing the array output::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
camera.resolution = (1280, 720)
with picamera.array.PiYUVArray(camera, size=(640, 360)) as output:
camera.capture(output, 'yuv', resize=(640, 360))
print('Captured %dx%d image' % (
output.array.shape[1], output.array.shape[0]))
.. _ITU-R BT.601: https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion
"""
def __init__(self, camera, size=None):
super(PiYUVArray, self).__init__(camera, size)
self._rgb = None
def flush(self):
super(PiYUVArray, self).flush()
self.array = bytes_to_yuv(self.getvalue(), self.size or self.camera.resolution)
self._rgb = None
@property
def rgb_array(self):
if self._rgb is None:
# Apply the standard biases
YUV = self.array.astype(float)
YUV[:, :, 0] = YUV[:, :, 0] - 16 # Offset Y by 16
YUV[:, :, 1:] = YUV[:, :, 1:] - 128 # Offset UV by 128
# YUV conversion matrix from ITU-R BT.601 version (SDTV)
# Y U V
M = np.array([[1.164, 0.000, 1.596], # R
[1.164, -0.392, -0.813], # G
[1.164, 2.017, 0.000]]) # B
# Calculate the dot product with the matrix to produce RGB output,
# clamp the results to byte range and convert to bytes
self._rgb = YUV.dot(M.T).clip(0, 255).astype(np.uint8)
return self._rgb
class BroadcomRawHeader(ct.Structure):
_fields_ = [
('name', ct.c_char * 32),
('width', ct.c_uint16),
('height', ct.c_uint16),
('padding_right', ct.c_uint16),
('padding_down', ct.c_uint16),
('dummy', ct.c_uint32 * 6),
('transform', ct.c_uint16),
('format', ct.c_uint16),
('bayer_order', ct.c_uint8),
('bayer_format', ct.c_uint8),
]
class PiBayerArray(PiArrayOutput):
"""
Produces a 3-dimensional RGB array from raw Bayer data.
This custom output class is intended to be used with the
:meth:`~picamera.PiCamera.capture` method, with the *bayer* parameter set
to ``True``, to include raw Bayer data in the JPEG output. The class
strips out the raw data, and constructs a numpy array from it. The
resulting data is accessed via the :attr:`~PiArrayOutput.array` attribute::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
with picamera.array.PiBayerArray(camera) as output:
camera.capture(output, 'jpeg', bayer=True)
print(output.array.shape)
The *output_dims* parameter specifies whether the resulting array is
three-dimensional (the default, or when *output_dims* is 3), or
two-dimensional (when *output_dims* is 2). The three-dimensional data is
already separated into the three color planes, whilst the two-dimensional
variant is not (in which case you need to know the Bayer ordering to
accurately deal with the results).
.. note::
Bayer data is *usually* full resolution, so the resulting array usually
has the shape (1944, 2592, 3) with the V1 module, or (2464, 3280, 3)
with the V2 module (if two-dimensional output is requested the
3-layered color dimension is omitted). If the camera's
:attr:`~picamera.PiCamera.sensor_mode` has been forced to something
other than 0, then the output will be the native size for the requested
sensor mode.
This also implies that the optional *size* parameter (for specifying a
resizer resolution) is not available with this array class.
As the sensor records 10-bit values, the array uses the unsigned 16-bit
integer data type.
By default, `de-mosaicing`_ is **not** performed; if the resulting array is
viewed it will therefore appear dark and too green (due to the green bias
in the `Bayer pattern`_). A trivial weighted-average demosaicing algorithm
is provided in the :meth:`demosaic` method::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
with picamera.array.PiBayerArray(camera) as output:
camera.capture(output, 'jpeg', bayer=True)
print(output.demosaic().shape)
Viewing the result of the de-mosaiced data will look more normal but still
considerably worse quality than the regular camera output (as none of the
other usual post-processing steps like auto-exposure, white-balance,
vignette compensation, and smoothing have been performed).
.. versionchanged:: 1.13
This class now supports the V2 module properly, and handles flipped
images, and forced sensor modes correctly.
.. _de-mosaicing: https://en.wikipedia.org/wiki/Demosaicing
.. _Bayer pattern: https://en.wikipedia.org/wiki/Bayer_filter
"""
BAYER_OFFSETS = {
0: ((0, 0), (1, 0), (0, 1), (1, 1)),
1: ((1, 0), (0, 0), (1, 1), (0, 1)),
2: ((1, 1), (0, 1), (1, 0), (0, 0)),
3: ((0, 1), (1, 1), (0, 0), (1, 0)),
}
def __init__(self, camera, output_dims=3):
super(PiBayerArray, self).__init__(camera, size=None)
if not (2 <= output_dims <= 3):
raise PiCameraValueError('output_dims must be 2 or 3')
self._demo = None
self._header = None
self._output_dims = output_dims
@property
def output_dims(self):
return self._output_dims
def _to_3d(self, array):
array_3d = np.zeros(array.shape + (3,), dtype=array.dtype)
(
(ry, rx), (gy, gx), (Gy, Gx), (by, bx)
) = PiBayerArray.BAYER_OFFSETS[self._header.bayer_order]
array_3d[ry::2, rx::2, 0] = array[ry::2, rx::2] # Red
array_3d[gy::2, gx::2, 1] = array[gy::2, gx::2] # Green
array_3d[Gy::2, Gx::2, 1] = array[Gy::2, Gx::2] # Green
array_3d[by::2, bx::2, 2] = array[by::2, bx::2] # Blue
return array_3d
def flush(self):
super(PiBayerArray, self).flush()
self._demo = None
offset = {
'OV5647': {
0: 6404096,
1: 2717696,
2: 6404096,
3: 6404096,
4: 1625600,
5: 1233920,
6: 445440,
7: 445440,
},
'IMX219': {
0: 10270208,
1: 2678784,
2: 10270208,
3: 10270208,
4: 2628608,
5: 1963008,
6: 1233920,
7: 445440,
},
}[self.camera.revision.upper()][self.camera.sensor_mode]
data = self.getvalue()[-offset:]
if data[:4] != b'BRCM':
raise PiCameraValueError('Unable to locate Bayer data at end of buffer')
# Extract header (with bayer order and other interesting bits), which
# is 176 bytes from start of bayer data, and pixel data which 32768
# bytes from start of bayer data
self._header = BroadcomRawHeader.from_buffer_copy(
data[176:176 + ct.sizeof(BroadcomRawHeader)])
data = np.frombuffer(data, dtype=np.uint8, offset=32768)
# Reshape and crop the data. The crop's width is multiplied by 5/4 to
# deal with the packed 10-bit format; the shape's width is calculated
# in a similar fashion but with padding included (which involves
# several additional padding steps)
crop = mo.PiResolution(
self._header.width * 5 // 4,
self._header.height)
shape = mo.PiResolution(
(((self._header.width + self._header.padding_right) * 5) + 3) // 4,
(self._header.height + self._header.padding_down)
).pad()
data = data.reshape((shape.height, shape.width))[:crop.height, :crop.width]
self.data_to_array(data)
def data_to_array(self, data):
"""Convert the cropped, reshaped array of 8 bit numbers into a sensible array"""
# Unpack 10-bit values; every 5 bytes contains the high 8-bits of 4
# values followed by the low 2-bits of 4 values packed into the fifth
# byte
# rwb27: separated this out to allow for different conversion by PiFastBayerArray
data = data.astype(np.uint16) << 2
for byte in range(4):
data[:, byte::5] |= ((data[:, 4::5] >> ((4 - byte) * 2)) & 3)
self.array = np.zeros(
(data.shape[0], data.shape[1] * 4 // 5), dtype=np.uint16)
for i in range(4):
self.array[:, i::4] = data[:, i::5]
if self.output_dims == 3:
self.array = self._to_3d(self.array)
def demosaic(self):
"""
Perform a rudimentary `de-mosaic`_ of ``self.array``, returning the
result as a new array. The result of the demosaic is *always* three
dimensional, with the last dimension being the color planes (see
*output_dims* parameter on the constructor).
.. _de-mosaic: https://en.wikipedia.org/wiki/Demosaicing
"""
if self._demo is None:
# Construct 3D representation of Bayer data (if necessary)
if self.output_dims == 2:
array_3d = self._to_3d(self.array)
else:
array_3d = self.array
# Construct representation of the bayer pattern
bayer = np.zeros(array_3d.shape, dtype=np.uint8)
(
(ry, rx), (gy, gx), (Gy, Gx), (by, bx)
) = PiBayerArray.BAYER_OFFSETS[self._header.bayer_order]
bayer[ry::2, rx::2, 0] = 1 # Red
bayer[gy::2, gx::2, 1] = 1 # Green
bayer[Gy::2, Gx::2, 1] = 1 # Green
bayer[by::2, bx::2, 2] = 1 # Blue
# Allocate output array with same shape as data and set up some
# constants to represent the weighted average window
window = (3, 3)
borders = (window[0] - 1, window[1] - 1)
border = (borders[0] // 2, borders[1] // 2)
# Pad out the data and the bayer pattern (np.pad is faster but
# unavailable on the version of numpy shipped with Raspbian at the
# time of writing)
rgb = np.zeros((
array_3d.shape[0] + borders[0],
array_3d.shape[1] + borders[1],
array_3d.shape[2]), dtype=array_3d.dtype)
rgb[
border[0]:rgb.shape[0] - border[0],
border[1]:rgb.shape[1] - border[1],
:] = array_3d
bayer_pad = np.zeros((
array_3d.shape[0] + borders[0],
array_3d.shape[1] + borders[1],
array_3d.shape[2]), dtype=bayer.dtype)
bayer_pad[
border[0]:bayer_pad.shape[0] - border[0],
border[1]:bayer_pad.shape[1] - border[1],
:] = bayer
bayer = bayer_pad
# For each plane in the RGB data, construct a view over the plane
# of 3x3 matrices. Then do the same for the bayer array and use
# Einstein summation to get the weighted average
self._demo = np.empty(array_3d.shape, dtype=array_3d.dtype)
for plane in range(3):
p = rgb[..., plane]
b = bayer[..., plane]
pview = as_strided(p, shape=(
p.shape[0] - borders[0],
p.shape[1] - borders[1]) + window, strides=p.strides * 2)
bview = as_strided(b, shape=(
b.shape[0] - borders[0],
b.shape[1] - borders[1]) + window, strides=b.strides * 2)
psum = np.einsum('ijkl->ij', pview)
bsum = np.einsum('ijkl->ij', bview)
self._demo[..., plane] = psum // bsum
return self._demo
class PiSharpBayerArray(PiBayerArray):
"""A PiBayerArray, demosaiced so as to preserve sharpness a bit more (esp. for green)"""
def demosaic(self):
if self._demo is None:
# Construct 3D representation of Bayer data (if necessary)
if self.output_dims == 2:
array_3d = self._to_3d(self.array)
else:
array_3d = self.array.copy()
# Construct representation of the bayer pattern
(
(ry, rx), (gy, gx), (Gy, Gx), (by, bx)
) = PiBayerArray.BAYER_OFFSETS[self._header.bayer_order]
output = np.empty_like(array_3d)
assert gy != Gy and gx != Gx, "Green pixels must be diagonal"
from scipy.ndimage.filters import convolve
for i in range(3):
a, b = (1, 0) if i==1 else (2, 1)
weights = np.array([[b, a , b],
[a, 4, a],
[b, a , b]], dtype=np.uint16)
# weights = np.array([[2,4,2]], dtype=np.uint16)
convolve(array_3d[:,:,i], weights, output=output[:,:,i], mode='constant', cval=0.0)
self._demo = output//4
return self._demo
class PiFastBayerArray(PiBayerArray):
_demo_shift = None # cache the value of "shift" used in demosaicing
"""
Produces a 3-dimensional RGB array from raw Bayer data, at half resolution.
This output class should be used with the :meth:`~picamera.PiCamera.capture`
method, with the *bayer* parameter set to ``True`` (this includes the raw
Bayer data in the JPEG metadata). This class extracts the Bayer data, and
stores it in the :attr:`~PiArrayOutput.array` attribute. The raw Bayer array
is made of packed 10-bit values, where every fifth byte contains the least
significant bits. It can be accessed as shown::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
with picamera.array.PiFastBayerArray(camera) as output:
camera.capture(output, 'jpeg', bayer=True)
print(output.array.shape)
As with :class:`~PiBayerArray`, note that this output is always at full
resolution, regardless of the camera's resolution setting.
In many situations, it is desirable to convert the raw array into an 8-bit
RGB array. This can be done with the :meth:`demosaic` method. As in the
superclass, this method converts the raw array to an RGB array. However,
this class uses a much faster (and much cruder) demosaicing algorithm, so
each pixel in the array is based on a group of four pixels (red, blue, and
two green) on the sensor. This produces a half-resolution RGB array, with
much less processing time required.
See :class:`~PiBayerArray` for full-resolution conversion to RGB. There
are a few important differences between this class and that:
* The resolution will always be half that used by ``PiBayerArray``
* The :attr:`~PiFastBayerArray.array` attribute contains raw Bayer data, not
unpacked RGB data.
* The output of :meth:`~demosaic` will have half the resolution compared to
:meth:`~PiBayerArray` but will still be an RGB array of unsigned 8-bit
integers.
"""
def data_to_array(self, data):
self.array = data # This is not quite the raw Bayer data - every 5th element
# is four lots of two least-significant-bits. In this PiBayerArray subclass,
# we skip converting it to a full resolution 16-bit array. Instead, we leave
# it as a 5-bytes-for-4-pixels format array, and
def demosaic(self, shift=0):
"""Convert the raw Bayer data into a half-resolution RGB array.
This uses a really blunt demosaicing algorithm: group pixels in squares,
and then use the red, blue, and two green pixels from each square to
calculate an RGB value. This is calculated as three unsigned 8-bit
integers.
As the sensor is 10 bit but output is 8-bit, we provide the ``shift``
parameter. Setting this to 2 will return the lower 8 bits, while setting
it to 0 (the default) will return the upper 8 bits. In the future,
there may be an option to work in 16-bit integers and return all of them
(though that would be slower). Currently, if ``shift`` is nonzero and
some pixels have higher values than will fit in the 8-bit output, overflow
will occur and those pixels may no longer be bright - so use the ``shift``
argument with caution.
NB that the highest useful ``shift`` value is 3; while the sensor is only
10-bit, there are two green pixels on the sensor for each output pixel.
Thus, we gain an extra bit of precision from averaging, allowing us to
effectively produce an 11-bit image.
"""
if self._demo is None or self._demo_shift != shift:
# As with `PiBayerArray`, should take into account vflip and hflip here
# Extract the R, G1, G2, B pixels into separate slices
# NB these should _not_ need to be copied at this stage.
# NB we end up with odd and even arrays for each because every
# 5th element is the least-significant-bits.
self._demo_shift = shift # remember this value, so we will recalculate
# if called again with a different shift value.
def bayer_slices(i, j, shift=shift):
if shift == 0: # Return the top 8 bits
# This should be really fast - they ought to be slices
return self.array[i::2, j::5], self.array[i::2,j+2::5]
else:
# Left-shift the arrays so we can fill the LSB later
a, b = bayer_slices(i, j, shift=0)
a, b = a << shift, b << shift # NB this copies a, b
# Now retrieve and add in the two least significant bits
# These are stored, packed, in every 5th byte:
lsb = self.array[i::2, 4::5]
# The LSB will be in bits (3-j)*2 and (3-j)*2 + 1
if shift == 2:
a += (lsb >> (3 - j)*2) & 3
b += (lsb >> (1 - j)*2) & 3
elif shift == 1:
a += (lsb >> ((3 - j)*2 + 1)) & 1
b += (lsb >> ((1 - j)*2 + 1)) & 1
elif shift == 3:
# A shift of 3 leaves the LSB at zero. It's only
# included because the two green pixels means that
# we do generate an LSB for green even with a shfit
# of 3. This might be handy for fluorescence images.
a += ((lsb >> (3 - j)*2) & 3) << 1
b += ((lsb >> (1 - j)*2) & 3) << 1
return a, b
Ra, Rb = bayer_slices(1,0)
G1a, G1b = bayer_slices(0,0)
G2a, G2b = bayer_slices(1,1)
Ba, Bb = bayer_slices(0,1)
# Make an array of the right size
shape = (Ra.shape[0], Ra.shape[1] * 2, 3)
rgb = np.empty(shape, dtype=Ra.dtype)
# Now put the relevant values in
rgb[:,0::2,0] = Ra # Red pixels (even)
rgb[:,1::2,0] = Rb # Red pixels (odd)
rgb[:,0::2,2] = Ba
rgb[:,1::2,2] = Bb
rgb[:,0::2,1] = G1a//2 # There are twice as many greens, so we
rgb[:,1::2,1] = G1b//2 # take an average
rgb[:,0::2,1] += G2a//2
rgb[:,1::2,1] += G2b//2
self._demo = rgb
return self._demo
class PiMotionArray(PiArrayOutput):
"""
Produces a 3-dimensional array of motion vectors from the H.264 encoder.
This custom output class is intended to be used with the *motion_output*
parameter of the :meth:`~picamera.PiCamera.start_recording` method. Once
recording has finished, the class generates a 3-dimensional numpy array
organized as (frames, rows, columns) where ``rows`` and ``columns`` are the
number of rows and columns of `macro-blocks`_ (16x16 pixel blocks) in the
original frames. There is always one extra column of macro-blocks present
in motion vector data.
The data-type of the :attr:`~PiArrayOutput.array` is an (x, y, sad)
structure where ``x`` and ``y`` are signed 1-byte values, and ``sad`` is an
unsigned 2-byte value representing the `sum of absolute differences`_ of
the block. For example::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
with picamera.array.PiMotionArray(camera) as output:
camera.resolution = (640, 480)
camera.start_recording(
'/dev/null', format='h264', motion_output=output)
camera.wait_recording(30)
camera.stop_recording()
print('Captured %d frames' % output.array.shape[0])
print('Frames are %dx%d blocks big' % (
output.array.shape[2], output.array.shape[1]))
If you are using the GPU resizer with your recording, use the optional
*size* parameter to specify the resizer's output resolution when
constructing the array::
import picamera
import picamera.array
with picamera.PiCamera() as camera:
camera.resolution = (640, 480)
with picamera.array.PiMotionArray(camera, size=(320, 240)) as output:
camera.start_recording(
'/dev/null', format='h264', motion_output=output,
resize=(320, 240))
camera.wait_recording(30)
camera.stop_recording()
print('Captured %d frames' % output.array.shape[0])
print('Frames are %dx%d blocks big' % (
output.array.shape[2], output.array.shape[1]))
.. note::
This class is not suitable for real-time analysis of motion vector
data. See the :class:`PiMotionAnalysis` class instead.
.. _macro-blocks: https://en.wikipedia.org/wiki/Macroblock
.. _sum of absolute differences: https://en.wikipedia.org/wiki/Sum_of_absolute_differences
"""
def flush(self):
super(PiMotionArray, self).flush()
width, height = self.size or self.camera.resolution
cols = ((width + 15) // 16) + 1
rows = (height + 15) // 16
b = self.getvalue()
frames = len(b) // (cols * rows * motion_dtype.itemsize)
self.array = np.frombuffer(b, dtype=motion_dtype).reshape((frames, rows, cols))
class PiAnalysisOutput(io.IOBase):
"""
Base class for analysis outputs.
This class extends :class:`io.IOBase` with a stub :meth:`analyze` method
which will be called for each frame output. In this base implementation the
method simply raises :exc:`NotImplementedError`.
"""
def __init__(self, camera, size=None):
super(PiAnalysisOutput, self).__init__()
self.camera = camera
self.size = size
def writable(self):
return True
def write(self, b):
return len(b)
def analyze(self, array):
"""
Stub method for users to override.
"""
try:
self.analyse(array)
warnings.warn(
PiCameraDeprecated(
'The analyse method is deprecated; use analyze (US '
'English spelling) instead'))
except NotImplementedError:
raise
def analyse(self, array):
"""
Deprecated alias of :meth:`analyze`.
"""
raise NotImplementedError
class PiRGBAnalysis(PiAnalysisOutput):
"""
Provides a basis for per-frame RGB analysis classes.
This custom output class is intended to be used with the
:meth:`~picamera.PiCamera.start_recording` method when it is called with
*format* set to ``'rgb'`` or ``'bgr'``. While recording is in progress, the
:meth:`~PiAnalysisOutput.write` method converts incoming frame data into a
numpy array and calls the stub :meth:`~PiAnalysisOutput.analyze` method
with the resulting array (this deliberately raises
:exc:`NotImplementedError` in this class; you must override it in your
descendent class).
.. note::
If your overridden :meth:`~PiAnalysisOutput.analyze` method runs slower
than the required framerate (e.g. 33.333ms when framerate is 30fps)
then the camera's effective framerate will be reduced. Furthermore,
this doesn't take into account the overhead of picamera itself so in
practice your method needs to be a bit faster still.
The array passed to :meth:`~PiAnalysisOutput.analyze` is organized as
(rows, columns, channel) where the channels 0, 1, and 2 are R, G, and B
respectively (or B, G, R if *format* is ``'bgr'``).
"""
def write(self, b):
result = super(PiRGBAnalysis, self).write(b)
self.analyze(bytes_to_rgb(b, self.size or self.camera.resolution))
return result
class PiYUVAnalysis(PiAnalysisOutput):
"""
Provides a basis for per-frame YUV analysis classes.
This custom output class is intended to be used with the
:meth:`~picamera.PiCamera.start_recording` method when it is called with
*format* set to ``'yuv'``. While recording is in progress, the
:meth:`~PiAnalysisOutput.write` method converts incoming frame data into a
numpy array and calls the stub :meth:`~PiAnalysisOutput.analyze` method
with the resulting array (this deliberately raises
:exc:`NotImplementedError` in this class; you must override it in your
descendent class).
.. note::
If your overridden :meth:`~PiAnalysisOutput.analyze` method runs slower
than the required framerate (e.g. 33.333ms when framerate is 30fps)
then the camera's effective framerate will be reduced. Furthermore,
this doesn't take into account the overhead of picamera itself so in
practice your method needs to be a bit faster still.
The array passed to :meth:`~PiAnalysisOutput.analyze` is organized as
(rows, columns, channel) where the channel 0 is Y (luminance), while 1 and
2 are U and V (chrominance) respectively. The chrominance values normally
have quarter resolution of the luminance values but this class makes all
channels equal resolution for ease of use.
"""
def write(self, b):
result = super(PiYUVAnalysis, self).write(b)
self.analyze(bytes_to_yuv(b, self.size or self.camera.resolution))
return result
class PiMotionAnalysis(PiAnalysisOutput):
"""
Provides a basis for real-time motion analysis classes.
This custom output class is intended to be used with the *motion_output*
parameter of the :meth:`~picamera.PiCamera.start_recording` method. While
recording is in progress, the write method converts incoming motion data
into numpy arrays and calls the stub :meth:`~PiAnalysisOutput.analyze`
method with the resulting array (which deliberately raises
:exc:`NotImplementedError` in this class).
.. note::
If your overridden :meth:`~PiAnalysisOutput.analyze` method runs slower
than the required framerate (e.g. 33.333ms when framerate is 30fps)
then the camera's effective framerate will be reduced. Furthermore,
this doesn't take into account the overhead of picamera itself so in
practice your method needs to be a bit faster still.
The array passed to :meth:`~PiAnalysisOutput.analyze` is organized as
(rows, columns) where ``rows`` and ``columns`` are the number of rows and
columns of `macro-blocks`_ (16x16 pixel blocks) in the original frames.
There is always one extra column of macro-blocks present in motion vector
data.
The data-type of the array is an (x, y, sad) structure where ``x`` and
``y`` are signed 1-byte values, and ``sad`` is an unsigned 2-byte value
representing the `sum of absolute differences`_ of the block.
An example of a crude motion detector is given below::
import numpy as np
import picamera
import picamera.array
class DetectMotion(picamera.array.PiMotionAnalysis):
def analyze(self, a):
a = np.sqrt(
np.square(a['x'].astype(np.float)) +
np.square(a['y'].astype(np.float))
).clip(0, 255).astype(np.uint8)
# If there're more than 10 vectors with a magnitude greater
# than 60, then say we've detected motion
if (a > 60).sum() > 10:
print('Motion detected!')
with picamera.PiCamera() as camera:
with DetectMotion(camera) as output:
camera.resolution = (640, 480)
camera.start_recording(
'/dev/null', format='h264', motion_output=output)
camera.wait_recording(30)
camera.stop_recording()
You can use the optional *size* parameter to specify the output resolution
of the GPU resizer, if you are using the *resize* parameter of
:meth:`~picamera.PiCamera.start_recording`.
"""
def __init__(self, camera, size=None):
super(PiMotionAnalysis, self).__init__(camera, size)
self.cols = None
self.rows = None
def write(self, b):
result = super(PiMotionAnalysis, self).write(b)
if self.cols is None:
width, height = self.size or self.camera.resolution
self.cols = ((width + 15) // 16) + 1
self.rows = (height + 15) // 16
self.analyze(
np.frombuffer(b, dtype=motion_dtype).\
reshape((self.rows, self.cols)))
return result
# class MMALArrayBuffer(mo.MMALBuffer):
# __slots__ = ('_shape',)
# def __init__(self, port, buf):
# super(MMALArrayBuffer, self).__init__(buf)
# width = port._format[0].es[0].video.width
# height = port._format[0].es[0].video.height
# bpp = self.size // (width * height)
# self.offset = 0
# self.length = width * height * bpp
# self._shape = (height, width, bpp)
# def __enter__(self):
# mmal_check(
# mmal.mmal_buffer_header_mem_lock(self._buf),
# prefix='unable to lock buffer header memory')
# assert self.offset == 0
# return np.frombuffer(
# ct.cast(
# self._buf[0].data,
# ct.POINTER(ct.c_uint8 * self._buf[0].alloc_size)).contents,
# dtype=np.uint8, count=self.length).reshape(self._shape)
# def __exit__(self, *exc):
# mmal.mmal_buffer_header_mem_unlock(self._buf)
# return False
# class PiArrayTransform(mo.MMALPythonComponent):
# """
# A derivative of :class:`~picamera.mmalobj.MMALPythonComponent` which eases
# the construction of custom MMAL transforms by representing buffer data as
# numpy arrays. The *formats* parameter specifies the accepted input
# formats as a sequence of strings (default: 'rgb', 'bgr', 'rgba', 'bgra').
# Override the :meth:`transform` method to modify buffers sent to the
# component, then place it in your MMAL pipeline as you would a normal
# encoder.
# """