ken4 commited on
Commit
cc4df30
·
verified ·
1 Parent(s): 0232099

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +10 -0
  2. app.py +40 -0
  3. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import JSONResponse
3
+ import easyocr
4
+ import requests
5
+ from PIL import Image
6
+ import numpy as np
7
+ from io import BytesIO
8
+
9
+ app = FastAPI(title="OCR API", description="OCR API with buffer & URL support")
10
+
11
+ reader = easyocr.Reader(['en', 'id'], gpu=False)
12
+
13
+ def run_ocr(image_bytes):
14
+ img = Image.open(BytesIO(image_bytes)).convert("RGB")
15
+ img_np = np.array(img)
16
+ results = reader.readtext(img_np)
17
+
18
+ output = []
19
+ for box, text, conf in results:
20
+ output.append({
21
+ "text": text,
22
+ "confidence": float(conf),
23
+ "bounding_box": box
24
+ })
25
+ return output
26
+
27
+ @app.post("/ocr/buffer")
28
+ async def ocr_buffer(file: UploadFile = File(...)):
29
+ image_bytes = await file.read()
30
+ result = run_ocr(image_bytes)
31
+ return JSONResponse({"success": True, "results": result})
32
+
33
+ @app.get("/ocr/url")
34
+ async def ocr_url(image_url: str):
35
+ try:
36
+ img_data = requests.get(image_url, timeout=10).content
37
+ result = run_ocr(img_data)
38
+ return JSONResponse({"success": True, "results": result})
39
+ except Exception as e:
40
+ return JSONResponse({"success": False, "error": str(e)}, status_code=400)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ numpy==1.26.4
4
+ opencv-python-headless==4.8.1.78
5
+ easyocr
6
+ Pillow
7
+ requests