[libcamera-devel,v6,12/12] py: cam: Add option to set stream orientation
diff mbox series

Message ID 20231019140133.32090-13-jacopo.mondi@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: Replace CameraConfiguration::transform
Related show

Commit Message

Jacopo Mondi Oct. 19, 2023, 2:01 p.m. UTC
Add a '--orientation|-o' option to the Python version of the cam test
application to set an orientation to the image stream.

Supported values are:
- rot0: no rotation
- rot180: rotate 180 degrees
- flip: vertical flip
- mirror: horizontal flip

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 src/py/cam/cam.py | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

Comments

Laurent Pinchart Oct. 23, 2023, 11:30 a.m. UTC | #1
Hi Jacopo,

Thank you for the patch.

On Thu, Oct 19, 2023 at 04:01:33PM +0200, Jacopo Mondi via libcamera-devel wrote:
> Add a '--orientation|-o' option to the Python version of the cam test
> application to set an orientation to the image stream.
> 
> Supported values are:
> - rot0: no rotation
> - rot180: rotate 180 degrees
> - flip: vertical flip
> - mirror: horizontal flip
> 
> Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> ---
>  src/py/cam/cam.py | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
> 
> diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py
> index a2a115c164b4..06bd51aa5efe 100755
> --- a/src/py/cam/cam.py
> +++ b/src/py/cam/cam.py
> @@ -23,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]
> @@ -146,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:
> @@ -385,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()
> @@ -408,6 +425,10 @@ 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'])
> +        if args.orientation is not None:
> +            ctx.opt_orientation = args.orientation
> +        else:
> +            ctx.opt_orientation = None

Unless I'm mistaken, this can simply be written

        ctx.opt_orientation = args.orientation

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

>          contexts.append(ctx)
>  
>      for ctx in contexts:

Patch
diff mbox series

diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py
index a2a115c164b4..06bd51aa5efe 100755
--- a/src/py/cam/cam.py
+++ b/src/py/cam/cam.py
@@ -23,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]
@@ -146,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:
@@ -385,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()
@@ -408,6 +425,10 @@  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'])
+        if args.orientation is not None:
+            ctx.opt_orientation = args.orientation
+        else:
+            ctx.opt_orientation = None
         contexts.append(ctx)
 
     for ctx in contexts: