Spaces:
Runtime error
Runtime error
Rivalcoder
commited on
Commit
Β·
cb18b0f
1
Parent(s):
4b32c6f
Add files
Browse files
app.py
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import easyocr
|
|
@@ -17,12 +21,12 @@ USER_NET_DIR = os.path.join(MODEL_DIR, "user_network")
|
|
| 17 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 18 |
os.makedirs(USER_NET_DIR, exist_ok=True)
|
| 19 |
|
| 20 |
-
# β
preload reader
|
| 21 |
reader = easyocr.Reader(
|
| 22 |
-
['en', 'hi'],
|
| 23 |
model_storage_directory=MODEL_DIR,
|
| 24 |
user_network_directory=USER_NET_DIR,
|
| 25 |
-
download_enabled=False
|
| 26 |
)
|
| 27 |
|
| 28 |
# =========================
|
|
@@ -35,22 +39,14 @@ async def root():
|
|
| 35 |
return {"message": "OCR API is running on Hugging Face π"}
|
| 36 |
|
| 37 |
|
| 38 |
-
def run_ocr_on_image(image: Image.Image):
|
| 39 |
-
"""Convert PIL β numpy
|
| 40 |
image_np = np.array(image)
|
| 41 |
results = reader.readtext(image_np)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
for
|
| 45 |
-
|
| 46 |
-
bbox_py = [[float(x), float(y)] for x, y in bbox]
|
| 47 |
-
|
| 48 |
-
text_results.append({
|
| 49 |
-
"bbox": bbox_py,
|
| 50 |
-
"text": str(text),
|
| 51 |
-
"confidence": float(prob)
|
| 52 |
-
})
|
| 53 |
-
return text_results
|
| 54 |
|
| 55 |
|
| 56 |
@app.post("/ocr")
|
|
@@ -58,28 +54,27 @@ async def ocr(file: UploadFile = File(...)):
|
|
| 58 |
try:
|
| 59 |
contents = await file.read()
|
| 60 |
|
| 61 |
-
# Detect file type
|
| 62 |
if file.filename.lower().endswith(".pdf"):
|
| 63 |
-
# β
Convert PDF
|
| 64 |
pages = convert_from_bytes(contents)
|
| 65 |
|
| 66 |
# β
Run OCR in parallel
|
| 67 |
text_results = []
|
| 68 |
with ThreadPoolExecutor() as executor:
|
| 69 |
-
|
| 70 |
-
for i,
|
| 71 |
text_results.append({
|
| 72 |
"page": i,
|
| 73 |
-
"
|
| 74 |
})
|
| 75 |
|
| 76 |
return JSONResponse(content={"pdf_results": text_results})
|
| 77 |
|
| 78 |
else:
|
| 79 |
-
# β
|
| 80 |
image = Image.open(io.BytesIO(contents))
|
| 81 |
-
|
| 82 |
-
return JSONResponse(content={"
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
warnings.filterwarnings("ignore", message=".*pin_memory.*")
|
| 3 |
+
|
| 4 |
+
|
| 5 |
import os
|
| 6 |
import io
|
| 7 |
import easyocr
|
|
|
|
| 21 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 22 |
os.makedirs(USER_NET_DIR, exist_ok=True)
|
| 23 |
|
| 24 |
+
# β
preload reader
|
| 25 |
reader = easyocr.Reader(
|
| 26 |
+
['en', 'hi'],
|
| 27 |
model_storage_directory=MODEL_DIR,
|
| 28 |
user_network_directory=USER_NET_DIR,
|
| 29 |
+
download_enabled=False
|
| 30 |
)
|
| 31 |
|
| 32 |
# =========================
|
|
|
|
| 39 |
return {"message": "OCR API is running on Hugging Face π"}
|
| 40 |
|
| 41 |
|
| 42 |
+
def run_ocr_on_image(image: Image.Image) -> str:
|
| 43 |
+
"""Convert PIL β numpy, run OCR, return plain text"""
|
| 44 |
image_np = np.array(image)
|
| 45 |
results = reader.readtext(image_np)
|
| 46 |
|
| 47 |
+
# β
only text, join all detections
|
| 48 |
+
extracted_text = " ".join([str(text) for _, text, _ in results])
|
| 49 |
+
return extracted_text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
@app.post("/ocr")
|
|
|
|
| 54 |
try:
|
| 55 |
contents = await file.read()
|
| 56 |
|
|
|
|
| 57 |
if file.filename.lower().endswith(".pdf"):
|
| 58 |
+
# β
Convert PDF β images
|
| 59 |
pages = convert_from_bytes(contents)
|
| 60 |
|
| 61 |
# β
Run OCR in parallel
|
| 62 |
text_results = []
|
| 63 |
with ThreadPoolExecutor() as executor:
|
| 64 |
+
page_texts = list(executor.map(run_ocr_on_image, pages))
|
| 65 |
+
for i, text in enumerate(page_texts, start=1):
|
| 66 |
text_results.append({
|
| 67 |
"page": i,
|
| 68 |
+
"text": text
|
| 69 |
})
|
| 70 |
|
| 71 |
return JSONResponse(content={"pdf_results": text_results})
|
| 72 |
|
| 73 |
else:
|
| 74 |
+
# β
Single image case
|
| 75 |
image = Image.open(io.BytesIO(contents))
|
| 76 |
+
text = run_ocr_on_image(image)
|
| 77 |
+
return JSONResponse(content={"text": text})
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|