OCR文字识别 - 接收上传的图片文件
(
background_tasks: BackgroundTasks,
file: UploadFile = File(...),
probability: Union[bool, str] = Form(False),
colors: str = Form("[]"),
custom_color_ranges: str = Form("null"),
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="是否显示广告")
)
| 407 | # OCR识别端点 - 文件上传 |
| 408 | @app.post("/ocr/file", response_model=OCRResponse) |
| 409 | async def ocr_recognition_file( |
| 410 | background_tasks: BackgroundTasks, |
| 411 | file: UploadFile = File(...), |
| 412 | probability: Union[bool, str] = Form(False), |
| 413 | colors: str = Form("[]"), |
| 414 | custom_color_ranges: str = Form("null"), |
| 415 | ocr: bool = Query(default_ocr, description="是否启用OCR功能"), |
| 416 | det: bool = Query(default_det, description="是否启用目标检测功能"), |
| 417 | old: bool = Query(default_old, description="是否使用旧版OCR模型"), |
| 418 | beta: bool = Query(default_beta, description="是否使用Beta版OCR模型"), |
| 419 | use_gpu: bool = Query(default_use_gpu, description="是否使用GPU加速"), |
| 420 | device_id: int = Query(default_device_id, description="GPU设备ID"), |
| 421 | show_ad: bool = Query(default_show_ad, description="是否显示广告") |
| 422 | ): |
| 423 | """ |
| 424 | OCR文字识别 - 接收上传的图片文件 |
| 425 | """ |
| 426 | image = None |
| 427 | try: |
| 428 | contents = await file.read() |
| 429 | except Exception as exc: |
| 430 | raise HTTPException(status_code=400, detail="无法读取上传文件") from exc |
| 431 | |
| 432 | if not contents: |
| 433 | raise HTTPException(status_code=400, detail="上传文件为空") |
| 434 | if len(contents) > CORE_MAX_IMAGE_BYTES: |
| 435 | raise HTTPException( |
| 436 | status_code=400, |
| 437 | detail=f"图片大小超过 {CORE_MAX_IMAGE_BYTES // 1024}KB 限制" |
| 438 | ) |
| 439 | try: |
| 440 | image = Image.open(io.BytesIO(contents)) |
| 441 | except Exception as exc: |
| 442 | raise HTTPException(status_code=400, detail="无法解析上传的图片") from exc |
| 443 | |
| 444 | try: |
| 445 | colors_data = json.loads(colors) if colors else [] |
| 446 | except json.JSONDecodeError as exc: |
| 447 | raise HTTPException(status_code=400, detail="colors JSON 解析失败") from exc |
| 448 | colors_list = _ensure_colors_list(colors_data) |
| 449 | custom_ranges = _ensure_custom_ranges(custom_color_ranges) |
| 450 | probability_flag = _coerce_bool_param(probability, 'probability') |
| 451 | |
| 452 | config_key = f"ocr={ocr}-det={det}-old={old}-beta={beta}-gpu={use_gpu}-dev={device_id}" |
| 453 | |
| 454 | ocr_instance = get_ocr_instance( |
| 455 | config_key, ocr, det, old, beta, use_gpu, device_id, show_ad, |
| 456 | default_import_onnx_path, default_charsets_path |
| 457 | ) |
| 458 | |
| 459 | start_time = time.time() |
| 460 | try: |
| 461 | if probability_flag: |
| 462 | result = ocr_instance.classification( |
| 463 | image, |
| 464 | probability=True, |
| 465 | colors=colors_list, |
| 466 | custom_color_ranges=custom_ranges |
nothing calls this directly
no test coverage detected
searching dependent graphs…