File size: 1,978 Bytes
0a6b0fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from fastapi import FastAPI, HTTPException, Body
from pydantic import BaseModel
import numpy as np
import cv2
import base64
from typing import Dict, List, Any
import os
import sys

# Add current directory to path to ensure imports work
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from ocr_processors import OCRManager
from unified_extractors import Vendor, VendorSchemaManager

app = FastAPI(title="OCR Service")

# Initialize managers globally
ocr_manager = OCRManager()
schema_manager = VendorSchemaManager()

class OCRRequest(BaseModel):
    image: str  # Base64 encoded image
    vendor_id: str

@app.get("/")
def health_check():
    return {"status": "ok", "service": "OCR Service"}

@app.post("/process")
def process_image(request: OCRRequest):
    try:
        # Decode image
        image_data = base64.b64decode(request.image)
        nparr = np.frombuffer(image_data, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        
        if image is None:
            raise HTTPException(status_code=400, detail="Invalid image data")

        # Resolve vendor
        try:
            vendor = Vendor(request.vendor_id)
        except ValueError:
            # Fallback for unknown vendors if necessary, or error
            # For now, let's assume valid vendor or default
            vendor = Vendor.DEFAULT

        # Extract text using the EXACT same logic as the original app
        # The OCRManager inside this service is the original code
        results = ocr_manager.extract_text_with_positions(
            image, 
            vendor, 
            schema_manager
        )
        
        return {"status": "success", "text_blocks": results}

    except Exception as e:
        print(f"ERROR in OCR Service: {str(e)}")
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)