# !/usr/bin/env python3 # -*- coding: UTF-8 -*- ################################################################################ # # Copyright (c) 2024 Baidu.com, Inc. All Rights Reserved # ################################################################################ """ Author: PaddlePaddle Authors """ import os import yaml def load_config(file_path): """ load_config """ # Refer to https://github.com/PaddlePaddle/PaddleOCR/blob/366ad29d6c202a79bad103c72556c1186915c9c8/tools/program.py#L75 _, ext = os.path.splitext(file_path) assert ext in ['.yml', '.yaml'], "only support yaml files for now" config = yaml.load(open(file_path, 'rb'), Loader=yaml.Loader) return config def merge_config(config, opts): """ merge_config """ # Refer to https://github.com/PaddlePaddle/PaddleOCR/blob/366ad29d6c202a79bad103c72556c1186915c9c8/tools/program.py#L88 for key, value in opts.items(): if "." not in key: if isinstance(value, dict) and key in config: config[key].update(value) else: config[key] = value else: sub_keys = key.split('.') assert ( sub_keys[0] in config ), "the sub_keys can only be one of global_config: {}, but get: " \ "{}, please check your running command".format( config.keys(), sub_keys[0]) cur = config[sub_keys[0]] for idx, sub_key in enumerate(sub_keys[1:]): if idx == len(sub_keys) - 2: cur[sub_key] = value else: cur = cur[sub_key] return config