forked from JaidedAI/EasyOCR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
87 lines (75 loc) · 2.69 KB
/
server.py
File metadata and controls
87 lines (75 loc) · 2.69 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.responses import JSONResponse
import easyocr
import uvicorn
import numpy as np # Import numpy
app = FastAPI(
title="EasyOCR FastAPI Server",
description="Upload an image and get OCR results",
)
# Cache readers by (tuple(langs), gpu_flag)
_readers: dict[tuple[tuple[str, ...], bool], easyocr.Reader] = {}
def get_reader(langs: list[str], gpu: bool) -> easyocr.Reader:
key = (tuple(langs), gpu)
if key not in _readers:
_readers[key] = easyocr.Reader(langs, gpu=gpu)
return _readers[key]
def to_native_types(data):
"""
Recursively converts numpy types in a data structure to native Python types
to ensure JSON serialization.
"""
if isinstance(data, list):
return [to_native_types(item) for item in data]
if isinstance(data, tuple):
return tuple(to_native_types(item) for item in data)
if isinstance(data, np.integer):
return int(data)
if isinstance(data, np.floating):
return float(data)
if isinstance(data, np.ndarray):
return data.tolist()
return data
@app.post("/readtext")
async def readtext_endpoint(
file: UploadFile = File(...),
langs: str = Form("en"),
detail: int = Form(1),
gpu: bool = Form(True),
):
"""
Perform OCR on an uploaded image.
- file: image to OCR
- langs: comma-separated language codes, e.g. "en,fr,ch_sim"
- detail: 1 for full output, 0 for flat list of strings
- gpu: whether to use GPU
"""
try:
print(
f"Received file: {file.filename}, langs: {langs}, detail: {detail}, gpu: {gpu}"
)
languages = [l.strip() for l in langs.split(",") if l.strip()]
reader = get_reader(languages, gpu)
img_bytes = await file.read()
result = reader.readtext(img_bytes, detail=detail)
# --- SOLUTION: Convert the result before returning ---
serializable_result = to_native_types(result)
return JSONResponse({"result": serializable_result})
except Exception as e:
# It's good practice to log the full exception for debugging
print(f"An error occurred: {e}")
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def health_check():
import torch
return {
"status": "ok",
"message": "EasyOCR server is running",
"cuda_available": torch.cuda.is_available(),
"cuda_device_count": torch.cuda.device_count(),
"current_device": torch.cuda.current_device() if torch.cuda.is_available() else None
}
if __name__ == "__main__":
uvicorn.run("server:app", host="0.0.0.0", port=80)