abhinavvvvv commited on
Commit
699048b
·
1 Parent(s): 69e7a6e

initial commit

Browse files
Files changed (5) hide show
  1. Dockerfile +19 -0
  2. app/main.py +23 -0
  3. app/ocr_engine.py +29 -0
  4. app/schemas.py +9 -0
  5. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ RUN apt-get update && apt-get install -y \
6
+ libglib2.0-0 \
7
+ libsm6 \
8
+ libxext6 \
9
+ libxrender1 \
10
+ && rm -rf /var/lib/apt/lists/*
11
+
12
+ COPY requirements.txt .
13
+ RUN pip install --no-cache-dir -r requirements.txt
14
+
15
+ COPY app ./app
16
+
17
+ EXPOSE 8000
18
+
19
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
app/main.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from app.ocr_engine import OCREngine
3
+ from app.schemas import OCRResponse, OCRLine
4
+
5
+ app = FastAPI(
6
+ title="Invoice OCR API",
7
+ version="1.0.0"
8
+ )
9
+
10
+ ocr_engine = OCREngine()
11
+
12
+ @app.get("/")
13
+ def health():
14
+ return {"status": "OCR API running"}
15
+
16
+ @app.post("/ocr", response_model=OCRResponse)
17
+ async def ocr_endpoint(file: UploadFile = File(...)):
18
+ image_bytes = await file.read()
19
+ lines = ocr_engine.extract_text(image_bytes)
20
+
21
+ return OCRResponse(
22
+ lines=[OCRLine(**line) for line in lines]
23
+ )
app/ocr_engine.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from paddleocr import PaddleOCR
2
+ from PIL import Image
3
+ import numpy as np
4
+ import cv2
5
+
6
+ class OCREngine:
7
+ def __init__(self):
8
+ self.ocr = PaddleOCR(
9
+ use_angle_cls=True,
10
+ lang='en',
11
+ use_gpu=False
12
+ )
13
+
14
+ def extract_text(self, image_bytes):
15
+ nparr = np.frombuffer(image_bytes, np.uint8)
16
+ img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
17
+
18
+ result = self.ocr.ocr(img, cls=True)
19
+
20
+ lines = []
21
+ for line in result[0]:
22
+ text = line[1][0]
23
+ confidence = float(line[1][1])
24
+ lines.append({
25
+ "text": text,
26
+ "confidence": confidence
27
+ })
28
+
29
+ return lines
app/schemas.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+ from typing import List
3
+
4
+ class OCRLine(BaseModel):
5
+ text: str
6
+ confidence: float
7
+
8
+ class OCRResponse(BaseModel):
9
+ lines: List[OCRLine]
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ paddleocr
4
+ paddlepaddle==2.6.1
5
+ python-multipart
6
+ opencv-python-headless
7
+ pillow