Spaces:
Paused
Paused
Commit ·
31ce858
1
Parent(s): d67f610
🧠 Switched to Flask API for Hindi OCR
Browse files- app.py +36 -35
- packages.txt +0 -1
- requirements.txt +3 -3
app.py
CHANGED
|
@@ -1,38 +1,39 @@
|
|
| 1 |
-
import
|
| 2 |
import pytesseract
|
| 3 |
-
from pdf2image import convert_from_bytes
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
import pytesseract
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import fitz # PyMuPDF
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
@app.route('/ocr', methods=['POST'])
|
| 10 |
+
def ocr():
|
| 11 |
+
if 'file' not in request.files:
|
| 12 |
+
return jsonify({'error': 'No file part'}), 400
|
| 13 |
+
|
| 14 |
+
file = request.files['file']
|
| 15 |
+
filename = file.filename
|
| 16 |
+
|
| 17 |
+
temp_path = f"temp_{filename}"
|
| 18 |
+
file.save(temp_path)
|
| 19 |
+
|
| 20 |
+
extracted_text = ""
|
| 21 |
+
|
| 22 |
+
if filename.lower().endswith(".pdf"):
|
| 23 |
+
pdf = fitz.open(temp_path)
|
| 24 |
+
for page_num in range(len(pdf)):
|
| 25 |
+
page = pdf.load_page(page_num)
|
| 26 |
+
pix = page.get_pixmap(dpi=300)
|
| 27 |
+
img_path = f"page_{page_num}.png"
|
| 28 |
+
pix.save(img_path)
|
| 29 |
+
|
| 30 |
+
img = Image.open(img_path)
|
| 31 |
+
extracted_text += pytesseract.image_to_string(img, lang="hin+eng") + "\n"
|
| 32 |
+
|
| 33 |
+
os.remove(img_path)
|
| 34 |
+
else:
|
| 35 |
+
img = Image.open(temp_path)
|
| 36 |
+
extracted_text = pytesseract.image_to_string(img, lang="hin+eng")
|
| 37 |
+
|
| 38 |
+
os.remove(temp_path)
|
| 39 |
+
return jsonify({'text': extracted_text.strip()})
|
packages.txt
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
tesseract-ocr
|
| 2 |
tesseract-ocr-hin
|
| 3 |
-
poppler-utils
|
| 4 |
libglib2.0-0
|
| 5 |
libsm6
|
| 6 |
libxrender1
|
|
|
|
| 1 |
tesseract-ocr
|
| 2 |
tesseract-ocr-hin
|
|
|
|
| 3 |
libglib2.0-0
|
| 4 |
libsm6
|
| 5 |
libxrender1
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
pytesseract
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 1 |
+
flask
|
| 2 |
pytesseract
|
| 3 |
+
Pillow
|
| 4 |
+
PyMuPDF
|