From ebd403fcf7c6e2cf44a00850a6f4f545fbc70ef8 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Thu, 11 Sep 2025 14:47:13 +0200 Subject: [PATCH] DAQ: Loop centre sequence - now using ml box to centre sample - takes centre of leftmost edge of either the face or the whole loop. Added print statements and more images saved to database. --- daq/src/aaredaq/daq.py | 43 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/daq/src/aaredaq/daq.py b/daq/src/aaredaq/daq.py index 38eb3d89..569a364c 100644 --- a/daq/src/aaredaq/daq.py +++ b/daq/src/aaredaq/daq.py @@ -4,6 +4,7 @@ from datetime import datetime from math import ceil from typing import List, Tuple import secrets +import os import cv2 import numpy as np @@ -705,6 +706,29 @@ class AareDAQ: omega_deg=geom.omega_deg ) + def __ml_loop_centre_box(self, sample_id: int | None = None, filename: str | None = None) -> SmargonCoordinate | None: + time.sleep(0.2) # Just to be sure image is stable + bgr_image = cv2.cvtColor(self.camera_image, cv2.COLOR_RGB2BGR) + box = self.__mlbox.predict(bgr_image, filename) + + if box is None: + if filename is not None: + #cv2.imwrite(f"{filename}_no_detection.jpg", bgr_image) + self.__aare.upload_image(sample_id, f"{filename}_no_detection", bgr_image) + return None + + x1, y1, x2, y2 = box + + if filename is not None: + #cv2.rectangle(bgr_image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2) + #cv2.imwrite(f"{filename}.jpg", bgr_image) + self.__aare.upload_image(sample_id, filename, bgr_image) + + geom = self.sample_geometry + centre_coord = y1 + (y2 - y1)/2 + coord = geom.picture_to_smargon(Coordinate(x=x1, y=centre_coord)) + return SmargonCoordinate(sh_mm=coord) + def ml_bounding_box(self, sample_id: int | None = None, filename: str | None = None) -> RasterGridRequest | None: try: self.__cfg.try_set_busy(timeout=360) @@ -811,16 +835,22 @@ class AareDAQ: i = 0 for s in self.__cfg.alc_zoom_settings.z: print('new loop center settings') - print(s.zoom_value,s.sam_cam_gain, s.sam_cam_exp) self.__devs.samcam_settings = SampleCameraSettings(exposure=s.sam_cam_exp, gain=s.sam_cam_gain) self.__devs.zoom_sync(s.zoom_value) - print(s.zoom_value, s.sam_cam_gain, s.sam_cam_exp) + print(f'zoom={s.zoom_value},gain={s.sam_cam_gain}, exp={s.sam_cam_exp}') + if sample_id is not None: + self.save_screenshot_db(sample_id, f"pre_alc") + print(sample_id) if i % 2 == 0: - angles = (0, -45, -90) + angles = (0, -45, -90, -135) else: - angles = (-90, -45, 0) + angles = (-135, -90, -45, 0) for angle in angles: - print(angle) + print(f"Moving to new omega: {angle}") + if sample_id is not None and i == 0: + exp = int(s.sam_cam_exp * 1000) + gain = int(s.sam_cam_gain) + self.save_screenshot_db(sample_id, f"pre_alc_{sample_id}_{angle}_{s.zoom_value:.0f}_{exp}_{gain}") self.__devs.aerotech.move(angle, wait=True) filename = None exp = int(s.sam_cam_exp * 1000) @@ -837,7 +867,8 @@ class AareDAQ: self.save_screenshot_db(sample_id, f"{sample_id}_{angle}_{s.zoom_value:.0f}_{exp}_{gain}") i += 1 return True - except LoopCenteringFailed: + except Exception as e: + print(f"Error in loop centering: {e}") return False def auto_loop_center(self, sample_id: int | None = None) -> float: