diff --git a/anylabeling/config.py b/anylabeling/config.py index 966bdb5c..464d37b7 100644 --- a/anylabeling/config.py +++ b/anylabeling/config.py @@ -1,12 +1,7 @@ import os.path as osp import shutil import yaml - -try: - import importlib.resources as pkg_resources -except ImportError: - # Try backported to PY<37 `importlib_resources`. - import importlib_resources as pkg_resources +import importlib.resources as pkg_resources from anylabeling import configs as anylabeling_configs from anylabeling.views.labeling.logger import logger diff --git a/anylabeling/services/auto_labeling/__base__/clip.py b/anylabeling/services/auto_labeling/__base__/clip.py index d1173637..c4445167 100644 --- a/anylabeling/services/auto_labeling/__base__/clip.py +++ b/anylabeling/services/auto_labeling/__base__/clip.py @@ -8,6 +8,7 @@ from functools import lru_cache from ..engines import OnnxBaseModel +from anylabeling.views.labeling.logger import logger _MODEL_INFO = { @@ -155,9 +156,13 @@ def tokenize(self, texts, context_length=52): @lru_cache() def default_vocab(): - current_dir = os.path.dirname(__file__) - return os.path.join(current_dir, "..", "configs", "clip_vocab.txt") - + import importlib.resources + from anylabeling.services.auto_labeling import configs + try: + with importlib.resources.path(configs.clip, 'clip_vocab.txt') as p: + return str(p) + except Exception as e: + logger.error(f"Error loading default vocab: {e}") def convert_to_unicode(text): """Converts `text` to Unicode (if it's not already), assuming utf-8 input.""" diff --git a/anylabeling/services/auto_labeling/__base__/ram.py b/anylabeling/services/auto_labeling/__base__/ram.py index 4c41aeb1..b98d050b 100644 --- a/anylabeling/services/auto_labeling/__base__/ram.py +++ b/anylabeling/services/auto_labeling/__base__/ram.py @@ -121,19 +121,15 @@ def predict_shapes(self, image, image_path=None): @staticmethod def load_tag_list(): - current_dir = os.path.dirname(__file__) - tag_list_file = os.path.join( - current_dir, "..", "configs", "ram_tag_list.txt" - ) - tag_list_chinese_file = os.path.join( - current_dir, "..", "configs", "ram_tag_list_chinese.txt" - ) + import importlib.resources as pkg_resources + from anylabeling.services.auto_labeling.configs import ram - with open(tag_list_file, "r", encoding="utf-8") as f: - tag_list = f.read().splitlines() + with pkg_resources.path(ram, 'ram_tag_list.txt') as p: + tag_list = p.read_text().splitlines() tag_list = np.array(tag_list) - with open(tag_list_chinese_file, "r", encoding="utf-8") as f: - tag_list_chinese = f.read().splitlines() + + with pkg_resources.path(ram, 'ram_tag_list_chinese.txt') as p: + tag_list_chinese = p.read_text().splitlines() tag_list_chinese = np.array(tag_list_chinese) return tag_list, tag_list_chinese diff --git a/anylabeling/services/auto_labeling/configs/__init__.py b/anylabeling/services/auto_labeling/configs/__init__.py new file mode 100644 index 00000000..a3f3c14b --- /dev/null +++ b/anylabeling/services/auto_labeling/configs/__init__.py @@ -0,0 +1,4 @@ +from . import bert +from . import clip +from . import ppocr +from . import ram diff --git a/anylabeling/services/auto_labeling/configs/bert/__init__.py b/anylabeling/services/auto_labeling/configs/bert/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/anylabeling/services/auto_labeling/configs/bert_base_uncased_tokenizer.json b/anylabeling/services/auto_labeling/configs/bert/bert_base_uncased_tokenizer.json similarity index 100% rename from anylabeling/services/auto_labeling/configs/bert_base_uncased_tokenizer.json rename to anylabeling/services/auto_labeling/configs/bert/bert_base_uncased_tokenizer.json diff --git a/anylabeling/services/auto_labeling/configs/clip/__init__.py b/anylabeling/services/auto_labeling/configs/clip/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/anylabeling/services/auto_labeling/configs/clip_vocab.txt b/anylabeling/services/auto_labeling/configs/clip/clip_vocab.txt similarity index 100% rename from anylabeling/services/auto_labeling/configs/clip_vocab.txt rename to anylabeling/services/auto_labeling/configs/clip/clip_vocab.txt diff --git a/anylabeling/services/auto_labeling/configs/ppocr/__init__.py b/anylabeling/services/auto_labeling/configs/ppocr/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/anylabeling/services/auto_labeling/configs/ram/__init__.py b/anylabeling/services/auto_labeling/configs/ram/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/anylabeling/services/auto_labeling/configs/ram_tag_list.txt b/anylabeling/services/auto_labeling/configs/ram/ram_tag_list.txt similarity index 100% rename from anylabeling/services/auto_labeling/configs/ram_tag_list.txt rename to anylabeling/services/auto_labeling/configs/ram/ram_tag_list.txt diff --git a/anylabeling/services/auto_labeling/configs/ram_tag_list_chinese.txt b/anylabeling/services/auto_labeling/configs/ram/ram_tag_list_chinese.txt similarity index 100% rename from anylabeling/services/auto_labeling/configs/ram_tag_list_chinese.txt rename to anylabeling/services/auto_labeling/configs/ram/ram_tag_list_chinese.txt diff --git a/anylabeling/services/auto_labeling/edge_sam.py b/anylabeling/services/auto_labeling/edge_sam.py index 3f383bee..98d57f5b 100644 --- a/anylabeling/services/auto_labeling/edge_sam.py +++ b/anylabeling/services/auto_labeling/edge_sam.py @@ -185,9 +185,6 @@ def post_process(self, masks, image=None): shape.add_point(QtCore.QPointF(point[0], point[1])) shape.shape_type = "polygon" shape.closed = True - shape.fill_color = "#000000" - shape.line_color = "#000000" - shape.line_width = 1 shape.label = "AUTOLABEL_OBJECT" shape.selected = False shapes.append(shape) @@ -222,9 +219,6 @@ def post_process(self, masks, image=None): "rectangle" if self.output_mode == "rectangle" else "rotation" ) shape.closed = True - shape.fill_color = "#000000" - shape.line_color = "#000000" - shape.line_width = 1 if self.clip_net is not None and self.classes: img = image[y_min:y_max, x_min:x_max] out = self.clip_net(img, self.classes) diff --git a/anylabeling/services/auto_labeling/grounding_dino.py b/anylabeling/services/auto_labeling/grounding_dino.py index df4432e7..f9302b31 100644 --- a/anylabeling/services/auto_labeling/grounding_dino.py +++ b/anylabeling/services/auto_labeling/grounding_dino.py @@ -357,11 +357,16 @@ def get_caption(text_prompt): @staticmethod def get_tokenlizer(text_encoder_type): - current_dir = os.path.dirname(__file__) + import importlib.resources + from anylabeling.services.auto_labeling import configs cfg_name = text_encoder_type.replace("-", "_") + "_tokenizer.json" - cfg_file = os.path.join(current_dir, "configs", cfg_name) - tokenizer = Tokenizer.from_file(cfg_file) - return tokenizer + try: + with importlib.resources.path(configs.bert, cfg_name) as p: + tokenizer = Tokenizer.from_file(str(p)) + return tokenizer + except Exception as e: + logger.error(f"Error loading tokenizer: {e}") + return None @staticmethod def get_phrases_from_posmap( diff --git a/anylabeling/services/auto_labeling/grounding_sam.py b/anylabeling/services/auto_labeling/grounding_sam.py index 50452784..79f2291e 100644 --- a/anylabeling/services/auto_labeling/grounding_sam.py +++ b/anylabeling/services/auto_labeling/grounding_sam.py @@ -771,11 +771,16 @@ def get_caption(text_prompt): @staticmethod def get_tokenlizer(text_encoder_type): - current_dir = os.path.dirname(__file__) + import importlib.resources + from anylabeling.services.auto_labeling import configs cfg_name = text_encoder_type.replace("-", "_") + "_tokenizer.json" - cfg_file = os.path.join(current_dir, "configs", cfg_name) - tokenizer = Tokenizer.from_file(cfg_file) - return tokenizer + try: + with importlib.resources.path(configs.bert, cfg_name) as p: + tokenizer = Tokenizer.from_file(str(p)) + return tokenizer + except Exception as e: + logger.error(f"Error loading tokenizer: {e}") + return None @staticmethod def get_phrases_from_posmap( diff --git a/anylabeling/services/auto_labeling/grounding_sam2.py b/anylabeling/services/auto_labeling/grounding_sam2.py index a4072174..d4e9f4f8 100644 --- a/anylabeling/services/auto_labeling/grounding_sam2.py +++ b/anylabeling/services/auto_labeling/grounding_sam2.py @@ -551,11 +551,16 @@ def get_caption(text_prompt): @staticmethod def get_tokenlizer(text_encoder_type): - current_dir = os.path.dirname(__file__) + import importlib.resources + from anylabeling.services.auto_labeling import configs cfg_name = text_encoder_type.replace("-", "_") + "_tokenizer.json" - cfg_file = os.path.join(current_dir, "configs", cfg_name) - tokenizer = Tokenizer.from_file(cfg_file) - return tokenizer + try: + with importlib.resources.path(configs.bert, cfg_name) as p: + tokenizer = Tokenizer.from_file(str(p)) + return tokenizer + except Exception as e: + logger.error(f"Error loading tokenizer: {e}") + return None @staticmethod def get_phrases_from_posmap( diff --git a/anylabeling/services/auto_labeling/yolov5_ram.py b/anylabeling/services/auto_labeling/yolov5_ram.py index d860265d..617b8b88 100755 --- a/anylabeling/services/auto_labeling/yolov5_ram.py +++ b/anylabeling/services/auto_labeling/yolov5_ram.py @@ -177,20 +177,18 @@ def predict_shapes(self, image, image_path=None): @staticmethod def load_tag_list(): - current_dir = os.path.dirname(__file__) - tag_list_file = os.path.join( - current_dir, "configs", "ram_tag_list.txt" - ) - tag_list_chinese_file = os.path.join( - current_dir, "configs", "ram_tag_list_chinese.txt" - ) + import importlib.resources as pkg_resources + from anylabeling.services.auto_labeling import configs - with open(tag_list_file, "r", encoding="utf-8") as f: - tag_list = f.read().splitlines() - tag_list = np.array(tag_list) - with open(tag_list_chinese_file, "r", encoding="utf-8") as f: - tag_list_chinese = f.read().splitlines() - tag_list_chinese = np.array(tag_list_chinese) + try: + tag_list = pkg_resources.read_text(configs.ram, 'ram_tag_list.txt').splitlines() + tag_list = np.array(tag_list) + + tag_list_chinese = pkg_resources.read_text(configs.ram, 'ram_tag_list_chinese.txt').splitlines() + tag_list_chinese = np.array(tag_list_chinese) + except Exception as e: + logger.error(f"Error loading tag list: {e}") + return np.array([]), np.array([]) return tag_list, tag_list_chinese diff --git a/x-anylabeling-linux-cpu.spec b/x-anylabeling-linux-cpu.spec index 7d6f3a6b..ce89a212 100644 --- a/x-anylabeling-linux-cpu.spec +++ b/x-anylabeling-linux-cpu.spec @@ -11,8 +11,10 @@ a = Analysis( ('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'), ('anylabeling/configs/*.yaml', 'anylabeling/configs'), ('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'), - ('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'), - ('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs') + ('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'), + ('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'), + ('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'), + ('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram') ], hiddenimports=[], hookspath=[], diff --git a/x-anylabeling-linux-gpu.spec b/x-anylabeling-linux-gpu.spec index 74bf525c..c1d87b6a 100644 --- a/x-anylabeling-linux-gpu.spec +++ b/x-anylabeling-linux-gpu.spec @@ -11,8 +11,10 @@ a = Analysis( ('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'), ('anylabeling/configs/*.yaml', 'anylabeling/configs'), ('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'), - ('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'), - ('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs'), + ('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'), + ('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'), + ('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'), + ('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram'), ('/home/cvhub/miniconda3/envs/x-anylabeling-gpu/lib/python3.8/site-packages/onnxruntime/capi/libonnxruntime_providers_cuda.so', 'onnxruntime/capi'), ('/home/cvhub/miniconda3/envs/x-anylabeling-gpu/lib/python3.8/site-packages/onnxruntime/capi/libonnxruntime_providers_shared.so', 'onnxruntime/capi'), ], diff --git a/x-anylabeling-macos.spec b/x-anylabeling-macos.spec index dccc9fe9..daecafcf 100644 --- a/x-anylabeling-macos.spec +++ b/x-anylabeling-macos.spec @@ -9,8 +9,10 @@ a = Analysis( ('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'), ('anylabeling/configs/*.yaml', 'anylabeling/configs'), ('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'), - ('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'), - ('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs') + ('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'), + ('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'), + ('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'), + ('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram') ], hiddenimports=[], hookspath=[], diff --git a/x-anylabeling-win-cpu.spec b/x-anylabeling-win-cpu.spec index 3af130fa..b68dbf53 100644 --- a/x-anylabeling-win-cpu.spec +++ b/x-anylabeling-win-cpu.spec @@ -13,8 +13,10 @@ a = Analysis( ('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'), ('anylabeling/configs/*.yaml', 'anylabeling/configs'), ('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'), - ('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'), - ('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs') + ('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'), + ('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'), + ('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'), + ('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram') ], hiddenimports=[], hookspath=[], diff --git a/x-anylabeling-win-gpu.spec b/x-anylabeling-win-gpu.spec index e55da32a..a7feaf27 100644 --- a/x-anylabeling-win-gpu.spec +++ b/x-anylabeling-win-gpu.spec @@ -13,8 +13,10 @@ a = Analysis( ('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'), ('anylabeling/configs/*.yaml', 'anylabeling/configs'), ('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'), - ('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'), - ('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs'), + ('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'), + ('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'), + ('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'), + ('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram'), ('C:/Users/18102/.conda/envs/x-anylabeling-gpu/Lib/site-packages/onnxruntime/capi/onnxruntime_providers_cuda.dll', 'onnxruntime/capi'), ('C:/Users/18102/.conda/envs/x-anylabeling-gpu/Lib/site-packages/onnxruntime/capi/onnxruntime_providers_shared.dll', 'onnxruntime/capi') ],