[v5,4/8] libtuning: Use logging framework in ctt_awb.awb()
diff mbox series

Message ID 20241206145242.827886-5-stefan.klug@ideasonboard.com
State New
Headers show
Series
  • rkisp1: Add manual colour temperature control
Related show

Commit Message

Stefan Klug Dec. 6, 2024, 2:52 p.m. UTC
To be able to use the awb function copied from the Raspberry Pi tuning
scripts in the libtuning code, we need to remove the Cam object. Use the
logging framework to replace all accesses to Cam.log.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>

---
Changes in v5:
- Replace format() with f-strings
---
 utils/tuning/libtuning/ctt_awb.py | 50 ++++++++++++++++---------------
 1 file changed, 26 insertions(+), 24 deletions(-)

Comments

Laurent Pinchart Dec. 9, 2024, 2:06 a.m. UTC | #1
Hi Stefan,

Thank you for the patch.

On Fri, Dec 06, 2024 at 03:52:24PM +0100, Stefan Klug wrote:
> To be able to use the awb function copied from the Raspberry Pi tuning
> scripts in the libtuning code, we need to remove the Cam object. Use the
> logging framework to replace all accesses to Cam.log.
> 
> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
> 
> ---
> Changes in v5:
> - Replace format() with f-strings
> ---
>  utils/tuning/libtuning/ctt_awb.py | 50 ++++++++++++++++---------------
>  1 file changed, 26 insertions(+), 24 deletions(-)
> 
> diff --git a/utils/tuning/libtuning/ctt_awb.py b/utils/tuning/libtuning/ctt_awb.py
> index abf22321a0ea..899f204da8cf 100644
> --- a/utils/tuning/libtuning/ctt_awb.py
> +++ b/utils/tuning/libtuning/ctt_awb.py
> @@ -4,6 +4,8 @@
>  #
>  # camera tuning tool for AWB
>  
> +import logging
> +
>  import matplotlib.pyplot as plt
>  from bisect import bisect_left
>  from scipy.optimize import fmin
> @@ -11,6 +13,7 @@ import numpy as np
>  
>  from .image import Image
>  
> +logger = logging.getLogger(__name__)
>  
>  """
>  obtain piecewise linear approximation for colour curve
> @@ -39,7 +42,7 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):
>      rb_raw = []
>      rbs_hat = []
>      for Img in imgs:
> -        Cam.log += '\nProcessing '+Img.name
> +        logger.info(f'Processing {Img.name}')
>          """
>          get greyscale patches with alsc applied if alsc enabled.
>          Note: if alsc is disabled then colour_cals will be set to None and the
> @@ -51,7 +54,7 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):
>          """
>          r_g = np.mean(r_patchs/g_patchs)
>          b_g = np.mean(b_patchs/g_patchs)
> -        Cam.log += '\n       r : {:.4f}       b : {:.4f}'.format(r_g, b_g)
> +        logger.info(f'       r : {r_g:.4f}       b : {b_g:.4f}')
>          """
>          The curve tends to be better behaved in so-called hatspace.
>          R, B, G represent the individual channels. The colour curve is plotted in
> @@ -74,12 +77,11 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):
>          """
>          r_g_hat = r_g/(1+r_g+b_g)
>          b_g_hat = b_g/(1+r_g+b_g)
> -        Cam.log += '\n   r_hat : {:.4f}   b_hat : {:.4f}'.format(r_g_hat, b_g_hat)
> -        rbs_hat.append((r_g_hat, b_g_hat, Img.col))
> +        logger.info(f'   r_hat : {r_g_hat:.4f}   b_hat : {b_g_hat:.4f}')
> +        rbs_hat.append((r_g_hat, b_g_hat, Img.color))

With a mention of this change in the commit message,

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

>          rb_raw.append((r_g, b_g))
> -        Cam.log += '\n'
>  
> -    Cam.log += '\nFinished processing images'
> +    logger.info('Finished processing images')
>      """
>      sort all lits simultaneously by r_hat
>      """
> @@ -95,7 +97,7 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):
>      fit quadratic fit to r_g hat and b_g_hat
>      """
>      a, b, c = np.polyfit(rbs_hat[0], rbs_hat[1], 2)
> -    Cam.log += '\nFit quadratic curve in hatspace'
> +    logger.info('Fit quadratic curve in hatspace')
>      """
>      the algorithm now approximates the shortest distance from each point to the
>      curve in dehatspace. Since the fit is done in hatspace, it is easier to
> @@ -151,14 +153,14 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):
>          if (x+y) > (rr+bb):
>              dist *= -1
>          dists.append(dist)
> -    Cam.log += '\nFound closest point on fit line to each point in dehatspace'
> +    logger.info('Found closest point on fit line to each point in dehatspace')
>      """
>      calculate wiggle factors in awb. 10% added since this is an upper bound
>      """
>      transverse_neg = - np.min(dists) * 1.1
>      transverse_pos = np.max(dists) * 1.1
> -    Cam.log += '\nTransverse pos : {:.5f}'.format(transverse_pos)
> -    Cam.log += '\nTransverse neg : {:.5f}'.format(transverse_neg)
> +    logger.info(f'Transverse pos : {transverse_pos:.5f}')
> +    logger.info(f'Transverse neg : {transverse_neg:.5f}')
>      """
>      set minimum transverse wiggles to 0.1 .
>      Wiggle factors dictate how far off of the curve the algorithm searches. 0.1
> @@ -167,10 +169,10 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):
>      """
>      if transverse_pos < 0.01:
>          transverse_pos = 0.01
> -        Cam.log += '\nForced transverse pos to 0.01'
> +        logger.info('Forced transverse pos to 0.01')
>      if transverse_neg < 0.01:
>          transverse_neg = 0.01
> -        Cam.log += '\nForced transverse neg to 0.01'
> +        logger.info('Forced transverse neg to 0.01')
>  
>      """
>      generate new b_hat values at each r_hat according to fit
> @@ -202,25 +204,25 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):
>      i = len(c_fit) - 1
>      while i > 0:
>          if c_fit[i] > c_fit[i-1]:
> -            Cam.log += '\nColour temperature increase found\n'
> -            Cam.log += '{} K at r = {} to '.format(c_fit[i-1], r_fit[i-1])
> -            Cam.log += '{} K at r = {}'.format(c_fit[i], r_fit[i])
> +            logger.info('Colour temperature increase found')
> +            logger.info(f'{c_fit[i - 1]} K at r = {r_fit[i - 1]} to ')
> +            logger.info(f'{c_fit[i]} K at r = {r_fit[i]}')
>              """
>              if colour temperature increases then discard point furthest from
>              the transformed fit (dehatspace)
>              """
>              error_1 = abs(dists[i-1])
>              error_2 = abs(dists[i])
> -            Cam.log += '\nDistances from fit:\n'
> -            Cam.log += '{} K : {:.5f} , '.format(c_fit[i], error_1)
> -            Cam.log += '{} K : {:.5f}'.format(c_fit[i-1], error_2)
> +            logger.info('Distances from fit:')
> +            logger.info(f'{c_fit[i]} K : {error_1:.5f}')
> +            logger.info(f'{c_fit[i - 1]} K : {error_2:.5f}')
>              """
>              find bad index
>              note that in python false = 0 and true = 1
>              """
>              bad = i - (error_1 < error_2)
> -            Cam.log += '\nPoint at {} K deleted as '.format(c_fit[bad])
> -            Cam.log += 'it is furthest from fit'
> +            logger.info(f'Point at {c_fit[bad]} K deleted as ')
> +            logger.info('it is furthest from fit')
>              """
>              delete bad point
>              """
> @@ -239,12 +241,12 @@ def awb(Cam, cal_cr_list, cal_cb_list, plot):
>      return formatted ct curve, ordered by increasing colour temperature
>      """
>      ct_curve = list(np.array(list(zip(b_fit, r_fit, c_fit))).flatten())[::-1]
> -    Cam.log += '\nFinal CT curve:'
> +    logger.info('Final CT curve:')
>      for i in range(len(ct_curve)//3):
>          j = 3*i
> -        Cam.log += '\n  ct: {}  '.format(ct_curve[j])
> -        Cam.log += '  r: {}  '.format(ct_curve[j+1])
> -        Cam.log += '  b: {}  '.format(ct_curve[j+2])
> +        logger.info(f'  ct: {ct_curve[j]}  ')
> +        logger.info(f'  r: {ct_curve[j + 1]}  ')
> +        logger.info(f'  b: {ct_curve[j + 2]}  ')
>  
>      """
>      plotting code for debug

Patch
diff mbox series

diff --git a/utils/tuning/libtuning/ctt_awb.py b/utils/tuning/libtuning/ctt_awb.py
index abf22321a0ea..899f204da8cf 100644
--- a/utils/tuning/libtuning/ctt_awb.py
+++ b/utils/tuning/libtuning/ctt_awb.py
@@ -4,6 +4,8 @@ 
 #
 # camera tuning tool for AWB
 
+import logging
+
 import matplotlib.pyplot as plt
 from bisect import bisect_left
 from scipy.optimize import fmin
@@ -11,6 +13,7 @@  import numpy as np
 
 from .image import Image
 
+logger = logging.getLogger(__name__)
 
 """
 obtain piecewise linear approximation for colour curve
@@ -39,7 +42,7 @@  def awb(Cam, cal_cr_list, cal_cb_list, plot):
     rb_raw = []
     rbs_hat = []
     for Img in imgs:
-        Cam.log += '\nProcessing '+Img.name
+        logger.info(f'Processing {Img.name}')
         """
         get greyscale patches with alsc applied if alsc enabled.
         Note: if alsc is disabled then colour_cals will be set to None and the
@@ -51,7 +54,7 @@  def awb(Cam, cal_cr_list, cal_cb_list, plot):
         """
         r_g = np.mean(r_patchs/g_patchs)
         b_g = np.mean(b_patchs/g_patchs)
-        Cam.log += '\n       r : {:.4f}       b : {:.4f}'.format(r_g, b_g)
+        logger.info(f'       r : {r_g:.4f}       b : {b_g:.4f}')
         """
         The curve tends to be better behaved in so-called hatspace.
         R, B, G represent the individual channels. The colour curve is plotted in
@@ -74,12 +77,11 @@  def awb(Cam, cal_cr_list, cal_cb_list, plot):
         """
         r_g_hat = r_g/(1+r_g+b_g)
         b_g_hat = b_g/(1+r_g+b_g)
-        Cam.log += '\n   r_hat : {:.4f}   b_hat : {:.4f}'.format(r_g_hat, b_g_hat)
-        rbs_hat.append((r_g_hat, b_g_hat, Img.col))
+        logger.info(f'   r_hat : {r_g_hat:.4f}   b_hat : {b_g_hat:.4f}')
+        rbs_hat.append((r_g_hat, b_g_hat, Img.color))
         rb_raw.append((r_g, b_g))
-        Cam.log += '\n'
 
-    Cam.log += '\nFinished processing images'
+    logger.info('Finished processing images')
     """
     sort all lits simultaneously by r_hat
     """
@@ -95,7 +97,7 @@  def awb(Cam, cal_cr_list, cal_cb_list, plot):
     fit quadratic fit to r_g hat and b_g_hat
     """
     a, b, c = np.polyfit(rbs_hat[0], rbs_hat[1], 2)
-    Cam.log += '\nFit quadratic curve in hatspace'
+    logger.info('Fit quadratic curve in hatspace')
     """
     the algorithm now approximates the shortest distance from each point to the
     curve in dehatspace. Since the fit is done in hatspace, it is easier to
@@ -151,14 +153,14 @@  def awb(Cam, cal_cr_list, cal_cb_list, plot):
         if (x+y) > (rr+bb):
             dist *= -1
         dists.append(dist)
-    Cam.log += '\nFound closest point on fit line to each point in dehatspace'
+    logger.info('Found closest point on fit line to each point in dehatspace')
     """
     calculate wiggle factors in awb. 10% added since this is an upper bound
     """
     transverse_neg = - np.min(dists) * 1.1
     transverse_pos = np.max(dists) * 1.1
-    Cam.log += '\nTransverse pos : {:.5f}'.format(transverse_pos)
-    Cam.log += '\nTransverse neg : {:.5f}'.format(transverse_neg)
+    logger.info(f'Transverse pos : {transverse_pos:.5f}')
+    logger.info(f'Transverse neg : {transverse_neg:.5f}')
     """
     set minimum transverse wiggles to 0.1 .
     Wiggle factors dictate how far off of the curve the algorithm searches. 0.1
@@ -167,10 +169,10 @@  def awb(Cam, cal_cr_list, cal_cb_list, plot):
     """
     if transverse_pos < 0.01:
         transverse_pos = 0.01
-        Cam.log += '\nForced transverse pos to 0.01'
+        logger.info('Forced transverse pos to 0.01')
     if transverse_neg < 0.01:
         transverse_neg = 0.01
-        Cam.log += '\nForced transverse neg to 0.01'
+        logger.info('Forced transverse neg to 0.01')
 
     """
     generate new b_hat values at each r_hat according to fit
@@ -202,25 +204,25 @@  def awb(Cam, cal_cr_list, cal_cb_list, plot):
     i = len(c_fit) - 1
     while i > 0:
         if c_fit[i] > c_fit[i-1]:
-            Cam.log += '\nColour temperature increase found\n'
-            Cam.log += '{} K at r = {} to '.format(c_fit[i-1], r_fit[i-1])
-            Cam.log += '{} K at r = {}'.format(c_fit[i], r_fit[i])
+            logger.info('Colour temperature increase found')
+            logger.info(f'{c_fit[i - 1]} K at r = {r_fit[i - 1]} to ')
+            logger.info(f'{c_fit[i]} K at r = {r_fit[i]}')
             """
             if colour temperature increases then discard point furthest from
             the transformed fit (dehatspace)
             """
             error_1 = abs(dists[i-1])
             error_2 = abs(dists[i])
-            Cam.log += '\nDistances from fit:\n'
-            Cam.log += '{} K : {:.5f} , '.format(c_fit[i], error_1)
-            Cam.log += '{} K : {:.5f}'.format(c_fit[i-1], error_2)
+            logger.info('Distances from fit:')
+            logger.info(f'{c_fit[i]} K : {error_1:.5f}')
+            logger.info(f'{c_fit[i - 1]} K : {error_2:.5f}')
             """
             find bad index
             note that in python false = 0 and true = 1
             """
             bad = i - (error_1 < error_2)
-            Cam.log += '\nPoint at {} K deleted as '.format(c_fit[bad])
-            Cam.log += 'it is furthest from fit'
+            logger.info(f'Point at {c_fit[bad]} K deleted as ')
+            logger.info('it is furthest from fit')
             """
             delete bad point
             """
@@ -239,12 +241,12 @@  def awb(Cam, cal_cr_list, cal_cb_list, plot):
     return formatted ct curve, ordered by increasing colour temperature
     """
     ct_curve = list(np.array(list(zip(b_fit, r_fit, c_fit))).flatten())[::-1]
-    Cam.log += '\nFinal CT curve:'
+    logger.info('Final CT curve:')
     for i in range(len(ct_curve)//3):
         j = 3*i
-        Cam.log += '\n  ct: {}  '.format(ct_curve[j])
-        Cam.log += '  r: {}  '.format(ct_curve[j+1])
-        Cam.log += '  b: {}  '.format(ct_curve[j+2])
+        logger.info(f'  ct: {ct_curve[j]}  ')
+        logger.info(f'  r: {ct_curve[j + 1]}  ')
+        logger.info(f'  b: {ct_curve[j + 2]}  ')
 
     """
     plotting code for debug