目标检测功能 - 接收上传的图片文件
(
background_tasks: BackgroundTasks,
file: UploadFile = File(...),
ocr: bool = Query(False, description="是否启用OCR功能"),
det: bool = Query(True, description="是否启用目标检测功能"),
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="是否显示广告")
)
| 541 | # 目标检测端点 - 文件上传 |
| 542 | @app.post("/det/file", response_model=DetectionResponse) |
| 543 | async def object_detection_file( |
| 544 | background_tasks: BackgroundTasks, |
| 545 | file: UploadFile = File(...), |
| 546 | ocr: bool = Query(False, description="是否启用OCR功能"), |
| 547 | det: bool = Query(True, description="是否启用目标检测功能"), |
| 548 | use_gpu: bool = Query(default_use_gpu, description="是否使用GPU加速"), |
| 549 | device_id: int = Query(default_device_id, description="GPU设备ID"), |
| 550 | show_ad: bool = Query(default_show_ad, description="是否显示广告") |
| 551 | ): |
| 552 | """ |
| 553 | 目标检测功能 - 接收上传的图片文件 |
| 554 | """ |
| 555 | try: |
| 556 | contents = await file.read() |
| 557 | except Exception as exc: |
| 558 | raise HTTPException(status_code=400, detail="无法读取上传文件") from exc |
| 559 | |
| 560 | if not contents: |
| 561 | raise HTTPException(status_code=400, detail="上传文件为空") |
| 562 | if len(contents) > CORE_MAX_IMAGE_BYTES: |
| 563 | raise HTTPException( |
| 564 | status_code=400, |
| 565 | detail=f"图片大小超过 {CORE_MAX_IMAGE_BYTES // 1024}KB 限制" |
| 566 | ) |
| 567 | |
| 568 | config_key = f"ocr={ocr}-det={det}-gpu={use_gpu}-dev={device_id}" |
| 569 | |
| 570 | ocr_instance = get_ocr_instance( |
| 571 | config_key, ocr, det, False, False, use_gpu, device_id, show_ad, |
| 572 | default_import_onnx_path, default_charsets_path |
| 573 | ) |
| 574 | |
| 575 | start_time = time.time() |
| 576 | try: |
| 577 | result = ocr_instance.detection(img_bytes=contents) |
| 578 | except (DdddOcrInputError, InvalidImageError) as exc: |
| 579 | raise HTTPException(status_code=400, detail=str(exc)) from exc |
| 580 | except Exception as exc: |
| 581 | logger.error(f"目标检测文件识别失败: {str(exc)}") |
| 582 | raise HTTPException(status_code=500, detail=f"目标检测失败: {str(exc)}") from exc |
| 583 | |
| 584 | background_tasks.add_task(cleanup_inactive_instances) |
| 585 | |
| 586 | return { |
| 587 | "result": result, |
| 588 | "processing_time": time.time() - start_time |
| 589 | } |
| 590 | |
| 591 | # 滑块匹配端点 |
| 592 | @app.post("/slide_match", response_model=SlideMatchResponse) |
nothing calls this directly
no test coverage detected
searching dependent graphs…