[{"id":23325,"web_url":"https://patchwork.libcamera.org/comment/23325/","msgid":"<20220605123834.f5uvzj5kefrf4ukq@uno.localdomain>","date":"2022-06-05T12:38:34","subject":"Re: [libcamera-devel] [PATCH v4 15/16] py: examples: Add itest.py","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Tomi\n\nOn Mon, May 30, 2022 at 05:27:21PM +0300, Tomi Valkeinen wrote:\n> Add a small tool which gives the user an IPython based interactive shell\n> with libcamera capturing in the background. Can be used to study and\n> test small things with libcamera.\n>\n\nFeels very similar to simple-capture.py\n\nCan't opening the IPython shell be made an option to simple-capture ?\n\n> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n> ---\n>  src/py/examples/itest.py | 195 +++++++++++++++++++++++++++++++++++++++\n>  1 file changed, 195 insertions(+)\n>  create mode 100755 src/py/examples/itest.py\n>\n> diff --git a/src/py/examples/itest.py b/src/py/examples/itest.py\n> new file mode 100755\n> index 00000000..9c97649d\n> --- /dev/null\n> +++ b/src/py/examples/itest.py\n> @@ -0,0 +1,195 @@\n> +#!/usr/bin/env python3\n> +\n> +# SPDX-License-Identifier: BSD-3-Clause\n> +# Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n> +\n> +# This sets up a single camera, starts a capture loop in a background thread,\n> +# and starts an IPython shell which can be used to study and test libcamera.\n> +#\n> +# Note: This is not an example as such, but more of a developer tool to try\n> +# out things.\n> +\n> +import argparse\n> +import IPython\n> +import libcamera as libcam\n> +import selectors\n> +import sys\n> +import threading\n> +\n> +\n> +def handle_camera_event(ctx):\n> +    cm = ctx['cm']\n> +    cam = ctx['cam']\n> +\n> +    # cm.get_ready_requests() will not block here, as we know there is an event\n> +    # to read.\n> +\n> +    reqs = cm.get_ready_requests()\n> +\n> +    assert len(reqs) > 0\n> +\n> +    # Process the captured frames\n> +\n> +    for req in reqs:\n> +        buffers = req.buffers\n> +\n> +        assert len(buffers) == 1\n> +\n> +        # We want to re-queue the buffer we just handled. Instead of creating\n> +        # a new Request, we re-use the old one. We need to call req.reuse()\n> +        # to re-initialize the Request before queuing.\n> +\n> +        req.reuse()\n> +        cam.queue_request(req)\n> +\n> +        scope = ctx['scope']\n> +\n> +        if 'num_frames' not in scope:\n> +            scope['num_frames'] = 0\n> +\n> +        scope['num_frames'] += 1\n> +\n> +\n> +def capture(ctx):\n> +    cm = ctx['cm']\n> +    cam = ctx['cam']\n> +    reqs = ctx['reqs']\n> +\n> +    # Queue the requests to the camera\n> +\n> +    for req in reqs:\n> +        ret = cam.queue_request(req)\n> +        assert ret == 0\n> +\n> +    # Use Selector to wait for events from the camera and from the keyboard\n> +\n> +    sel = selectors.DefaultSelector()\n> +    sel.register(cm.event_fd, selectors.EVENT_READ, lambda fd: handle_camera_event(ctx))\n> +\n> +    reqs = []\n> +\n> +    while ctx['running']:\n> +        events = sel.select()\n> +        for key, mask in events:\n> +            key.data(key.fileobj)\n> +\n> +\n> +def main():\n> +    parser = argparse.ArgumentParser()\n> +    parser.add_argument('-c', '--camera', type=str, default='1',\n> +                        help='Camera index number (starting from 1) or part of the name')\n> +    parser.add_argument('-f', '--format', type=str, help='Pixel format')\n> +    parser.add_argument('-s', '--size', type=str, help='Size (\"WxH\")')\n> +    args = parser.parse_args()\n> +\n> +    cm = libcam.CameraManager.singleton()\n> +\n> +    try:\n> +        if args.camera.isnumeric():\n> +            cam_idx = int(args.camera)\n> +            cam = next((cam for i, cam in enumerate(cm.cameras) if i + 1 == cam_idx))\n> +        else:\n> +            cam = next((cam for cam in cm.cameras if args.camera in cam.id))\n> +    except Exception:\n> +        print(f'Failed to find camera \"{args.camera}\"')\n> +        return -1\n> +\n> +    # Acquire the camera for our use\n> +\n> +    ret = cam.acquire()\n> +    assert ret == 0\n> +\n> +    # Configure the camera\n> +\n> +    cam_config = cam.generate_configuration([libcam.StreamRole.Viewfinder])\n> +\n> +    stream_config = cam_config.at(0)\n> +\n> +    if args.format:\n> +        fmt = libcam.PixelFormat(args.format)\n> +        stream_config.pixel_format = fmt\n> +\n> +    if args.size:\n> +        w, h = [int(v) for v in args.size.split('x')]\n> +        stream_config.size = libcam.Size(w, h)\n> +\n> +    ret = cam.configure(cam_config)\n> +    assert ret == 0\n> +\n> +    stream = stream_config.stream\n> +\n> +    # Allocate the buffers for capture\n> +\n> +    allocator = libcam.FrameBufferAllocator(cam)\n> +    ret = allocator.allocate(stream)\n> +    assert ret > 0\n> +\n> +    num_bufs = len(allocator.buffers(stream))\n> +\n> +    print(f'Capturing {stream_config} with {num_bufs} buffers from {cam.id}')\n> +\n> +    # Create the requests and assign a buffer for each request\n> +\n> +    reqs = []\n> +    for i in range(num_bufs):\n> +        # Use the buffer index as the \"cookie\"\n> +        req = cam.create_request(i)\n> +\n> +        buffer = allocator.buffers(stream)[i]\n> +        ret = req.add_buffer(stream, buffer)\n> +        assert ret == 0\n> +\n> +        reqs.append(req)\n> +\n> +    # Start the camera\n> +\n> +    ret = cam.start()\n> +    assert ret == 0\n> +\n> +    # Create a scope for the IPython shell\n> +    scope = {\n> +        'cm': cm,\n> +        'cam': cam,\n> +        'libcam': libcam,\n> +    }\n> +\n> +    # Create a simple context shared between the background thread and the main\n> +    # thread.\n> +    ctx = {\n> +        'running': True,\n> +        'cm': cm,\n> +        'cam': cam,\n> +        'reqs': reqs,\n> +        'scope': scope,\n> +    }\n> +\n> +    # Note that \"In CPython, due to the Global Interpreter Lock, only one thread\n> +    # can execute Python code at once\". We rely on that here, which is not very\n> +    # nice. I am sure an fd-based polling loop could be integrated with IPython,\n> +    # somehow, which would allow us to drop the threading.\n> +\n> +    t = threading.Thread(target=capture, args=(ctx,))\n> +    t.start()\n> +\n> +    IPython.embed(banner1='', exit_msg='', confirm_exit=False, user_ns=scope)\n> +\n> +    print('Exiting...')\n> +\n> +    ctx['running'] = False\n> +    t.join()\n> +\n> +    # Stop the camera\n> +\n> +    ret = cam.stop()\n> +    assert ret == 0\n> +\n> +    # Release the camera\n> +\n> +    ret = cam.release()\n> +    assert ret == 0\n> +\n> +    return 0\n> +\n> +\n> +if __name__ == '__main__':\n> +    sys.exit(main())\n> --\n> 2.34.1\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 363D9BD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun,  5 Jun 2022 12:38:40 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 47A0C61FB5;\n\tSun,  5 Jun 2022 14:38:39 +0200 (CEST)","from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net\n\t[217.70.183.197])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D38AD60104\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  5 Jun 2022 14:38:37 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby mail.gandi.net (Postfix) with ESMTPSA id AF2831C0004;\n\tSun,  5 Jun 2022 12:38:36 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1654432719;\n\tbh=IZWEEcQEuDHH1Fl9h1HEcxgUPsYR5eNpY8MQWGHAd2I=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=ExpCkZprr7F1tfWnTfMXMYBkrgJ2Rx83IF3ge5+qHKm6ZPAC0rys0TLCPX6jRM1u+\n\tW8yjHXS9pPTtNyjxeDAU55XvXBvkKU32/fyFaesNKQhbaxRbWTPeOwiSYhXprv4fQA\n\tluUMOuOHbAHy/80x4IDLz+rTczkNbrxXDEQc2H4k4Mux9+WQB3+OviZlSSOJl0YtV1\n\tzUhsM09HO453MLzplUwm/Idj0N2N/0bjERziGtG3cwqawlpCcM5ewtSUEbNQIlWgrT\n\t/nGKtCk7uxGOyoFUIpsRkDaAej/6O+xBRN5w4ZvgHUU9a1yyI4TxKdD9PsSSCRDa+H\n\tA2+0eVo4qSC7w==","Date":"Sun, 5 Jun 2022 14:38:34 +0200","To":"Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>","Message-ID":"<20220605123834.f5uvzj5kefrf4ukq@uno.localdomain>","References":"<20220530142722.57618-1-tomi.valkeinen@ideasonboard.com>\n\t<20220530142722.57618-16-tomi.valkeinen@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220530142722.57618-16-tomi.valkeinen@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v4 15/16] py: examples: Add itest.py","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":23333,"web_url":"https://patchwork.libcamera.org/comment/23333/","msgid":"<804de5a0-93aa-e35c-b99d-db82170921aa@ideasonboard.com>","date":"2022-06-06T08:58:41","subject":"Re: [libcamera-devel] [PATCH v4 15/16] py: examples: Add itest.py","submitter":{"id":109,"url":"https://patchwork.libcamera.org/api/people/109/","name":"Tomi Valkeinen","email":"tomi.valkeinen@ideasonboard.com"},"content":"On 05/06/2022 15:38, Jacopo Mondi wrote:\n> Hi Tomi\n> \n> On Mon, May 30, 2022 at 05:27:21PM +0300, Tomi Valkeinen wrote:\n>> Add a small tool which gives the user an IPython based interactive shell\n>> with libcamera capturing in the background. Can be used to study and\n>> test small things with libcamera.\n>>\n> \n> Feels very similar to simple-capture.py\n> \n> Can't opening the IPython shell be made an option to simple-capture ?\n\nYes, this is a bit extra in many ways, and not an example as such. But \nalso handy =).\n\nI don't want to add anything extra to simple-capture, and I think this \ncould be improved in many ways, going into different direction than \nsimple-capture.\n\n  Tomi","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 1659DBD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  6 Jun 2022 08:58:48 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7DB2E65635;\n\tMon,  6 Jun 2022 10:58:47 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 37E5F633A4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  6 Jun 2022 10:58:45 +0200 (CEST)","from [192.168.1.111] (91-156-85-209.elisa-laajakaista.fi\n\t[91.156.85.209])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7344530A;\n\tMon,  6 Jun 2022 10:58:44 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1654505927;\n\tbh=js3NvA4ftu+pts3Tw2oPwnXrtcY5fpX4DgBTbr3clgk=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=aipU/oVguaavCXY85zzcraX0FDSof2K0RxIRC0ApUshmPJOPkT5xFLmiSEBss1Qu0\n\toFvJ12vQBFNwtYyrc37hxBbOzwjL+JIVYvKmVUyBNxZOwqep6/GRzKUpZb2HMEiHRm\n\tVj/i4S+8sZQW27OaAKnZiuVCu2tcCda6T1RHpZFdxhVC2C+faimy4o+OU7sGrMWo46\n\t7CMje0vAAfRDC2bQ6nnb3StEe9WJbqAvHeW/FGC4brwHQYmy1Bv9KBnuc9KyjzEIab\n\tgpRtrHT25xYFb9lIBdzQ/PeJ8pCFRTYCpk7VZ72t35XOwT4libCHaGULm+RJrvyix3\n\tP4VgNDT3bIl2Q==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1654505924;\n\tbh=js3NvA4ftu+pts3Tw2oPwnXrtcY5fpX4DgBTbr3clgk=;\n\th=Date:Subject:To:Cc:References:From:In-Reply-To:From;\n\tb=iYZUZpMwzzrEyq6MMB7yMSB+AHWjpSJg+z/Ffma1o6iS4wKGFe7FyrYeDx9RatwyC\n\tFQ4aw5kRjrFfqmdUPdHBdrkujFah2UCmACf4U+6AuaISBPXcwOmesehfpv5XL1+GmH\n\tCeDr6iHFy0de7fViR9L234qnOR4jpUeyGB+qh/oo="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"iYZUZpMw\"; dkim-atps=neutral","Message-ID":"<804de5a0-93aa-e35c-b99d-db82170921aa@ideasonboard.com>","Date":"Mon, 6 Jun 2022 11:58:41 +0300","MIME-Version":"1.0","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101\n\tThunderbird/91.9.1","Content-Language":"en-US","To":"Jacopo Mondi <jacopo@jmondi.org>","References":"<20220530142722.57618-1-tomi.valkeinen@ideasonboard.com>\n\t<20220530142722.57618-16-tomi.valkeinen@ideasonboard.com>\n\t<20220605123834.f5uvzj5kefrf4ukq@uno.localdomain>","In-Reply-To":"<20220605123834.f5uvzj5kefrf4ukq@uno.localdomain>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"7bit","Subject":"Re: [libcamera-devel] [PATCH v4 15/16] py: examples: Add itest.py","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Tomi Valkeinen via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]