2025-12-29 17:34:58 +08:00
|
|
|
"""Image OCR endpoint."""
|
|
|
|
|
|
2026-02-07 09:26:45 +08:00
|
|
|
import time
|
|
|
|
|
import uuid
|
2025-12-29 17:34:58 +08:00
|
|
|
|
2026-02-07 09:26:45 +08:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
|
|
|
|
|
|
|
|
|
from app.core.dependencies import (
|
|
|
|
|
get_image_processor,
|
|
|
|
|
get_layout_detector,
|
|
|
|
|
get_ocr_service,
|
|
|
|
|
get_mineru_ocr_service,
|
|
|
|
|
get_glmocr_service,
|
|
|
|
|
)
|
|
|
|
|
from app.core.logging_config import get_logger, RequestIDAdapter
|
2026-02-04 12:35:14 +08:00
|
|
|
from app.schemas.image import ImageOCRRequest, ImageOCRResponse
|
2025-12-29 17:34:58 +08:00
|
|
|
from app.services.image_processor import ImageProcessor
|
|
|
|
|
from app.services.layout_detector import LayoutDetector
|
2026-02-06 15:06:50 +08:00
|
|
|
from app.services.ocr_service import OCRService, MineruOCRService, GLMOCRService
|
2025-12-29 17:34:58 +08:00
|
|
|
|
|
|
|
|
router = APIRouter()
|
2026-02-07 09:26:45 +08:00
|
|
|
logger = get_logger()
|
2025-12-29 17:34:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/ocr", response_model=ImageOCRResponse)
|
|
|
|
|
async def process_image_ocr(
|
|
|
|
|
request: ImageOCRRequest,
|
2026-02-07 09:26:45 +08:00
|
|
|
http_request: Request,
|
|
|
|
|
response: Response,
|
2025-12-29 17:34:58 +08:00
|
|
|
image_processor: ImageProcessor = Depends(get_image_processor),
|
|
|
|
|
layout_detector: LayoutDetector = Depends(get_layout_detector),
|
2026-01-05 17:30:54 +08:00
|
|
|
mineru_service: MineruOCRService = Depends(get_mineru_ocr_service),
|
|
|
|
|
paddle_service: OCRService = Depends(get_ocr_service),
|
2026-02-06 15:06:50 +08:00
|
|
|
glmocr_service: GLMOCRService = Depends(get_glmocr_service),
|
2025-12-29 17:34:58 +08:00
|
|
|
) -> ImageOCRResponse:
|
|
|
|
|
"""Process an image and extract content as LaTeX, Markdown, and MathML.
|
|
|
|
|
|
|
|
|
|
The processing pipeline:
|
|
|
|
|
1. Load and preprocess image (add 30% whitespace padding)
|
|
|
|
|
2. Detect layout using DocLayout-YOLO
|
|
|
|
|
3. Based on layout:
|
|
|
|
|
- If plain text exists: use PP-DocLayoutV2 for mixed recognition
|
|
|
|
|
- Otherwise: use PaddleOCR-VL with formula prompt
|
|
|
|
|
4. Convert output to LaTeX, Markdown, and MathML formats
|
2026-02-04 12:00:06 +08:00
|
|
|
|
|
|
|
|
Note: OMML conversion is not included due to performance overhead.
|
2026-02-04 12:35:14 +08:00
|
|
|
Use the /convert/latex-to-omml endpoint to convert LaTeX to OMML separately.
|
2025-12-29 17:34:58 +08:00
|
|
|
"""
|
2026-02-07 09:26:45 +08:00
|
|
|
# Get or generate request ID
|
|
|
|
|
request_id = http_request.headers.get("x-request-id", str(uuid.uuid4()))
|
|
|
|
|
response.headers["x-request-id"] = request_id
|
|
|
|
|
|
|
|
|
|
# Create logger adapter with request_id
|
|
|
|
|
log = RequestIDAdapter(logger, {"request_id": request_id})
|
|
|
|
|
log.request_id = request_id
|
2025-12-29 17:34:58 +08:00
|
|
|
|
|
|
|
|
try:
|
2026-02-07 09:26:45 +08:00
|
|
|
log.info("Starting image OCR processing")
|
|
|
|
|
|
|
|
|
|
# Preprocess image
|
|
|
|
|
preprocess_start = time.time()
|
2026-02-06 15:06:50 +08:00
|
|
|
image = image_processor.preprocess(
|
|
|
|
|
image_url=request.image_url,
|
|
|
|
|
image_base64=request.image_base64,
|
|
|
|
|
)
|
2026-02-07 09:26:45 +08:00
|
|
|
preprocess_time = time.time() - preprocess_start
|
|
|
|
|
log.debug(f"Image preprocessing completed in {preprocess_time:.3f}s")
|
|
|
|
|
|
|
|
|
|
# Layout detection
|
|
|
|
|
layout_start = time.time()
|
2026-02-06 15:06:50 +08:00
|
|
|
layout_info = layout_detector.detect(image)
|
2026-02-07 09:26:45 +08:00
|
|
|
layout_time = time.time() - layout_start
|
|
|
|
|
log.info(f"Layout detection completed in {layout_time:.3f}s")
|
|
|
|
|
|
|
|
|
|
# OCR recognition
|
|
|
|
|
ocr_start = time.time()
|
2026-02-06 15:06:50 +08:00
|
|
|
if layout_info.MixedRecognition:
|
2026-02-07 09:26:45 +08:00
|
|
|
recognition_method = "MixedRecognition (MinerU)"
|
|
|
|
|
log.info(f"Using {recognition_method}")
|
2026-01-05 17:30:54 +08:00
|
|
|
ocr_result = mineru_service.recognize(image)
|
|
|
|
|
else:
|
2026-02-07 09:26:45 +08:00
|
|
|
recognition_method = "FormulaOnly (GLMOCR)"
|
|
|
|
|
log.info(f"Using {recognition_method}")
|
2026-02-06 15:06:50 +08:00
|
|
|
ocr_result = glmocr_service.recognize(image)
|
2026-02-07 09:26:45 +08:00
|
|
|
ocr_time = time.time() - ocr_start
|
|
|
|
|
|
|
|
|
|
total_time = time.time() - preprocess_start
|
|
|
|
|
log.info(f"OCR processing completed - Method: {recognition_method}, " f"Layout time: {layout_time:.3f}s, OCR time: {ocr_time:.3f}s, " f"Total time: {total_time:.3f}s")
|
|
|
|
|
|
2025-12-29 17:34:58 +08:00
|
|
|
except RuntimeError as e:
|
2026-02-07 09:26:45 +08:00
|
|
|
log.error(f"OCR processing failed: {str(e)}", exc_info=True)
|
2025-12-29 17:34:58 +08:00
|
|
|
raise HTTPException(status_code=503, detail=str(e))
|
2026-02-07 09:26:45 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
log.error(f"Unexpected error during OCR processing: {str(e)}", exc_info=True)
|
|
|
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
2025-12-29 17:34:58 +08:00
|
|
|
|
|
|
|
|
return ImageOCRResponse(
|
|
|
|
|
latex=ocr_result.get("latex", ""),
|
|
|
|
|
markdown=ocr_result.get("markdown", ""),
|
|
|
|
|
mathml=ocr_result.get("mathml", ""),
|
2026-02-04 12:00:06 +08:00
|
|
|
mml=ocr_result.get("mml", ""),
|
2025-12-29 17:34:58 +08:00
|
|
|
)
|