{"id":17690,"url":"https://patchwork.libcamera.org/api/1.1/patches/17690/?format=json","web_url":"https://patchwork.libcamera.org/patch/17690/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20221026163252.563851-1-tomi.valkeinen@ideasonboard.com>","date":"2022-10-26T16:32:52","name":"[libcamera-devel] py: examples: Add simple-capture-opencv.py","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"b04a2de6babb1fa70ad3af3996f3701ee3a851bd","submitter":{"id":109,"url":"https://patchwork.libcamera.org/api/1.1/people/109/?format=json","name":"Tomi Valkeinen","email":"tomi.valkeinen@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17690/mbox/","series":[{"id":3578,"url":"https://patchwork.libcamera.org/api/1.1/series/3578/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3578","date":"2022-10-26T16:32:52","name":"[libcamera-devel] py: examples: Add simple-capture-opencv.py","version":1,"mbox":"https://patchwork.libcamera.org/series/3578/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17690/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17690/checks/","tags":{},"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 74D6FBD16B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 26 Oct 2022 16:33:07 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C715362F5C;\n\tWed, 26 Oct 2022 18:33:06 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F29F661F4B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 26 Oct 2022 18:33:05 +0200 (CEST)","from desky.lan (91-154-32-225.elisa-laajakaista.fi [91.154.32.225])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 15E6E4F8;\n\tWed, 26 Oct 2022 18:33:05 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1666801986;\n\tbh=Win9EzSik4UiD6vhwhMXD8O5nm1PHSeXO5R8O7ajqIc=;\n\th=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:\n\tList-Help:List-Subscribe:From:Reply-To:From;\n\tb=OFhiqENvANehNP/WVGrtDXq5vyNkaP2eQ0Bn2ElVliQyJO5W9EZgN2yoUOvpZv1Sb\n\tye3SofTqJeKuYA+f2GgXUyQOZBf3yuyUtKmHsET9MAg/jcocAZYkiKz4MoBUQyfdNF\n\th4/fYRCdrHBZL8RdAYcNUuH43LoVNanyRazY3Ak56PKQLgG47ATn9mQJ+QlyaR5Pl4\n\t6qRTssYQNFD3lICPdLeKthoyECTYHn7Iq6M5phjTswCGezcbCtQP2q/FijQvk4vYxx\n\to1Cd2il67EggDl0tOdVAS7KJWcxe31eLg4VCKZCb2JLCkfnrrPDTuJ3q2DHc/kCf1t\n\tsNjw03RK61j5g==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1666801985;\n\tbh=Win9EzSik4UiD6vhwhMXD8O5nm1PHSeXO5R8O7ajqIc=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=lvY9CngcRFYuW1irTfYBjsXtiC7REK78KkvsIqVtRcYRmPimeRqMZ3bkz96VRkN/c\n\tCvlRyYC/Zap6B2niSEWQR3UR5n2DP7VBdAV36kAJMbxLV8kavTf/IMiUHbqkWFOGms\n\tgE1PAxzcn+OjhX6Nh3O6FUNGqUuHZPb4Y/AXETaI="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"lvY9Cngc\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tDavid Plowman <david.plowman@raspberrypi.com>","Date":"Wed, 26 Oct 2022 19:32:52 +0300","Message-Id":"<20221026163252.563851-1-tomi.valkeinen@ideasonboard.com>","X-Mailer":"git-send-email 2.34.1","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH] py: examples: Add simple-capture-opencv.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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a simple example showing how the captured frame can be imported into\nopencv and processed there.\n\nThis is only a simple example, and it has the same issue as all the\nother examples: pixel format conversions. Each pixel format requires\ncustom conversion code, and I have implemented the conversion only for a\nfew of them.\n\nAlso, this is almost identical to simple-capture.py. Normally it would\nmake sense to add common helpers, but so far I have stayed away from it\nto keep the examples independent and in a single file. Making the\nexamples \"too fancy\" will easily make them more difficult to understand\nfor a newcomer.\n\nSigned-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n---\n src/py/examples/simple-capture-opencv.py | 216 +++++++++++++++++++++++\n 1 file changed, 216 insertions(+)\n create mode 100755 src/py/examples/simple-capture-opencv.py","diff":"diff --git a/src/py/examples/simple-capture-opencv.py b/src/py/examples/simple-capture-opencv.py\nnew file mode 100755\nindex 00000000..5a34619e\n--- /dev/null\n+++ b/src/py/examples/simple-capture-opencv.py\n@@ -0,0 +1,216 @@\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+# A simple capture example with opencv. This is almost identical to\n+# simple-capture.py, except we use opencv to process and show the image.\n+\n+import argparse\n+import cv2\n+import libcamera as libcam\n+import libcamera.utils\n+import numpy as np\n+import selectors\n+import sys\n+\n+# Number of frames to capture\n+TOTAL_FRAMES = 100\n+\n+\n+def cartoonify(cvbuf):\n+    grayScaleImage = cv2.cvtColor(cvbuf, cv2.COLOR_BGR2GRAY)\n+    smoothGrayScale = cv2.medianBlur(grayScaleImage, 5)\n+    edgefied = cv2.adaptiveThreshold(smoothGrayScale, 255,\n+                                     cv2.ADAPTIVE_THRESH_MEAN_C,\n+                                     cv2.THRESH_BINARY, 9, 9)\n+    return edgefied\n+\n+\n+def libcam_to_cv(mfb: libcamera.utils.MappedFrameBuffer, cfg: libcam.StreamConfiguration):\n+    w = cfg.size.width\n+    h = cfg.size.height\n+    fmt = cfg.pixel_format\n+\n+    data = np.array(mfb.planes[0], dtype=np.uint8)\n+\n+    if fmt == libcam.formats.YUYV:\n+        # This is not correct, produces a bluish tint. I didn't find out\n+        # what kind of array opencv wants for YUYV.\n+        yuyv = data.reshape((h, w // 2 * 4))\n+        yuv = np.empty((h, w, 2), dtype=np.uint8)\n+        yuv[:, :, 0] = yuyv[:, 0::2]                    # Y\n+        yuv[:, :, 1] = yuyv[:, 1::2]                    # UV\n+        image = cv2.cvtColor(yuv, cv2.COLOR_YUV2RGB_YUYV)\n+    elif fmt == libcam.formats.RGB888:\n+        rgb = data.reshape((h, w, 3))\n+        rgb[:, :, [0, 1, 2]] = rgb[:, :, [2, 1, 0]]\n+        image = rgb\n+    elif fmt == libcam.formats.BGR888:\n+        rgb = data.reshape((h, w, 3))\n+        image = rgb\n+    elif fmt == libcam.formats.MJPEG:\n+        image = cv2.imdecode(data, cv2.IMREAD_COLOR)\n+    else:\n+        raise Exception(\"Unsupported pixel format\")\n+\n+    return image\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+    print(f'Capturing {TOTAL_FRAMES} frames with {stream_config}')\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+    # 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+    # frames_queued and frames_done track the number of frames queued and done\n+\n+    frames_queued = 0\n+    frames_done = 0\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+        frames_queued += 1\n+\n+    # The main loop. Wait for the queued Requests to complete, process them,\n+    # and re-queue them again.\n+\n+    sel = selectors.DefaultSelector()\n+    sel.register(cm.event_fd, selectors.EVENT_READ)\n+\n+    while frames_done < TOTAL_FRAMES:\n+        # cm.get_ready_requests() does not block, so we use a Selector to wait\n+        # for a camera event. Here we should almost always get a single\n+        # Request, but in some cases there could be multiple or none.\n+\n+        events = sel.select()\n+        if not events:\n+            continue\n+\n+        reqs = cm.get_ready_requests()\n+\n+        for req in reqs:\n+            frames_done += 1\n+\n+            buffers = req.buffers\n+\n+            # A ready Request could contain multiple buffers if multiple streams\n+            # were being used. Here we know we only have a single stream,\n+            # and we use next(iter()) to get the first and only buffer.\n+\n+            assert len(buffers) == 1\n+\n+            stream, fb = next(iter(buffers.items()))\n+\n+            with libcamera.utils.MappedFrameBuffer(fb) as mfb:\n+                # Convert the raw buffer to opencv (numpy) buffer\n+                image = libcam_to_cv(mfb, stream.configuration)\n+\n+                # Process the image\n+                image = cartoonify(image)\n+\n+                # Show the image in a window\n+                cv2.imshow('Image', image)\n+\n+                # This one is needed for the opencv to show the window...\n+                cv2.waitKey(1)\n+\n+            meta = fb.metadata\n+\n+            print(\"seq {:3}, bytes {}, frames queued/done {:3}/{:<3}\"\n+                  .format(meta.sequence,\n+                          '/'.join([str(p.bytes_used) for p in meta.planes]),\n+                          frames_queued, frames_done))\n+\n+            # If we want to capture more frames we need to queue more Requests.\n+            # We could create a totally new Request, but it is more efficient\n+            # to reuse the existing one that we just received.\n+            if frames_queued < TOTAL_FRAMES:\n+                req.reuse()\n+                cam.queue_request(req)\n+                frames_queued += 1\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","prefixes":["libcamera-devel"]}