diff --git a/daq/src/aaredaq/daq.py b/daq/src/aaredaq/daq.py index 6e1b5488..5b2b77d9 100644 --- a/daq/src/aaredaq/daq.py +++ b/daq/src/aaredaq/daq.py @@ -34,7 +34,7 @@ from aaredaqlib.models import ( PuckLoadedInfo, SampleShortInfoList, DAQStatusModel, BeamlineStatus, SessionStatus, SampleCameraSettings, AutofocusSettings, BoundingBoxModel, - ZoomModeEnum, CrystalSize, SimpleScanParameters) + ZoomModeEnum, CrystalSize, SimpleScanParameters, MLBoxModel) from aaredaqlib.raster_grid import RasterGridRequest, CompletedRasterGrid, CompletedRasterGridElem from aaredaqlib.rotation_scan import RotationScanRequest, CompletedRotationScan from aaredaqlib.sample_geometry import SampleGeometryModel @@ -825,7 +825,6 @@ class AareDAQ: if boxes 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, None, None @@ -897,60 +896,6 @@ class AareDAQ: self.__cfg.state_busy = False raise - def __loop_center(self, s, filename: str | None = "", iteration: int = 0) -> SmargonCoordinate: - # TODO: tidy up! - curr_image = self.camera_image - - gray_without_feature = cv2.cvtColor( - self.__cfg.get_alc_bkg(self.zoom, s.exposure, s.gain), cv2.COLOR_RGB2GRAY - ) - gray_with_feature = cv2.cvtColor(curr_image, cv2.COLOR_RGB2GRAY) - - # Subtract the "without feature" image from the "with feature" image - diff_image = cv2.absdiff(gray_with_feature, gray_without_feature) - - thresh_value, thresh = cv2.threshold(diff_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) - - print(f'Thresh value: {thresh_value}') - if filename is not None: - #cv2.imwrite(f"{filename}_curr_image_colour.jpg", curr_image) - cv2.imwrite(f"curr_image.jpg", gray_with_feature) - cv2.imwrite(f"diff.jpg", diff_image) - cv2.imwrite(f"thresh.jpg", thresh) - #cv2.imwrite(f"{filename}_adaptive.tiff", adapt_thresh) - - # # Find contours of the detected feature - contours, _ = cv2.findContours( - thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE - ) - - leftmost_point = None - - # Loop through each contour - for contour in contours: - # Loop through each point in the current contour - for point in contour: - x, y = point[0] # Point is a nested array [ [x, y] ] - - # Check if this is the leftmost point - if leftmost_point is None or x < leftmost_point[0]: - leftmost_point = (x, y) - - if leftmost_point is None: - raise LoopCenteringFailed() - - geom = self.sample_geometry - if iteration == 0: - coord = geom.picture_to_smargon( - Coordinate(x=leftmost_point[0], y=leftmost_point[1]) - ) - else: - #TODO only move x after first iteration - coord = geom.picture_to_smargon( - Coordinate(x=leftmost_point[0], y=leftmost_point[1]) - ) - return SmargonCoordinate(sh_mm=coord) - def face_detection(self, steps: int = 14, step_size: int = 15) -> dict: self.__cfg.try_set_busy(timeout=360) try: @@ -966,6 +911,25 @@ class AareDAQ: self.__cfg.state_busy = False return result + def face_detection_centre_correction(self, m:MLBoxModel, tolerance: float = 0.2): + geom = self.sample_geometry + beam_y = geom.beam_location_pxl.y + beam_x = geom.beam_location_pxl.x + + x1 = m.box.top_x + y1 = m.box.top_y + y2 = m.box.bottom_y + + centre_y = y1 + (y2 - y1) / 2 + centre_x = x1 + + if beam_y !=0 and abs(centre_y-beam_y)/abs(beam_y) > tolerance: + coord = geom.picture_to_smargon(Coordinate(x=beam_x, y=centre_y)) + self.__devs.smargon.target = SmargonCoordinate(sh_mm=coord) + self.__devs.smargon.wait(60) + + return + def __face_detection_sequence(self, steps: int = 14, step_size: int = 15) -> dict: self.__set_state(BeamlineStateEnum.SampleAlignment) self.__devs.lamp_light = 2.5 @@ -1004,6 +968,8 @@ class AareDAQ: cls_id = int(m.cls.value) x1, y1, x2, y2 = m.box.top_x, m.box.top_y, m.box.bottom_x, m.box.bottom_y + self.face_detection_centre_correction(m, tolerance=0.2) + if cls_id == 3: boxes[angle] = (x1, y1, x2, y2) logger.info(f"accepted box at angle {angle}, cls={cls_id}, box={(x1, y1, x2, y2)}")