Files
breakpilot-core/paddleocr-service/main.py
Benjamin Admin 992d4f2a6b
Some checks failed
Deploy to Coolify / deploy (push) Has been cancelled
fix: PaddleOCR v3 API — explicit model name + predict() statt ocr()
lang="latin" braucht text_recognition_model_name in PP-OCRv5.
Neue API nutzt predict() statt ocr(), Ergebnis-Format angepasst.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 12:47:07 +01:00

81 lines
2.1 KiB
Python

"""PaddleOCR Remote Service — PP-OCRv5 Latin auf x86_64."""
import io
import os
import numpy as np
from fastapi import FastAPI, File, Header, HTTPException, UploadFile
from PIL import Image
app = FastAPI(title="PaddleOCR Service")
_engine = None
API_KEY = os.environ.get("PADDLEOCR_API_KEY", "")
def get_engine():
global _engine
if _engine is None:
from paddleocr import PaddleOCR
_engine = PaddleOCR(
lang="en",
text_recognition_model_name="latin_PP-OCRv5_mobile_rec",
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False,
show_log=False,
)
return _engine
@app.get("/health")
def health():
return {"status": "ok", "model": "PP-OCRv5-latin"}
@app.post("/ocr")
async def ocr(
file: UploadFile = File(...),
x_api_key: str = Header(default=""),
):
if API_KEY and x_api_key != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
img_bytes = await file.read()
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
img_np = np.array(img)
engine = get_engine()
result = engine.predict(img_np)
words = []
for item in result:
rec_texts = item.get("rec_texts", [])
rec_scores = item.get("rec_scores", [])
dt_polys = item.get("dt_polys", [])
for text, score, poly in zip(rec_texts, rec_scores, dt_polys):
if not text or not text.strip():
continue
xs = [p[0] for p in poly]
ys = [p[1] for p in poly]
x_min, x_max = min(xs), max(xs)
y_min, y_max = min(ys), max(ys)
words.append(
{
"text": text.strip(),
"left": int(x_min),
"top": int(y_min),
"width": int(x_max - x_min),
"height": int(y_max - y_min),
"conf": round(float(score) * 100, 1),
}
)
return {
"words": words,
"image_width": img_np.shape[1],
"image_height": img_np.shape[0],
}