From aa02706a34474eb108cf99f94396341e2709f045 Mon Sep 17 00:00:00 2001 From: Stefan Klug Date: Thu, 6 Jun 2024 11:58:33 +0200 Subject: libtuning: Migrate prints to python logging framework In ctt_ccm.py the logging functionality of the Cam object was used. As we don't want to port over that class, it needs to be replaced anyways. While at it, also replace the eprint function as it doesn't add any value over the logging framework and misses the ability for easy log formatting. For nice output formatting add the coloredlogs library. Signed-off-by: Stefan Klug Reviewed-by: Paul Elder Reviewed-by: Daniel Scally --- utils/tuning/libtuning/ctt_ccm.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'utils/tuning/libtuning/ctt_ccm.py') diff --git a/utils/tuning/libtuning/ctt_ccm.py b/utils/tuning/libtuning/ctt_ccm.py index f37adaf4..c4362756 100644 --- a/utils/tuning/libtuning/ctt_ccm.py +++ b/utils/tuning/libtuning/ctt_ccm.py @@ -4,6 +4,8 @@ # # camera tuning tool for CCM (colour correction matrix) +import logging + import numpy as np from scipy.optimize import minimize @@ -12,6 +14,8 @@ from .image import Image from .ctt_awb import get_alsc_patches from .utils import visualise_macbeth_chart +logger = logging.getLogger(__name__) + """ takes 8-bit macbeth chart values, degammas and returns 16 bit """ @@ -129,7 +133,7 @@ def ccm(Cam, cal_cr_list, cal_cb_list): """ ccm_tab = {} for Img in imgs: - Cam.log += '\nProcessing image: ' + Img.name + logger.info('Processing image: ' + Img.name) """ get macbeth patches with alsc applied if alsc enabled. Note: if alsc is disabled then colour_cals will be set to None and no @@ -154,7 +158,7 @@ def ccm(Cam, cal_cr_list, cal_cb_list): each channel for each patch """ gain = np.mean(m_srgb) / np.mean((r, g, b)) - Cam.log += '\nGain with respect to standard colours: {:.3f}'.format(gain) + logger.info(f'Gain with respect to standard colours: {gain:.3f}') r = np.mean(gain * r, axis=1) b = np.mean(gain * b, axis=1) g = np.mean(gain * g, axis=1) @@ -192,15 +196,13 @@ def ccm(Cam, cal_cr_list, cal_cb_list): zero since the input data is imperfect ''' - Cam.log += ("\n \n Optimised Matrix Below: \n \n") [r1, r2, g1, g2, b1, b2] = result.x # The new, optimised color correction matrix values + # This is the optimised Color Matrix (preserving greys by summing rows up to 1) optimised_ccm = [r1, r2, (1 - r1 - r2), g1, g2, (1 - g1 - g2), b1, b2, (1 - b1 - b2)] - # This is the optimised Color Matrix (preserving greys by summing rows up to 1) - Cam.log += str(optimised_ccm) - Cam.log += "\n Old Color Correction Matrix Below \n" - Cam.log += str(ccm) + logger.info(f'Optimized Matrix: {np.round(optimised_ccm, 4)}') + logger.info(f'Old Matrix: {np.round(ccm, 4)}') formatted_ccm = np.array(original_ccm).reshape((3, 3)) @@ -229,7 +231,7 @@ def ccm(Cam, cal_cr_list, cal_cb_list): We now want to spit out some data that shows how the optimisation has improved the color matrices ''' - Cam.log += "Here are the Improvements" + logger.info("Here are the Improvements") # CALCULATE WORST CASE delta e old_worst_delta_e = 0 @@ -244,8 +246,8 @@ def ccm(Cam, cal_cr_list, cal_cb_list): if new_delta_e > new_worst_delta_e: new_worst_delta_e = new_delta_e - Cam.log += "Before color correction matrix was optimised, we got an average delta E of " + str(before_average) + " and a maximum delta E of " + str(old_worst_delta_e) - Cam.log += "After color correction matrix was optimised, we got an average delta E of " + str(after_average) + " and a maximum delta E of " + str(new_worst_delta_e) + logger.info(f'delta E optimized: average: {after_average:.2f} max:{new_worst_delta_e:.2f}') + logger.info(f'delta E old: average: {before_average:.2f} max:{old_worst_delta_e:.2f}') visualise_macbeth_chart(m_rgb, optimised_ccm_rgb, after_gamma_rgb, str(Img.col) + str(matrix_selection_types[typenum])) ''' @@ -262,9 +264,8 @@ def ccm(Cam, cal_cr_list, cal_cb_list): ccm_tab[Img.col].append(optimised_ccm) else: ccm_tab[Img.col] = [optimised_ccm] - Cam.log += '\n' - Cam.log += '\nFinished processing images' + logger.info('Finished processing images') """ average any ccms that share a colour temperature """ @@ -273,7 +274,7 @@ def ccm(Cam, cal_cr_list, cal_cb_list): tab = np.where((10000 * tab) % 1 <= 0.05, tab + 0.00001, tab) tab = np.where((10000 * tab) % 1 >= 0.95, tab - 0.00001, tab) ccm_tab[k] = list(np.round(tab, 5)) - Cam.log += '\nMatrix calculated for colour temperature of {} K'.format(k) + logger.info(f'Matrix calculated for colour temperature of {k} K') """ return all ccms with respective colour temperature in the correct format, -- cgit v1.2.1