ocr.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """运输计划 - 人员证书 OCR"""
  2. from typing import Optional
  3. from fastapi import APIRouter, Depends, HTTPException
  4. from pydantic import BaseModel, ConfigDict, Field
  5. from sqlalchemy.orm import Session
  6. from app.core.database import get_db
  7. from app.core.db_auth import DbUserContext, get_db_current_user
  8. from app.services.pl_ocr_service import recognize_personnel_cert
  9. router = APIRouter(prefix="/ocr", tags=["运输计划-OCR"])
  10. class RecognizeCertBody(BaseModel):
  11. model_config = ConfigDict(populate_by_name=True)
  12. cert_name: str = Field(..., alias="certName")
  13. attachment_url: Optional[str] = Field(None, alias="attachmentUrl")
  14. attachment_urls: Optional[str] = Field(None, alias="attachmentUrls")
  15. class RecognizeCertResult(BaseModel):
  16. model_config = ConfigDict(populate_by_name=True, ser_json_by_alias=True)
  17. cert_no: Optional[str] = Field(None, alias="certNo")
  18. expire_date: Optional[str] = Field(None, alias="expireDate")
  19. expire_date_raw: Optional[str] = Field(None, alias="expireDateRaw")
  20. monthly_used: int = Field(..., alias="monthlyUsed")
  21. monthly_limit: int = Field(..., alias="monthlyLimit")
  22. @router.post("/recognize-cert")
  23. def recognize_cert(
  24. body: RecognizeCertBody,
  25. db: Session = Depends(get_db),
  26. user: DbUserContext = Depends(get_db_current_user),
  27. ) -> dict:
  28. _ = user
  29. urls = (body.attachment_urls or body.attachment_url or "").strip()
  30. if not urls:
  31. raise HTTPException(status_code=400, detail="请先上传证书附件")
  32. result = recognize_personnel_cert(
  33. db,
  34. cert_name=body.cert_name,
  35. attachment_urls=urls,
  36. )
  37. return {
  38. "certNo": result.get("certNo"),
  39. "expireDate": result.get("expireDate"),
  40. "expireDateRaw": result.get("expireDateRaw"),
  41. "monthlyUsed": result["monthlyUsed"],
  42. "monthlyLimit": result["monthlyLimit"],
  43. }