Last active
May 28, 2020 07:01
-
-
Save rwb27/c2ea3afb204a634f9f21a99bd8dd1541 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import PIL.Image | |
import PIL.ExifTags | |
import sys | |
import os | |
def formatted_exif_data(image): | |
"""Retrieve an image's EXIF data and return as a dictionary with string keys""" | |
# with thanks to https://stackoverflow.com/questions/4764932/in-python-how-do-i-read-the-exif-data-for-an-image | |
exif_data = {} | |
for k, v in image._getexif().items(): | |
# Use names rather than codes where they are available | |
try: | |
exif_data[PIL.ExifTags.TAGS[k]] = v | |
except KeyError: | |
exif_data[k] = v | |
return exif_data | |
def parse_maker_note(maker_note): | |
"""Split the "maker note" EXIF field from a Raspberry Pi camera image into useful parameters""" | |
camera_parameters = {} | |
last_key = None | |
for p in maker_note.decode().split(" "): | |
# The maker note contains <thing>=<thing> entries, space delimited but with spaces in some values. | |
# So, if there is an = then we assume we've got key=value, but if there is no = sign, we append | |
# the current chunk to the latest value, because that's where it probably belongs... | |
if "=" in p: | |
last_key, v = p.split("=") | |
camera_parameters[last_key] = v | |
else: | |
camera_parameters[last_key] += " " + p | |
return camera_parameters | |
def print_kv(k, v, format=""): | |
"""Consistently print a key-value pair""" | |
print(kv_to_string(k, v, format=format)) | |
def kv_to_string(k, v, format=""): | |
"""Consistently output a key-value pair as text""" | |
return ("{0: >28}: {1" + format + "}").format(k, v) | |
def exif_data_as_string(image): | |
"""Extract the EXIF data from a PIL image object, and format as a string.""" | |
exif_data = formatted_exif_data(image) | |
output = "" | |
for k, v in exif_data.items(): | |
output += kv_to_string(k, v) + "\n" | |
camera_parameters = parse_maker_note(exif_data['MakerNote']) | |
output += "MakerNote Expanded:\n" | |
for k, v in camera_parameters.items(): | |
output += kv_to_string(k, v) + "\n" | |
output += "Derived Values:\n" | |
# calculate exposure time - see https://www.media.mit.edu/pia/Research/deepview/exif.html | |
ssv = exif_data['ShutterSpeedValue'] | |
exposure_time = 1/2.0**(float(ssv[0])/ssv[1]) | |
output += kv_to_string("exposure_time", exposure_time, ":.4") | |
return output | |
if __name__ == "__main__": | |
try: | |
assert len(sys.argv) == 2 | |
assert os.path.isfile(sys.argv[1]) | |
except: | |
print("Usage: {} <filename.jpg>".format(sys.argv[0])) | |
print("This tool will print out a summary of the image's metadata. It's designed to work with images taken on a Raspberry Pi camera.") | |
exit(-1) | |
image = PIL.Image.open(sys.argv[1]) | |
print(exif_data_as_string(image)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
#import microscope | |
#import picamera | |
import numpy as np | |
#import matplotlib.pyplot as plt | |
import time | |
import picamera_array | |
import cv2 | |
import PIL.Image | |
import PIL.ExifTags | |
from dump_exif import exif_data_as_string | |
import sys | |
full_resolution=(3280,2464) | |
class DummyCam(object): | |
resolution = full_resolution | |
revision = 'IMX219' | |
sensor_mode = 0 | |
def load_raw_image(filename, ArrayType=picamera_array.PiBayerArray, open_jpeg=False): | |
with open(filename, mode="rb") as file: | |
jpeg = file.read() | |
cam = DummyCam() | |
bayer_array = ArrayType(cam) | |
bayer_array.write(jpeg) | |
bayer_array.flush() | |
if open_jpeg: | |
jpeg = PIL.Image.open(filename) | |
# with thanks to https://stackoverflow.com/questions/4764932/in-python-how-do-i-read-the-exif-data-for-an-image | |
exif_data = jpeg._getexif() | |
return bayer_array, jpeg, exif_data | |
return bayer_array | |
def extract_file(filename): | |
"""Extract metadata and raw image from a file""" | |
print("converting {}...".format(filename)) | |
bayer_array, jpeg, exif_data = load_raw_image(filename, open_jpeg=True) | |
# extract EXIF metadata from the image | |
root_fname, junk = filename.rsplit(".j", 2) #get rid of the .jpeg extension | |
with open(root_fname + "_exif.txt", "w") as f: | |
f.write(exif_data_as_string(jpeg)) | |
# extract raw bayer data | |
cv2.imwrite(root_fname + "_raw16.tif", bayer_array.demosaic()*64) | |
cv2.imwrite(root_fname + "_raw8.png", (bayer_array.demosaic()//4).astype(np.uint8)) | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print("Usage: {} <filename.jpg> ...".format(sys.argv[0])) | |
print("Specify one or more filenames corresponding to Raspberry Pi JPEGs including raw Bayer data.") | |
print("Each file will be processed, to produce three new files:") | |
print("<filename>_raw16.tif will contain the full raw data as a 16-bit TIFF file (the lower 6 bits are empty).") | |
print("<filename>_raw8.png will contain the top 8 bits of the raw data, in an easier-to-handle file.") | |
print("<filename>_exif.txt will contain the EXIF metadata extracted as a text file - this includes analogue gain.") | |
sys.exit(0) | |
for filename in sys.argv[1:]: | |
extract_file(filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
class PiResolution(namedtuple('PiResolution', ('width', 'height'))): | |
""" | |
A :func:`~collections.namedtuple` derivative which represents a resolution | |
with a :attr:`width` and :attr:`height`. | |
.. attribute:: width | |
The width of the resolution in pixels | |
.. attribute:: height | |
The height of the resolution in pixels | |
.. versionadded:: 1.11 | |
""" | |
__slots__ = () # workaround python issue #24931 | |
def pad(self, width=32, height=16): | |
""" | |
Returns the resolution padded up to the nearest multiple of *width* | |
and *height* which default to 32 and 16 respectively (the camera's | |
native block size for most operations). For example: | |
.. code-block:: pycon | |
>>> PiResolution(1920, 1080).pad() | |
PiResolution(width=1920, height=1088) | |
>>> PiResolution(100, 100).pad(16, 16) | |
PiResolution(width=128, height=112) | |
>>> PiResolution(100, 100).pad(16, 16) | |
PiResolution(width=112, height=112) | |
""" | |
return PiResolution( | |
width=((self.width + (width - 1)) // width) * width, | |
height=((self.height + (height - 1)) // height) * height, | |
) | |
def transpose(self): | |
""" | |
Returns the resolution with the width and height transposed. For | |
example: | |
.. code-block:: pycon | |
>>> PiResolution(1920, 1080).transpose() | |
PiResolution(width=1080, height=1920) | |
""" | |
return PiResolution(self.height, self.width) | |
def __str__(self): | |
return '%dx%d' % (self.width, self.height) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. | |
# """ | |
# __slots__ = () | |
# def __init__(self, formats=('rgb', 'bgr', 'rgba', 'bgra')): | |
# super(PiArrayTransform, self).__init__() | |
# if isinstance(formats, bytes): | |
# formats = formats.decode('ascii') | |
# if isinstance(formats, str): | |
# formats = (formats,) | |
# try: | |
# formats = { | |
# { | |
# 'rgb': mmal.MMAL_ENCODING_RGB24, | |
# 'bgr': mmal.MMAL_ENCODING_BGR24, | |
# 'rgba': mmal.MMAL_ENCODING_RGBA, | |
# 'bgra': mmal.MMAL_ENCODING_BGRA, | |
# }[fmt] | |
# for fmt in formats | |
# } | |
# except KeyError as e: | |
# raise PiCameraValueError( | |
# 'PiArrayTransform cannot handle format %s' % str(e)) | |
# self.inputs[0].supported_formats = formats | |
# self.outputs[0].supported_formats = formats | |
# def _callback(self, port, source_buf): | |
# try: | |
# target_buf = self.outputs[0].get_buffer(False) | |
# except PiCameraPortDisabled: | |
# return False | |
# if target_buf: | |
# target_buf.copy_meta(source_buf) | |
# result = self.transform( | |
# MMALArrayBuffer(port, source_buf._buf), | |
# MMALArrayBuffer(self.outputs[0], target_buf._buf)) | |
# try: | |
# self.outputs[0].send_buffer(target_buf) | |
# except PiCameraPortDisabled: | |
# return False | |
# return False | |
# def transform(self, source, target): | |
# """ | |
# This method will be called for every frame passing through the | |
# transform. The *source* and *target* parameters represent buffers from | |
# the input and output ports of the transform respectively. They will be | |
# derivatives of :class:`~picamera.mmalobj.MMALBuffer` which return a | |
# 3-dimensional numpy array when used as context managers. For example:: | |
# def transform(self, source, target): | |
# with source as source_array, target as target_array: | |
# # Copy the source array data to the target | |
# target_array[...] = source_array | |
# # Draw a box around the edges | |
# target_array[0, :, :] = 0xff | |
# target_array[-1, :, :] = 0xff | |
# target_array[:, 0, :] = 0xff | |
# target_array[:, -1, :] = 0xff | |
# return False | |
# The target buffer's meta-data starts out as a copy of the source | |
# buffer's meta-data, but the target buffer's data starts out | |
# uninitialized. | |
# """ | |
# return False | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python code to set the lens shading in picamera | |
# (c) Richard Bowman 2018 | |
# released under GPL (for what it's worth) | |
# This is mostly a port of https://github.com/6by9/lens_shading | |
from __future__ import print_function | |
import picamera | |
from picamera import mmal, mmalobj as mo, user_vcsm as vcsm | |
from picamera.exc import mmal_check | |
import numpy as np | |
import ctypes as ct | |
import time | |
class PiCameraWithCustomLensShading(picamera.PiCamera): | |
def __init__(self, lens_shading_table=None, **kwargs): | |
self._lens_shading_table_array = lens_shading_table | |
picamera.PiCamera.__init__(self, **kwargs) | |
def _configure_camera(self, *args, **kwargs): | |
self._set_lens_shading_table_extra(self._lens_shading_table_array) | |
picamera.PiCamera._configure_camera(self, *args, **kwargs) | |
def _set_lens_shading_table_extra(self, lens_shading_table): | |
#TODO: make sure the resolution is appropriate to the current mode! | |
if lens_shading_table is None: | |
return # None means don't set anything | |
full_resolution = [3280, 2464] if self.revision.upper() == "IMX219" else (2592, 1944) | |
table_shape = (4,) + tuple([(r // 64) + 1 for r in full_resolution[::-1]]) | |
assert lens_shading_table.shape == table_shape, "Error: was expecting a lens shading table with shape {}".format(table_shape) | |
assert lens_shading_table.dtype == np.uint8, "Error: lens shading table must be uint8" | |
#n_grids, grid_height, grid_width = lens_shading_table.shape | |
#x = np.arange(grid_width) - grid_width//2 | |
#y = np.arange(grid_height) - grid_height//2 | |
#for i in range(4): | |
# lens_shading_table[i,:,:] = 32 + (np.sqrt(x[np.newaxis,:]**2 + y[:,np.newaxis]**2) > 5*i)*200 | |
# This sets the lens shading table based on the example code by 6by9 | |
# https://github.com/6by9/lens_shading/ | |
vc_memory_handle = vcsm.vcsm_malloc(grid_width*grid_height*4, "ls_grid") # allocate shared memory on the GPU | |
lens_shading_parameters = mmal.MMAL_PARAMETER_LENS_SHADING_T( | |
hdr = mmal.MMAL_PARAMETER_HEADER_T( | |
mmal.MMAL_PARAMETER_LENS_SHADING_OVERRIDE, | |
ct.sizeof(mmal.MMAL_PARAMETER_LENS_SHADING_T), | |
), | |
enabled = mmal.MMAL_TRUE, | |
grid_cell_size = 64, | |
grid_width = grid_width, | |
grid_stride = grid_width, | |
grid_height = grid_height, | |
mem_handle_table = vcsm.vcsm_vc_hdl_from_hdl(vc_memory_handle), | |
ref_transform = 3,# TODO: figure out what this should be properly!!! | |
) | |
print("Lens shading parameters:") | |
for f in lens_shading_parameters._fields_: | |
print(" {}: {}".format(f[0], getattr(lens_shading_parameters, f[0]))) | |
contiguous_lens_shading_table = np.ascontiguousarray(lens_shading_table) # make sure the array is contiguous in memory | |
try: | |
vc_lens_shading_table = vcsm.vcsm_lock(vc_memory_handle) # lock the memory to write to it | |
ct.memmove(vc_lens_shading_table, contiguous_lens_shading_table.ctypes.data_as(ct.c_void_p), grid_width*grid_height*4) | |
finally: | |
vcsm.vcsm_unlock_hdl(vc_memory_handle) # be careful to unlock the memory as soon as we're done! | |
#mmal_check(mmal.mmal_port_parameter_set(camera._camera.control._port, lens_shading_parameters.hdr)) | |
self._camera.control.params[mmal.MMAL_PARAMETER_LENS_SHADING_OVERRIDE] = lens_shading_parameters | |
vcsm.vcsm_free(vc_memory_handle) | |
if __name__ == "__main__": | |
npz = np.load("lens_shading_table.npz") | |
lens_shading_table_from_file = npz['lens_shading_table'] | |
#full_resolution = [3280, 2464] # Sorry, picamera v1 users! | |
#table_shape = (4,) + tuple([(r // 64) + 1 for r in full_resolution[::-1]]) | |
lens_shading_table = np.zeros_like(lens_shading_table_from_file) | |
lens_shading_table[...] = 32 #flat shading table | |
lens_shading_table_rings = np.zeros_like(lens_shading_table) | |
n_grids, grid_height, grid_width = lens_shading_table.shape | |
x = np.arange(grid_width) - grid_width//2 | |
y = np.arange(grid_height) - grid_height//2 | |
for i in range(4): | |
lens_shading_table_rings[i,:,:] = 32 + (np.sqrt(x[np.newaxis,:]**2 + y[:,np.newaxis]**2) > 5*i)*200 | |
try: | |
vcsm.vcsm_init() | |
with picamera.PiCamera() as cam: | |
cam.start_preview() | |
time.sleep(2) | |
awb_gains = cam.awb_gains | |
shutter_speed = cam.exposure_speed | |
analog_gain = cam.analog_gain | |
digital_gain = cam.digital_gain | |
for lst in [lens_shading_table_from_file, lens_shading_table, lens_shading_table_rings]: | |
#with PiCameraWithCustomLensShading(lens_shading_table=lst) as cam: | |
with picamera.PiCamera(lens_shading_table=lst, resolution=(3280,2464)) as cam: | |
cam.awb_gains = awb_gains | |
cam.shutter_speed = shutter_speed | |
cam.analog_gain = analog_gain | |
cam.digital_gain = digital_gain | |
cam.start_preview()#fullscreen=False, window=(0,50,640,480)) | |
time.sleep(2) | |
finally: | |
vcsm.vcsm_exit() | |
pass |
View raw
(Sorry about that, but we can’t show files that are this big right now.)
@rwb27 - I've been trying to find a way to extract the raw bayer data out of a JPEG+RAW image, directly into a numpy array.
I noticed that in this gist you seem to do this by forking enough of the picamera[array] library to be able to initialize a PiBayerArray (with a dummy camera object), and use that to extract the raw data into a numpy array.
I assume you forked that library because it won't install on non-linux systems. A few questions:
- Have you found any more graceful way of doing this?
- Would you mind if I copied your code to use in my (commercial) application?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case it helps, my notes while writing this:
There's a forum thread about using the custom headers generated by 6by9's program which also includes some information about how to use the resulting information in a table. What I want to know is the size of the tables! I also missed the important feature about when this needs to happen (before setting up the camera fully - I've not experimented to figure out exactly when).
Looking at his source code, the grid generated in the header file
More useful info might be in the commit where 6by9 mopdified raspistill to use a custom shading table