|
from fastapi import FastAPI, File, UploadFile, HTTPException |
|
from fastapi.responses import JSONResponse |
|
from typing import List |
|
from rapidocr_onnxruntime import RapidOCR |
|
from PIL import Image |
|
import numpy as np |
|
import uvicorn |
|
from io import BytesIO |
|
|
|
app = FastAPI( |
|
title="OCR API", |
|
description="API for Optical Character Recognition using RapidOCR", |
|
version="1.0.0" |
|
) |
|
|
|
engine = RapidOCR() |
|
|
|
@app.get("/") |
|
async def index(): |
|
return {"message": "Welcome to the OCR API"} |
|
|
|
@app.post("/ocr") |
|
async def process_ocr(images: List[UploadFile] = File(...)): |
|
if not images: |
|
raise HTTPException(status_code=400, detail="No image files provided") |
|
|
|
text_results = [] |
|
|
|
for img_file in images: |
|
try: |
|
contents = await img_file.read() |
|
img = Image.open(BytesIO(contents)) |
|
|
|
width, height = img.size |
|
crop_top = 201 |
|
crop_area = (0, crop_top, width, height // 2) |
|
img_crop = img.crop(crop_area) |
|
|
|
img_array = np.array(img_crop) |
|
result, _ = engine(img_array) |
|
|
|
text_array = [item[1] for item in result] |
|
text_results.append(text_array) |
|
|
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}") |
|
|
|
return JSONResponse(content={"text": text_results}) |
|
|
|
if __name__ == "__main__": |
|
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=False) |