Files
motovaultpro/ocr/app/models/schemas.py
Eric Gullickson 54cbd49171
All checks were successful
Deploy to Staging / Build Images (pull_request) Successful in 31s
Deploy to Staging / Deploy to Staging (pull_request) Successful in 31s
Deploy to Staging / Verify Staging (pull_request) Successful in 2m19s
Deploy to Staging / Notify Staging Ready (pull_request) Successful in 8s
Deploy to Staging / Notify Staging Failure (pull_request) Has been skipped
feat: add VIN photo OCR pipeline (refs #67)
Implement VIN-specific OCR extraction with optimized preprocessing:

- Add POST /extract/vin endpoint for VIN extraction
- VIN preprocessor: CLAHE, deskew, denoise, adaptive threshold
- VIN validator: check digit validation, OCR error correction (I->1, O->0)
- VIN extractor: PSM modes 6/7/8, character whitelist, alternatives
- Response includes confidence, bounding box, and alternatives
- Unit tests for validator and preprocessor
- Integration tests for VIN extraction endpoint

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 19:31:36 -06:00

96 lines
2.3 KiB
Python

"""Pydantic models for OCR API request/response validation."""
from enum import Enum
from typing import Optional
from pydantic import BaseModel, Field
class DocumentType(str, Enum):
"""Types of documents that can be processed."""
VIN = "vin"
RECEIPT = "receipt"
MANUAL = "manual"
UNKNOWN = "unknown"
class ExtractedField(BaseModel):
"""A single extracted field with confidence score."""
value: str
confidence: float = Field(ge=0.0, le=1.0)
class BoundingBox(BaseModel):
"""Bounding box for detected region."""
x: int
y: int
width: int
height: int
class VinAlternative(BaseModel):
"""Alternative VIN candidate."""
vin: str
confidence: float = Field(ge=0.0, le=1.0)
class VinExtractionResponse(BaseModel):
"""Response from VIN extraction endpoint."""
success: bool
vin: Optional[str] = None
confidence: float = Field(ge=0.0, le=1.0)
bounding_box: Optional[BoundingBox] = Field(default=None, alias="boundingBox")
alternatives: list[VinAlternative] = Field(default_factory=list)
processing_time_ms: int = Field(alias="processingTimeMs")
error: Optional[str] = None
model_config = {"populate_by_name": True}
class OcrResponse(BaseModel):
"""Response from OCR extraction."""
success: bool
document_type: DocumentType = Field(alias="documentType")
raw_text: str = Field(alias="rawText")
confidence: float = Field(ge=0.0, le=1.0)
extracted_fields: dict[str, ExtractedField] = Field(
default_factory=dict, alias="extractedFields"
)
processing_time_ms: int = Field(alias="processingTimeMs")
model_config = {"populate_by_name": True}
class JobStatus(str, Enum):
"""Status of an async OCR job."""
PENDING = "pending"
PROCESSING = "processing"
COMPLETED = "completed"
FAILED = "failed"
class JobResponse(BaseModel):
"""Response for async job status."""
job_id: str = Field(alias="jobId")
status: JobStatus
progress: Optional[int] = Field(default=None, ge=0, le=100)
result: Optional[OcrResponse] = None
error: Optional[str] = None
model_config = {"populate_by_name": True}
class JobSubmitRequest(BaseModel):
"""Request to submit an async OCR job."""
callback_url: Optional[str] = Field(default=None, alias="callbackUrl")
model_config = {"populate_by_name": True}