Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import uvicorn
|
| 4 |
+
import easyocr
|
| 5 |
+
import os
|
| 6 |
+
import shutil
|
| 7 |
+
import uuid
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
reader = easyocr.Reader(['en', 'hi', 'mr'], gpu=False)
|
| 11 |
+
|
| 12 |
+
def extract_keywords(image_path):
|
| 13 |
+
results = reader.readtext(image_path, detail=1)
|
| 14 |
+
keywords = []
|
| 15 |
+
for _, text, _ in results:
|
| 16 |
+
if text:
|
| 17 |
+
parts = text.split()
|
| 18 |
+
for p in parts:
|
| 19 |
+
cleaned = p.strip().upper()
|
| 20 |
+
if cleaned:
|
| 21 |
+
keywords.append(cleaned)
|
| 22 |
+
return keywords
|
| 23 |
+
|
| 24 |
+
@app.post("/ocr")
|
| 25 |
+
async def ocr_endpoint(file: UploadFile = File(...)):
|
| 26 |
+
tmp_name = f"/tmp/{uuid.uuid4().hex}_{file.filename}"
|
| 27 |
+
with open(tmp_name, "wb") as buffer:
|
| 28 |
+
shutil.copyfileobj(file.file, buffer)
|
| 29 |
+
|
| 30 |
+
keywords = extract_keywords(tmp_name)
|
| 31 |
+
|
| 32 |
+
return JSONResponse({"ocrKeywords": keywords})
|