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

Function ocr_recognition

ddddocr/api/routes.py:80–118  ·  view source on GitHub ↗

执行OCR识别

(request: OCRRequest)

Source from the content-addressed store, hash-verified

78
79 @app.post("/ocr", response_model=APIResponse)
80 async def ocr_recognition(request: OCRRequest):
81 """执行OCR识别"""
82 try:
83 if not service.ocr_instance:
84 raise HTTPException(status_code=400, detail="OCR功能未初始化,请先调用 /initialize 接口")
85
86 if "ocr" not in service.enabled_features:
87 raise HTTPException(status_code=400, detail="OCR功能已禁用")
88
89 # 解码base64图片
90 try:
91 image_data = base64.b64decode(request.image)
92 except Exception:
93 raise HTTPException(status_code=400, detail="图片base64解码失败")
94
95 # 设置字符集范围
96 if request.charset_range is not None:
97 service.ocr_instance.set_ranges(request.charset_range)
98
99 # 执行OCR识别
100 result = service.ocr_instance.classification(
101 image_data,
102 png_fix=request.png_fix,
103 probability=request.probability,
104 color_filter_colors=request.color_filter_colors,
105 color_filter_custom_ranges=request.color_filter_custom_ranges
106 )
107
108 if request.probability:
109 response_data = OCRResponse(text=None, probability=result)
110 else:
111 response_data = OCRResponse(text=result, probability=None)
112
113 return APIResponse(success=True, message="OCR识别成功", data=response_data.dict())
114
115 except HTTPException:
116 raise
117 except Exception as e:
118 return APIResponse(success=False, message=f"OCR识别失败: {str(e)}")
119
120 @app.post("/detect", response_model=APIResponse)
121 async def object_detection(request: DetectionRequest):

Callers

nothing calls this directly

Calls 4

APIResponseClass · 0.85
classificationMethod · 0.80
OCRResponseClass · 0.70
set_rangesMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…