File size: 1,166 Bytes
2caaf23
 
 
 
 
 
 
e4cd444
2caaf23
 
 
e4cd444
 
 
 
 
 
 
2caaf23
 
 
 
 
 
 
 
 
 
 
 
 
 
e4cd444
 
2caaf23
 
 
e4cd444
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
import os
from fastapi import FastAPI, File, UploadFile
from paddleocr import PaddleOCR
from PIL import Image
import numpy as np
import io
import uvicorn
from functools import lru_cache

app = FastAPI()

# Define a cache to store the results of the OCR function
@lru_cache(maxsize=128)
def ocr_cache(img_array):
    ocr = PaddleOCR(lang='en', use_angle_cls=True)
    result = ocr.ocr(img_array)
    extracted_text =''.join([line[1][0] for line in result[0]])
    return extracted_text

@app.post("/extract-text/")
async def extract_text(file: UploadFile = File(...)):
    image = Image.open(io.BytesIO(await file.read()))
    
    # Convert the image to a NumPy array
    img_array = np.array(image)

    # Handle different image channels
    if img_array.ndim == 2:  # Grayscale image
        img_array = np.stack((img_array,)*3, axis=-1)
    elif img_array.shape[-1] == 4:  # RGBA image
        img_array = img_array[..., :3]
    
    # Perform OCR using the cache
    extracted_text = ocr_cache(tuple(img_array.flatten()))
    return {"text": extracted_text}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))