from fastapi import FastAPI, UploadFile, File, Request from fastapi.responses import JSONResponse import requests import easyocr from PIL import Image import numpy as np from io import BytesIO app = FastAPI(title="OCR API", description="OCR through URL or Buffer") reader = easyocr.Reader(['en'], gpu=False) def run_ocr(image_bytes): img = Image.open(BytesIO(image_bytes)).convert("RGB") img_np = np.array(img) results = reader.readtext(img_np) parsed = [] output = [] for box, text, conf in results: parsed.append(str(text)) output.append({ "text": str(text), "confidence": float(conf), "bounding_box": [[float(x), float(y)] for x, y in box] }) final_text = " ".join(parsed) return { "results": output, "parsedText": parsed, "finalText": final_text } @app.get("/ocr") async def ocr_url(url: str): try: img_data = requests.get(url, timeout=10).content data = run_ocr(img_data) return JSONResponse({"success": True, **data}) except Exception as e: return JSONResponse({"success": False, "error": str(e)}, status_code=400) @app.post("/ocr") async def ocr_buffer(file: UploadFile = File(...)): try: image_bytes = await file.read() data = run_ocr(image_bytes) return JSONResponse({"success": True, **data}) except Exception as e: return JSONResponse({"success": False, "error": str(e)}, status_code=400)