From 59ddc333c7d97db40534dedd734942338d2cd46b Mon Sep 17 00:00:00 2001 From: edo-neo Date: Tue, 19 Aug 2025 16:25:15 +0200 Subject: [PATCH] dev --- src/components/hikrobot_sc/hikrobot_sc.py | 37 +++++++++++++++++++---- src/components/vision.py | 10 +++++- src/main.py | 4 +++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/components/hikrobot_sc/hikrobot_sc.py b/src/components/hikrobot_sc/hikrobot_sc.py index 4db396e..c92580d 100644 --- a/src/components/hikrobot_sc/hikrobot_sc.py +++ b/src/components/hikrobot_sc/hikrobot_sc.py @@ -23,7 +23,7 @@ class HikrobotSmartCamera(Component): self.ok_frames = [] self.ok_results =[] - def config_changed(self): + def config_changed(self, vision_recipe=None): self.connected = False global MV_VS_OK device_info_list = MV_VS_DEVICE_INFO_LIST() @@ -85,8 +85,32 @@ class HikrobotSmartCamera(Component): return -2 # Set the camera recipe - if self.config.get("general", {}).get("solution_name", None) is not None: + # Load recipe from vision/recipes directory + if vision_recipe is not None: + # Use the vision_recipe parameter to load the recipe + from configparser import ConfigParser + from pathlib import Path + + recipes_dir = Path("./config/vision/recipes") + recipe_path = recipes_dir / f"{vision_recipe}.ini" + + if recipe_path.exists(): + config = ConfigParser(inline_comment_prefixes="#") + config.read(recipe_path) + if "general" in config and "solution_name" in config["general"]: + solutionName = config["general"]["solution_name"] + self.log.info(f"Loading solution: {solutionName} from {recipe_path}") + mv_lib.MV_VS_SetStringValue(pHandle, "SrcOperateSolutionName", solutionName) + mv_lib.MV_VS_SetCommandValue(pHandle, "CommandProjectLoad") + time.sleep(5) + else: + self.log.error(f"No solution_name found in {recipe_path}") + else: + self.log.error(f"Recipe file not found: {recipe_path}") + elif self.config.get("general", {}).get("solution_name", None) is not None: + # Fallback to the old method if vision_recipe is not provided solutionName = self.config["general"]["solution_name"] + self.log.info(f"Using fallback solution: {solutionName}") mv_lib.MV_VS_SetStringValue(pHandle, "SrcOperateSolutionName", solutionName) mv_lib.MV_VS_SetCommandValue(pHandle, "CommandProjectLoad") time.sleep(5) @@ -98,9 +122,9 @@ class HikrobotSmartCamera(Component): self.connected = True @Component.reconfig_on_error - def _get(self): + def _get(self, vision_recipe=None): if not self.connected: - self.config_changed() + self.config_changed(vision_recipe=vision_recipe) if not self.connected: return concat_frame = None @@ -136,6 +160,7 @@ class HikrobotSmartCamera(Component): frame = cv2.rotate(frame, cv2.ROTATE_180) else: self.connected = False + return if concat_frame is None: concat_frame = copy.deepcopy(frame) else: @@ -147,10 +172,10 @@ class HikrobotSmartCamera(Component): super()._get([concat_frame, self.ok_results]) - def resume(self): + def resume(self, vision_recipe=None): self.log.info(f"RESUMING") if not self.connected: - self.config_changed() + self.config_changed(vision_recipe=vision_recipe) if self.connected: # init OK result memory self.ok_frames = [None for n in range(self.num_cameras)] diff --git a/src/components/vision.py b/src/components/vision.py index 07bed8d..fbc0222 100644 --- a/src/components/vision.py +++ b/src/components/vision.py @@ -62,6 +62,7 @@ class Vision(Component): status_signal = pyqtSignal(object) loading_model_signal = pyqtSignal(object) + recipe_changed_signal = pyqtSignal(str) # Signal to notify when a recipe is changed def __init__(self, config=None, name=None, period=None, lazy=True, paused=False, threaded=True): super().__init__(config=config, name=name, period=period, lazy=lazy, paused=paused, threaded=threaded) @@ -178,12 +179,19 @@ class Vision(Component): read = config.read(self.recipe_path) if len(read) != 1 or self.recipe_path not in read: raise AssertionError("Recipe could not be read.") - os.path.splitext(os.path.basename(read[0]))[0] + recipe_name = os.path.splitext(os.path.basename(read[0]))[0] self.vision_config = config._sections.get("general", None) self.markers = self.parse_markers(config._sections.get("markers", None)) self.zones = self.parse_zones(config._sections.get("zones", None)) self.labels = self.parse_labels(config._sections.get("labels", None)) self.recipe_watcher.addPath(str(self.recipe_path)) + + # Emit a signal that can be connected to the HikrobotSmartCamera component + self.log.info(f"Recipe loaded: {recipe_name}") + + # Emit the recipe_changed_signal with the recipe name + # This signal can be connected to the HikrobotSmartCamera component in main.py + self.recipe_changed_signal.emit(recipe_name) except Exception: self.log.exception(traceback.format_exc()) self.log.exception(f"Error reading {self.recipe_path!r}:") diff --git a/src/main.py b/src/main.py index 7c9dc4b..9d0155a 100644 --- a/src/main.py +++ b/src/main.py @@ -178,6 +178,10 @@ try: if "vision" in self.components and "hikrobot_sc" in self.components: self.components["vision"].set_sources({"hikrobot_sc": self.components["hikrobot_sc"].out}) + # Connect the recipe_changed_signal from Vision to HikrobotSmartCamera + self.components["vision"].recipe_changed_signal.connect( + lambda recipe_name: self.components["hikrobot_sc"].config_changed(vision_recipe=recipe_name) + ) # connect tecna to screwdriver if "screwdriver" in self.components and "tecna_t3" in self.components: