Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 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
|
| 10 |
|
| 11 |
reader = easyocr.Reader(['en', 'id'], gpu=False)
|
| 12 |
|
|
@@ -34,17 +34,20 @@ def run_ocr(image_bytes):
|
|
| 34 |
"finalText": final_text
|
| 35 |
}
|
| 36 |
|
| 37 |
-
@app.
|
| 38 |
-
async def
|
| 39 |
-
image_bytes = await file.read()
|
| 40 |
-
data = run_ocr(image_bytes)
|
| 41 |
-
return JSONResponse({"success": True, **data})
|
| 42 |
-
|
| 43 |
-
@app.get("/ocr/url")
|
| 44 |
-
async def ocr_url(image_url: str):
|
| 45 |
try:
|
| 46 |
-
img_data = requests.get(
|
| 47 |
data = run_ocr(img_data)
|
| 48 |
return JSONResponse({"success": True, **data})
|
| 49 |
except Exception as e:
|
| 50 |
return JSONResponse({"success": False, "error": str(e)}, status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
from fastapi.responses import JSONResponse
|
|
|
|
| 3 |
import requests
|
| 4 |
+
import easyocr
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from io import BytesIO
|
| 8 |
|
| 9 |
+
app = FastAPI(title="OCR API", description="OCR through URL or Buffer")
|
| 10 |
|
| 11 |
reader = easyocr.Reader(['en', 'id'], gpu=False)
|
| 12 |
|
|
|
|
| 34 |
"finalText": final_text
|
| 35 |
}
|
| 36 |
|
| 37 |
+
@app.get("/ocr")
|
| 38 |
+
async def ocr_url(url: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
+
img_data = requests.get(url, timeout=10).content
|
| 41 |
data = run_ocr(img_data)
|
| 42 |
return JSONResponse({"success": True, **data})
|
| 43 |
except Exception as e:
|
| 44 |
return JSONResponse({"success": False, "error": str(e)}, status_code=400)
|
| 45 |
+
|
| 46 |
+
@app.post("/ocr")
|
| 47 |
+
async def ocr_buffer(file: UploadFile = File(...)):
|
| 48 |
+
try:
|
| 49 |
+
image_bytes = await file.read()
|
| 50 |
+
data = run_ocr(image_bytes)
|
| 51 |
+
return JSONResponse({"success": True, **data})
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return JSONResponse({"success": False, "error": str(e)}, status_code=400)
|