[{"id":32616,"web_url":"https://patchwork.libcamera.org/comment/32616/","msgid":"<20241209020623.GN17570@pendragon.ideasonboard.com>","date":"2024-12-09T02:06:23","subject":"Re: [PATCH v5 4/8] libtuning: Use logging framework in ctt_awb.awb()","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Stefan,\n\nThank you for the patch.\n\nOn Fri, Dec 06, 2024 at 03:52:24PM +0100, Stefan Klug wrote:\n> To be able to use the awb function copied from the Raspberry Pi tuning\n> scripts in the libtuning code, we need to remove the Cam object. Use the\n> logging framework to replace all accesses to Cam.log.\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n> \n> ---\n> Changes in v5:\n> - Replace format() with f-strings\n> ---\n>  utils/tuning/libtuning/ctt_awb.py | 50 ++++++++++++++++---------------\n>  1 file changed, 26 insertions(+), 24 deletions(-)\n> \n> diff --git a/utils/tuning/libtuning/ctt_awb.py b/utils/tuning/libtuning/ctt_awb.py\n> index abf22321a0ea..899f204da8cf 100644\n> --- a/utils/tuning/libtuning/ctt_awb.py\n> +++ b/utils/tuning/libtuning/ctt_awb.py\n> @@ -4,6 +4,8 @@\n>  #\n>  # camera tuning tool for AWB\n>  \n> +import logging\n> +\n>  import matplotlib.pyplot as plt\n>  from bisect import bisect_left\n>  from scipy.optimize import fmin\n> @@ -11,6 +13,7 @@ import numpy as np\n>  \n>  from .image import Image\n>  \n> +logger = logging.getLogger(__name__)\n>  \n>  \"\"\"\n>  obtain piecewise linear approximation for colour curve\n> @@ -39,7 +42,7 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):\n>      rb_raw = []\n>      rbs_hat = []\n>      for Img in imgs:\n> -        Cam.log += '\\nProcessing '+Img.name\n> +        logger.info(f'Processing {Img.name}')\n>          \"\"\"\n>          get greyscale patches with alsc applied if alsc enabled.\n>          Note: if alsc is disabled then colour_cals will be set to None and the\n> @@ -51,7 +54,7 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):\n>          \"\"\"\n>          r_g = np.mean(r_patchs/g_patchs)\n>          b_g = np.mean(b_patchs/g_patchs)\n> -        Cam.log += '\\n       r : {:.4f}       b : {:.4f}'.format(r_g, b_g)\n> +        logger.info(f'       r : {r_g:.4f}       b : {b_g:.4f}')\n>          \"\"\"\n>          The curve tends to be better behaved in so-called hatspace.\n>          R, B, G represent the individual channels. The colour curve is plotted in\n> @@ -74,12 +77,11 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):\n>          \"\"\"\n>          r_g_hat = r_g/(1+r_g+b_g)\n>          b_g_hat = b_g/(1+r_g+b_g)\n> -        Cam.log += '\\n   r_hat : {:.4f}   b_hat : {:.4f}'.format(r_g_hat, b_g_hat)\n> -        rbs_hat.append((r_g_hat, b_g_hat, Img.col))\n> +        logger.info(f'   r_hat : {r_g_hat:.4f}   b_hat : {b_g_hat:.4f}')\n> +        rbs_hat.append((r_g_hat, b_g_hat, Img.color))\n\nWith a mention of this change in the commit message,\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n>          rb_raw.append((r_g, b_g))\n> -        Cam.log += '\\n'\n>  \n> -    Cam.log += '\\nFinished processing images'\n> +    logger.info('Finished processing images')\n>      \"\"\"\n>      sort all lits simultaneously by r_hat\n>      \"\"\"\n> @@ -95,7 +97,7 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):\n>      fit quadratic fit to r_g hat and b_g_hat\n>      \"\"\"\n>      a, b, c = np.polyfit(rbs_hat[0], rbs_hat[1], 2)\n> -    Cam.log += '\\nFit quadratic curve in hatspace'\n> +    logger.info('Fit quadratic curve in hatspace')\n>      \"\"\"\n>      the algorithm now approximates the shortest distance from each point to the\n>      curve in dehatspace. Since the fit is done in hatspace, it is easier to\n> @@ -151,14 +153,14 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):\n>          if (x+y) > (rr+bb):\n>              dist *= -1\n>          dists.append(dist)\n> -    Cam.log += '\\nFound closest point on fit line to each point in dehatspace'\n> +    logger.info('Found closest point on fit line to each point in dehatspace')\n>      \"\"\"\n>      calculate wiggle factors in awb. 10% added since this is an upper bound\n>      \"\"\"\n>      transverse_neg = - np.min(dists) * 1.1\n>      transverse_pos = np.max(dists) * 1.1\n> -    Cam.log += '\\nTransverse pos : {:.5f}'.format(transverse_pos)\n> -    Cam.log += '\\nTransverse neg : {:.5f}'.format(transverse_neg)\n> +    logger.info(f'Transverse pos : {transverse_pos:.5f}')\n> +    logger.info(f'Transverse neg : {transverse_neg:.5f}')\n>      \"\"\"\n>      set minimum transverse wiggles to 0.1 .\n>      Wiggle factors dictate how far off of the curve the algorithm searches. 0.1\n> @@ -167,10 +169,10 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):\n>      \"\"\"\n>      if transverse_pos < 0.01:\n>          transverse_pos = 0.01\n> -        Cam.log += '\\nForced transverse pos to 0.01'\n> +        logger.info('Forced transverse pos to 0.01')\n>      if transverse_neg < 0.01:\n>          transverse_neg = 0.01\n> -        Cam.log += '\\nForced transverse neg to 0.01'\n> +        logger.info('Forced transverse neg to 0.01')\n>  \n>      \"\"\"\n>      generate new b_hat values at each r_hat according to fit\n> @@ -202,25 +204,25 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):\n>      i = len(c_fit) - 1\n>      while i > 0:\n>          if c_fit[i] > c_fit[i-1]:\n> -            Cam.log += '\\nColour temperature increase found\\n'\n> -            Cam.log += '{} K at r = {} to '.format(c_fit[i-1], r_fit[i-1])\n> -            Cam.log += '{} K at r = {}'.format(c_fit[i], r_fit[i])\n> +            logger.info('Colour temperature increase found')\n> +            logger.info(f'{c_fit[i - 1]} K at r = {r_fit[i - 1]} to ')\n> +            logger.info(f'{c_fit[i]} K at r = {r_fit[i]}')\n>              \"\"\"\n>              if colour temperature increases then discard point furthest from\n>              the transformed fit (dehatspace)\n>              \"\"\"\n>              error_1 = abs(dists[i-1])\n>              error_2 = abs(dists[i])\n> -            Cam.log += '\\nDistances from fit:\\n'\n> -            Cam.log += '{} K : {:.5f} , '.format(c_fit[i], error_1)\n> -            Cam.log += '{} K : {:.5f}'.format(c_fit[i-1], error_2)\n> +            logger.info('Distances from fit:')\n> +            logger.info(f'{c_fit[i]} K : {error_1:.5f}')\n> +            logger.info(f'{c_fit[i - 1]} K : {error_2:.5f}')\n>              \"\"\"\n>              find bad index\n>              note that in python false = 0 and true = 1\n>              \"\"\"\n>              bad = i - (error_1 < error_2)\n> -            Cam.log += '\\nPoint at {} K deleted as '.format(c_fit[bad])\n> -            Cam.log += 'it is furthest from fit'\n> +            logger.info(f'Point at {c_fit[bad]} K deleted as ')\n> +            logger.info('it is furthest from fit')\n>              \"\"\"\n>              delete bad point\n>              \"\"\"\n> @@ -239,12 +241,12 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):\n>      return formatted ct curve, ordered by increasing colour temperature\n>      \"\"\"\n>      ct_curve = list(np.array(list(zip(b_fit, r_fit, c_fit))).flatten())[::-1]\n> -    Cam.log += '\\nFinal CT curve:'\n> +    logger.info('Final CT curve:')\n>      for i in range(len(ct_curve)//3):\n>          j = 3*i\n> -        Cam.log += '\\n  ct: {}  '.format(ct_curve[j])\n> -        Cam.log += '  r: {}  '.format(ct_curve[j+1])\n> -        Cam.log += '  b: {}  '.format(ct_curve[j+2])\n> +        logger.info(f'  ct: {ct_curve[j]}  ')\n> +        logger.info(f'  r: {ct_curve[j + 1]}  ')\n> +        logger.info(f'  b: {ct_curve[j + 2]}  ')\n>  \n>      \"\"\"\n>      plotting code for debug","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 60FC7C3259\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  9 Dec 2024 02:06:41 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6F10167E42;\n\tMon,  9 Dec 2024 03:06:40 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 679A7618AD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  9 Dec 2024 03:06:39 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E75F5502;\n\tMon,  9 Dec 2024 03:06:07 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"cGJsevmd\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1733709968;\n\tbh=bFOkREeaPn4kN16FKtaDGGoRwof8jOIAvhPDEDRNcx0=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=cGJsevmd0YR05AabF7C7U0H1FVg57vd3F+GsqIWTeaOgxZggdQnkKB8OXEmghl6nw\n\tKvsT51rI1kEcGF0O7UQVP8Q2qrL5UDaVnDOnzVNcwGEAfFHoxraqXv0s1jMgXSyqor\n\tjAatPbY5nUrCJ0OmReFJIQeh9x9mEKfvCizogCZ0=","Date":"Mon, 9 Dec 2024 04:06:23 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tPaul Elder <paul.elder@ideasonboard.com>","Subject":"Re: [PATCH v5 4/8] libtuning: Use logging framework in ctt_awb.awb()","Message-ID":"<20241209020623.GN17570@pendragon.ideasonboard.com>","References":"<20241206145242.827886-1-stefan.klug@ideasonboard.com>\n\t<20241206145242.827886-5-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20241206145242.827886-5-stefan.klug@ideasonboard.com>","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]