From 5826b429c8b10b743ea1ddcc0d8bb3e3241ed735 Mon Sep 17 00:00:00 2001 From: Somnath Pandit Date: Sat, 16 Dec 2023 15:39:41 +0530 Subject: [PATCH 1/5] calib data save bug resolve --- simple_picameraGUI.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/simple_picameraGUI.py b/simple_picameraGUI.py index bd627d6..bdf16c4 100644 --- a/simple_picameraGUI.py +++ b/simple_picameraGUI.py @@ -211,17 +211,20 @@ def _calibration_frame(self): self.btn_OK.grid(row=0, column=9, padx=5) def _recalculate_scale(self, *event): - self.calib_data = self.bars_per_um_per_unit_zoom - with open(self.save_dir + "calib.json", "w") as f: - json.dump(self.calib_data, f, indent=2) - scale_unit_um = 1 if self.scale_unit.get() == "mm": scale_unit_um = 1000 # 1mm = 1000um + current_zoom = int(self.lens_zoom.get()[:-1]) self.bars_per_um_per_unit_zoom = self.scalebar_len / ( self.physical_len.get() * scale_unit_um * current_zoom ) # physical len in um + + # Save new calibration data to calib.json + self.calib_data = self.bars_per_um_per_unit_zoom + with open(self.save_dir + "calib.json", "w") as f: + json.dump(self.calib_data, f, indent=2) + self._update_fixed_scalebar() def _update_fixed_scalebar(self): From 05bc5ed9a38c4f5ede9909c4118411777165888f Mon Sep 17 00:00:00 2001 From: Somnath Pandit Date: Sat, 16 Dec 2023 15:44:49 +0530 Subject: [PATCH 2/5] print calibration data saved --- simple_picameraGUI.py | 1 + 1 file changed, 1 insertion(+) diff --git a/simple_picameraGUI.py b/simple_picameraGUI.py index bdf16c4..b6fd558 100644 --- a/simple_picameraGUI.py +++ b/simple_picameraGUI.py @@ -224,6 +224,7 @@ def _recalculate_scale(self, *event): self.calib_data = self.bars_per_um_per_unit_zoom with open(self.save_dir + "calib.json", "w") as f: json.dump(self.calib_data, f, indent=2) + print("calibration data saved!", self.calib_data) self._update_fixed_scalebar() From 76df323800e28132d57efb4489ee2bd1ed742bb7 Mon Sep 17 00:00:00 2001 From: Somnath Pandit Date: Sat, 16 Dec 2023 16:13:25 +0530 Subject: [PATCH 3/5] calibration data verbose and warning --- simple_picameraGUI.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/simple_picameraGUI.py b/simple_picameraGUI.py index b6fd558..7e9ea2e 100644 --- a/simple_picameraGUI.py +++ b/simple_picameraGUI.py @@ -54,10 +54,13 @@ def __init__(self, **kw) -> None: try: with open(self.save_dir + "calib.json", "r") as f: self.bars_per_um_per_unit_zoom = json.load(f) + print("calibration data loaded.") except: self.calib_data = self.bars_per_um_per_unit_zoom - with open(self.save_dir + "calib.json", "w") as f: - json.dump(self.calib_data, f, indent=2) + messagebox.showwarning( + "Calibration error", "Scalebar calibration data not found please recalibrate." + ) + print("calibration file 'calib.json' not found in save dir.") self._update_fixed_scalebar() # self.bind("", self._hide_input_window) From d8f6b617a6a1ab04cfa4bd54509036be3f408c92 Mon Sep 17 00:00:00 2001 From: Somnath Pandit Date: Sat, 16 Dec 2023 16:39:00 +0530 Subject: [PATCH 4/5] code clean inital calib data load --- simple_picameraGUI.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/simple_picameraGUI.py b/simple_picameraGUI.py index 7e9ea2e..a44fbe2 100644 --- a/simple_picameraGUI.py +++ b/simple_picameraGUI.py @@ -24,11 +24,12 @@ def __init__(self, **kw) -> None: self.minsize(410, 300) self.title("sPiCameraGUI") + # For scalebar self.lens_zoom = StringVar(self, "5X") self.scale_unit = StringVar(self, "um") self.fixed_scalebar_len = 50 self.scalebar_len = self.fixed_scalebar_len - self.physical_len = DoubleVar(self, 100.0) + self.physical_len = DoubleVar(self, 100.0) # um self.scale_bar_font_size = 10 self.save_dir = os.path.join(homedir, "PiCamCapture", "") @@ -43,25 +44,7 @@ def __init__(self, **kw) -> None: self._camera_init() self._set_camera_preview_size() - # default bars_per_um_per_unit_zoom calculate for initialization - scale_unit_um = 1 - current_zoom = int(self.lens_zoom.get()[:-1]) - self.bars_per_um_per_unit_zoom = self.scalebar_len / ( - self.physical_len.get() * scale_unit_um * current_zoom - ) # physical len in um - - # try loading calib data from calib.json else create initial file - try: - with open(self.save_dir + "calib.json", "r") as f: - self.bars_per_um_per_unit_zoom = json.load(f) - print("calibration data loaded.") - except: - self.calib_data = self.bars_per_um_per_unit_zoom - messagebox.showwarning( - "Calibration error", "Scalebar calibration data not found please recalibrate." - ) - print("calibration file 'calib.json' not found in save dir.") - + self._load_calib_data() self._update_fixed_scalebar() # self.bind("", self._hide_input_window) @@ -77,6 +60,25 @@ def _camera_init(self): self.quit() exit() + def _load_calib_data(self): + # try loading calib data from calib.json else create initial file + try: + with open(self.save_dir + "calib.json", "r") as f: + self.bars_per_um_per_unit_zoom = json.load(f) + print("calibration data loaded.") + except: + self.calib_data = self.bars_per_um_per_unit_zoom + messagebox.showwarning( + "Calibration error", "Scalebar calibration data not found please recalibrate." + ) + print( + "calibration file 'calib.json' not found in save dir.\nprocceding with default calibration.." + ) + # default bars_per_um_per_unit_zoom calculate for initialization + self.bars_per_um_per_unit_zoom = self.scalebar_len / ( + self.physical_len.get() * int(self.lens_zoom.get()[:-1]) + ) # physical len in um + def create_frames(self): self.window = Frame(self.master) self.screen_width, self.screen_height = ( From aba9f2ecfa3696cfab49b1312e7b1dfb1e1c836f Mon Sep 17 00:00:00 2001 From: Somnath Pandit Date: Sat, 16 Dec 2023 16:45:38 +0530 Subject: [PATCH 5/5] initial calib data problem solved --- simple_picameraGUI.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simple_picameraGUI.py b/simple_picameraGUI.py index a44fbe2..746cfad 100644 --- a/simple_picameraGUI.py +++ b/simple_picameraGUI.py @@ -66,8 +66,7 @@ def _load_calib_data(self): with open(self.save_dir + "calib.json", "r") as f: self.bars_per_um_per_unit_zoom = json.load(f) print("calibration data loaded.") - except: - self.calib_data = self.bars_per_um_per_unit_zoom + except FileNotFoundError: messagebox.showwarning( "Calibration error", "Scalebar calibration data not found please recalibrate." ) @@ -78,6 +77,7 @@ def _load_calib_data(self): self.bars_per_um_per_unit_zoom = self.scalebar_len / ( self.physical_len.get() * int(self.lens_zoom.get()[:-1]) ) # physical len in um + self.calib_data = self.bars_per_um_per_unit_zoom def create_frames(self): self.window = Frame(self.master)