| 151 | |
| 152 | |
| 153 | class OpenDetector(object): |
| 154 | |
| 155 | def __init__(self, |
| 156 | config=None, |
| 157 | backend='torch', |
| 158 | onnx_model_path=None, |
| 159 | use_gpu='auto', |
| 160 | numId=0): |
| 161 | """ |
| 162 | Args: |
| 163 | config (dict, optional): 配置信息。默认为None。 |
| 164 | backend (str): 'torch' 或 'onnx' |
| 165 | onnx_model_path (str): ONNX模型路径(仅当backend='onnx'时需要) |
| 166 | use_gpu (str, optional): GPU使用策略,可选值为'auto'/'true'/'false'。默认为'auto'。 |
| 167 | numId (int, optional): 设备编号。默认为0。 |
| 168 | """ |
| 169 | |
| 170 | if config is None: |
| 171 | config = Config(DEFAULT_CFG_PATH_DET).cfg |
| 172 | |
| 173 | # Parse use_gpu parameter |
| 174 | if use_gpu == 'auto': |
| 175 | try: |
| 176 | import torch |
| 177 | device = 'gpu' if torch.cuda.is_available() else 'cpu' |
| 178 | except: |
| 179 | device = 'cpu' |
| 180 | elif use_gpu == 'true': |
| 181 | device = 'gpu' |
| 182 | elif use_gpu == 'false': |
| 183 | device = 'cpu' |
| 184 | else: |
| 185 | raise ValueError(f"use_gpu must be 'auto', 'true', or 'false', got '{use_gpu}'") |
| 186 | |
| 187 | config['Global']['device'] = device |
| 188 | |
| 189 | self._init_common(config) |
| 190 | backend = backend if config['Global'].get( |
| 191 | 'backend', None) is None else config['Global']['backend'] |
| 192 | self.backend = backend |
| 193 | if backend == 'torch': |
| 194 | import torch |
| 195 | self.torch = torch |
| 196 | if config['Architecture']['algorithm'] == 'DB_mobile': |
| 197 | if not os.path.exists(config['Global']['pretrained_model']): |
| 198 | config['Global'][ |
| 199 | 'pretrained_model'] = check_and_download_model( |
| 200 | MODEL_NAME_DET, DOWNLOAD_URL_DET) |
| 201 | self._init_torch_model(config, numId) |
| 202 | elif backend == 'onnx': |
| 203 | from tools.infer.onnx_engine import ONNXEngine |
| 204 | onnx_model_path = onnx_model_path if config['Global'].get( |
| 205 | 'onnx_model_path', |
| 206 | None) is None else config['Global']['onnx_model_path'] |
| 207 | if onnx_model_path is None: |
| 208 | if config['Architecture']['algorithm'] == 'DB_mobile': |
| 209 | onnx_model_path = check_and_download_model( |
| 210 | MODEL_NAME_DET_ONNX, DOWNLOAD_URL_DET_ONNX) |
no outgoing calls
no test coverage detected