From 21479713e87f33bac57c74deaa6d7bab55a8af12 Mon Sep 17 00:00:00 2001 From: ariskadatabisnis Date: Tue, 17 Nov 2026 10:59:27 +0700 Subject: [PATCH] add-obb --- main-obb.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/main-obb.py b/main-obb.py index c9dd6a6..e93c7ab 100644 --- a/main-obb.py +++ b/main-obb.py @@ -13,16 +13,15 @@ from openvino import Layout, Type MODEL_XML = os.getenv("MODEL_XML", "/models/best.xml") MODEL_BIN = os.getenv("MODEL_BIN", "/models/best.bin") +CLASSES_FILE = os.getenv("CLASSES_FILE", "/opt/nuclio/classes.json") -classes_file = os.getenv("CLASSES_FILE", "/opt/nuclio/classes.json") -with open(classes_file, 'r') as f: - classes_data = json.load(f) - coconame = [cls['name'] for cls in sorted(classes_data, key=lambda x: x['id'])] +# Will be initialized in init_context +coconame = [] class Yolov9OBB: def __init__( - self, xml_model_path=MODEL_XML, bin_model_path=MODEL_BIN, conf=0.1, nms=0.4 + self, xml_model_path=MODEL_XML, bin_model_path=MODEL_BIN, conf=0.1, nms=0.4, class_names=None ): core = ov.Core() if bin_model_path: @@ -52,7 +51,7 @@ class Yolov9OBB: self.colors = [] np.random.seed(42) - for i in range(len(coconame)): + for i in range(len(class_names)): color = tuple(np.random.randint(100, 256, size=3)) self.colors.append(color) @@ -177,8 +176,24 @@ class Yolov9OBB: def init_context(context): + """Nuclio init_context – called once per container. + + Loads the IR model and compiles it for the CPU. + """ + global coconame context.logger.info("Init context OBB ----> 0%") - model = Yolov9OBB(MODEL_XML, MODEL_BIN, conf=0.1, nms=0.4) + + # Load class names + try: + with open(CLASSES_FILE, 'r') as f: + classes_data = json.load(f) + coconame = [cls['name'] for cls in sorted(classes_data, key=lambda x: x['id'])] + context.logger.info(f"Loaded {len(coconame)} classes") + except Exception as exc: + context.logger.error(f"Failed to load classes from {CLASSES_FILE}: {exc}") + coconame = [] + + model = Yolov9OBB(MODEL_XML, MODEL_BIN, conf=0.1, nms=0.4, class_names=coconame) context.user_data.model = model context.logger.info("Init context OBB ----> 100%")