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

Function ocr_recognition

ddddocr/api/app.py:336–404  ·  view source on GitHub ↗

OCR文字识别 - 接收Base64编码的图片

(
    request: OCRRequest,
    background_tasks: BackgroundTasks,
    ocr: bool = Query(default_ocr, description="是否启用OCR功能"),
    det: bool = Query(default_det, description="是否启用目标检测功能"),
    old: bool = Query(default_old, description="是否使用旧版OCR模型"),
    beta: bool = Query(default_beta, description="是否使用Beta版OCR模型"),
    use_gpu: bool = Query(default_use_gpu, description="是否使用GPU加速"),
    device_id: int = Query(default_device_id, description="GPU设备ID"),
    show_ad: bool = Query(default_show_ad, description="是否显示广告")
)

Source from the content-addressed store, hash-verified

334# OCR识别端点 - JSON请求
335@app.post("/ocr", response_model=OCRResponse)
336async def ocr_recognition(
337 request: OCRRequest,
338 background_tasks: BackgroundTasks,
339 ocr: bool = Query(default_ocr, description="是否启用OCR功能"),
340 det: bool = Query(default_det, description="是否启用目标检测功能"),
341 old: bool = Query(default_old, description="是否使用旧版OCR模型"),
342 beta: bool = Query(default_beta, description="是否使用Beta版OCR模型"),
343 use_gpu: bool = Query(default_use_gpu, description="是否使用GPU加速"),
344 device_id: int = Query(default_device_id, description="GPU设备ID"),
345 show_ad: bool = Query(default_show_ad, description="是否显示广告")
346):
347 """
348 OCR文字识别 - 接收Base64编码的图片
349 """
350 image = None
351 try:
352 img_data = _decode_base64_bytes(request.image)
353 image = Image.open(io.BytesIO(img_data))
354 except HTTPException:
355 raise
356 except Exception as exc:
357 logger.error(f"OCR请求图片解析失败: {str(exc)}")
358 raise HTTPException(status_code=400, detail="无法读取图片") from exc
359
360 config_key = f"ocr={ocr}-det={det}-old={old}-beta={beta}-gpu={use_gpu}-dev={device_id}"
361
362 ocr_instance = get_ocr_instance(
363 config_key, ocr, det, old, beta, use_gpu, device_id, show_ad,
364 default_import_onnx_path, default_charsets_path
365 )
366
367 start_time = time.time()
368 try:
369 if request.probability:
370 result = ocr_instance.classification(
371 image,
372 probability=True,
373 colors=request.colors,
374 custom_color_ranges=request.custom_color_ranges
375 )
376 response_data = {
377 "result": result,
378 "processing_time": time.time() - start_time
379 }
380 else:
381 result = ocr_instance.classification(
382 image,
383 colors=request.colors,
384 custom_color_ranges=request.custom_color_ranges
385 )
386 response_data = {
387 "result": result,
388 "processing_time": time.time() - start_time
389 }
390 except (DdddOcrInputError, InvalidImageError) as exc:
391 logger.warning(f"OCR识别参数错误: {str(exc)}")
392 raise HTTPException(status_code=400, detail=str(exc)) from exc
393 except Exception as exc:

Callers

nothing calls this directly

Calls 3

_decode_base64_bytesFunction · 0.85
get_ocr_instanceFunction · 0.85
classificationMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…