MCPcopy
hub / github.com/sml2h3/ddddocr / get_ocr_instance

Function get_ocr_instance

ddddocr/api/app.py:239–281  ·  view source on GitHub ↗

获取或创建OCR实例

(
    config_key: str,
    ocr: bool = True,
    det: bool = False,
    old: bool = False,
    beta: bool = False,
    use_gpu: bool = False,
    device_id: int = 0,
    show_ad: bool = True,
    import_onnx_path: str = "",
    charsets_path: str = ""
)

Source from the content-addressed store, hash-verified

237
238# 函数:获取OCR实例
239def get_ocr_instance(
240 config_key: str,
241 ocr: bool = True,
242 det: bool = False,
243 old: bool = False,
244 beta: bool = False,
245 use_gpu: bool = False,
246 device_id: int = 0,
247 show_ad: bool = True,
248 import_onnx_path: str = "",
249 charsets_path: str = ""
250):
251 """
252 获取或创建OCR实例
253 """
254 if config_key in ocr_instances:
255 ocr_instances[config_key]["last_used"] = time.time()
256 return ocr_instances[config_key]["instance"]
257
258 logger.info(f"创建新的OCR实例,配置: {config_key}")
259 try:
260 instance = DdddOcr(
261 ocr=ocr,
262 det=det,
263 old=old,
264 beta=beta,
265 use_gpu=use_gpu,
266 device_id=device_id,
267 show_ad=show_ad,
268 import_onnx_path=import_onnx_path,
269 charsets_path=charsets_path
270 )
271 ocr_instances[config_key] = {
272 "instance": instance,
273 "last_used": time.time()
274 }
275 return instance
276 except (DdddOcrInputError, InvalidImageError) as e:
277 logger.error(f"创建OCR实例失败: {str(e)}")
278 raise HTTPException(status_code=400, detail=str(e)) from e
279 except Exception as e:
280 logger.error(f"创建OCR实例失败: {str(e)}")
281 raise HTTPException(status_code=500, detail=f"初始化OCR失败: {str(e)}") from e
282
283# 清理不活跃的OCR实例
284def cleanup_inactive_instances(max_idle_time: int = 3600):

Callers 7

ocr_recognitionFunction · 0.85
ocr_recognition_fileFunction · 0.85
object_detectionFunction · 0.85
object_detection_fileFunction · 0.85
slide_match_recognitionFunction · 0.85
set_charset_rangeFunction · 0.85

Calls 1

DdddOcrClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…