| 76 | |
| 77 | |
| 78 | class OpenOCRE2E(object): |
| 79 | |
| 80 | def __init__(self, |
| 81 | mode='mobile', |
| 82 | backend='torch', |
| 83 | onnx_det_model_path=None, |
| 84 | onnx_rec_model_path=None, |
| 85 | drop_score=0.5, |
| 86 | det_box_type='quad', |
| 87 | use_gpu='auto'): |
| 88 | """ |
| 89 | 初始化函数,用于初始化OCR引擎的相关配置和组件。 |
| 90 | |
| 91 | Args: |
| 92 | mode (str, optional): 运行模式,可选值为'mobile'或'server'。默认为'mobile'。 |
| 93 | drop_score (float, optional): 检测框的置信度阈值,低于该阈值的检测框将被丢弃。默认为0.5。 |
| 94 | det_box_type (str, optional): 检测框的类型,可选值为'quad' and 'poly'。默认为'quad'。 |
| 95 | use_gpu (str, optional): GPU使用策略,可选值为'auto'/'true'/'false'。默认为'auto'。 |
| 96 | |
| 97 | Returns: |
| 98 | 无返回值。 |
| 99 | |
| 100 | """ |
| 101 | # Auto-switch backend for server mode |
| 102 | if mode == 'server' and backend != 'torch': |
| 103 | logger.warning( |
| 104 | f"Server mode only supports 'torch' backend, got '{backend}'. " |
| 105 | f"Automatically switching to 'torch' backend. " |
| 106 | f"Please make sure 'torch' and 'torchvision' are installed: " |
| 107 | f"pip install torch torchvision") |
| 108 | backend = 'torch' |
| 109 | |
| 110 | # Parse use_gpu parameter |
| 111 | if use_gpu == 'auto': |
| 112 | try: |
| 113 | import torch |
| 114 | device = 'gpu' if torch.cuda.is_available() else 'cpu' |
| 115 | except: |
| 116 | device = 'cpu' |
| 117 | elif use_gpu == 'true': |
| 118 | device = 'gpu' |
| 119 | elif use_gpu == 'false': |
| 120 | device = 'cpu' |
| 121 | else: |
| 122 | raise ValueError(f"use_gpu must be 'auto', 'true', or 'false', got '{use_gpu}'") |
| 123 | |
| 124 | cfg_det = Config(DEFAULT_CFG_PATH_DET).cfg # mobile model |
| 125 | cfg_det['Global']['device'] = device |
| 126 | if mode == 'server': |
| 127 | cfg_rec = Config(DEFAULT_CFG_PATH_REC_SERVER).cfg # server model |
| 128 | else: |
| 129 | cfg_rec = Config(DEFAULT_CFG_PATH_REC).cfg # mobile model |
| 130 | |
| 131 | cfg_rec['Global']['device'] = device |
| 132 | |
| 133 | self.text_detector = OpenDetector(cfg_det, |
| 134 | backend=backend, |
| 135 | onnx_model_path=onnx_det_model_path, |
no outgoing calls
no test coverage detected