Skip to content

Commit

Permalink
Merge pull request #3 from panditsomnath10016git/calibration
Browse files Browse the repository at this point in the history
Calibration data save bug addressed
  • Loading branch information
panditsomnath10016git authored Dec 16, 2023
2 parents 1e231b9 + aba9f2e commit a17b297
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions simple_picameraGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand All @@ -43,22 +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)
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)

self._load_calib_data()
self._update_fixed_scalebar()
# self.bind("<Escape>", self._hide_input_window)

Expand All @@ -74,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 FileNotFoundError:
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
self.calib_data = self.bars_per_um_per_unit_zoom

def create_frames(self):
self.window = Frame(self.master)
self.screen_width, self.screen_height = (
Expand Down Expand Up @@ -211,17 +216,21 @@ 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)
print("calibration data saved!", self.calib_data)

self._update_fixed_scalebar()

def _update_fixed_scalebar(self):
Expand Down

0 comments on commit a17b297

Please sign in to comment.