diff options
Diffstat (limited to 'src/py/cam/cam.py')
-rwxr-xr-x | src/py/cam/cam.py | 52 |
1 files changed, 30 insertions, 22 deletions
diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py index 2ae89fa8..ff4b7f66 100755 --- a/src/py/cam/cam.py +++ b/src/py/cam/cam.py @@ -3,9 +3,6 @@ # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> -# \todo Convert ctx and state dicts to proper classes, and move relevant -# functions to those classes. - from typing import Any import argparse import binascii @@ -26,6 +23,7 @@ class CameraContext: opt_metadata: bool opt_save_frames: bool opt_capture: int + opt_orientation: str stream_names: dict[libcam.Stream, str] streams: list[libcam.Stream] @@ -149,6 +147,21 @@ class CameraContext: if 'pixelformat' in stream_opts: stream_config.pixel_format = libcam.PixelFormat(stream_opts['pixelformat']) + if self.opt_orientation is not None: + orientation_map = { + 'rot0': libcam.Orientation.Rotate0, + 'rot180': libcam.Orientation.Rotate180, + 'mirror': libcam.Orientation.Rotate0Mirror, + 'flip': libcam.Orientation.Rotate180Mirror, + } + + orient = orientation_map.get(self.opt_orientation, None) + if orient is None: + print('Bad orientation: ', self.opt_orientation) + sys.exit(-1) + + camconfig.orientation = orient + stat = camconfig.validate() if stat == libcam.CameraConfiguration.Status.Invalid: @@ -161,9 +174,7 @@ class CameraContext: print('Camera configuration adjusted') - r = self.camera.configure(camconfig) - if r != 0: - raise Exception('Configure failed') + self.camera.configure(camconfig) self.stream_names = {} self.streams = [] @@ -178,12 +189,7 @@ class CameraContext: allocator = libcam.FrameBufferAllocator(self.camera) for stream in self.streams: - ret = allocator.allocate(stream) - if ret < 0: - print('Cannot allocate buffers') - exit(-1) - - allocated = len(allocator.buffers(stream)) + allocated = allocator.allocate(stream) print('{}-{}: Allocated {} buffers'.format(self.id, self.stream_names[stream], allocated)) @@ -208,10 +214,7 @@ class CameraContext: buffers = self.allocator.buffers(stream) buffer = buffers[buf_num] - ret = request.add_buffer(stream, buffer) - if ret < 0: - print('Can not set buffer for request') - exit(-1) + request.add_buffer(stream, buffer) requests.append(request) @@ -269,6 +272,11 @@ class CaptureState: ctx.last = ts ctx.fps = fps + if ctx.opt_metadata: + reqmeta = req.metadata + for ctrl, val in reqmeta.items(): + print(f'\t{ctrl} = {val}') + for stream, fb in buffers.items(): stream_name = ctx.stream_names[stream] @@ -287,11 +295,6 @@ class CaptureState: '/'.join([str(p.bytes_used) for p in meta.planes]), crcs)) - if ctx.opt_metadata: - reqmeta = req.metadata - for ctrl, val in reqmeta.items(): - print(f'\t{ctrl} = {val}') - if ctx.opt_save_frames: with libcamera.utils.MappedFrameBuffer(fb) as mfb: filename = 'frame-{}-{}-{}.data'.format(ctx.id, stream_name, ctx.reqs_completed) @@ -398,6 +401,7 @@ def main(): parser.add_argument('--metadata', nargs=0, type=bool, action=CustomAction, help='Print the metadata for completed requests') parser.add_argument('--strict-formats', type=bool, nargs=0, action=CustomAction, help='Do not allow requested stream format(s) to be adjusted') parser.add_argument('-s', '--stream', nargs='+', action=CustomAction) + parser.add_argument('-o', '--orientation', help='Desired image orientation (rot0, rot180, mirror, flip)') args = parser.parse_args() cm = libcam.CameraManager.singleton() @@ -421,6 +425,7 @@ def main(): ctx.opt_metadata = args.metadata.get(cam_idx, False) ctx.opt_strict_formats = args.strict_formats.get(cam_idx, False) ctx.opt_stream = args.stream.get(cam_idx, ['role=viewfinder']) + ctx.opt_orientation = args.orientation contexts.append(ctx) for ctx in contexts: @@ -434,7 +439,10 @@ def main(): if args.info: ctx.do_cmd_info() - if args.capture: + # Filter out capture contexts which are not marked for capture + contexts = [ctx for ctx in contexts if ctx.opt_capture > 0] + + if contexts: state = CaptureState(cm, contexts) if args.renderer == 'null': |