File size: 668 Bytes
9a0d4a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import uuid
import time
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse

from ocr_core import OCRCore

app = FastAPI()
ocr = OCRCore()

TMP_DIR = "/tmp/ocr_api"
os.makedirs(TMP_DIR, exist_ok=True)

def save_tmp(file: UploadFile):
    ext = os.path.splitext(file.filename)[1] or ".jpg"
    name = f"{int(time.time())}_{uuid.uuid4().hex}{ext}"
    path = os.path.join(TMP_DIR, name)

    with open(path, "wb") as f:
        f.write(file.file.read())
    return path

@app.post("/ocr")
async def run_ocr(file: UploadFile = File(...)):
    path = save_tmp(file)
    result = ocr.run(path)
    return JSONResponse(result)