| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- 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
|