In [ ]:
!pip install tensorflow opencv-python
from google.colab import files
uploaded = files.upload()
# Upload:
# - bangla_ocr_model.h5
# - test_char.png (any single handwritten Bangla char image)
Requirement already satisfied: tensorflow in /usr/local/lib/python3.11/dist-packages (2.18.0) Requirement already satisfied: opencv-python in /usr/local/lib/python3.11/dist-packages (4.12.0.88) Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (1.4.0) Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (1.6.3) Requirement already satisfied: flatbuffers>=24.3.25 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (25.2.10) Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (0.6.0) Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (0.2.0) Requirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (18.1.1) Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (3.4.0) Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from tensorflow) (25.0) Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<6.0.0dev,>=3.20.3 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (5.29.5) Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (2.32.3) Requirement already satisfied: setuptools in /usr/local/lib/python3.11/dist-packages (from tensorflow) (75.2.0) Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (1.17.0) Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (3.1.0) Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (4.14.1) Requirement already satisfied: wrapt>=1.11.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (1.17.2) Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (1.73.1) Requirement already satisfied: tensorboard<2.19,>=2.18 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (2.18.0) Requirement already satisfied: keras>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (3.8.0) Requirement already satisfied: numpy<2.1.0,>=1.26.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (2.0.2) Requirement already satisfied: h5py>=3.11.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (3.14.0) Requirement already satisfied: ml-dtypes<0.5.0,>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (0.4.1) Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.11/dist-packages (from tensorflow) (0.37.1) Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from astunparse>=1.6.0->tensorflow) (0.45.1) Requirement already satisfied: rich in /usr/local/lib/python3.11/dist-packages (from keras>=3.5.0->tensorflow) (13.9.4) Requirement already satisfied: namex in /usr/local/lib/python3.11/dist-packages (from keras>=3.5.0->tensorflow) (0.1.0) Requirement already satisfied: optree in /usr/local/lib/python3.11/dist-packages (from keras>=3.5.0->tensorflow) (0.16.0) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2.21.0->tensorflow) (3.4.2) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2.21.0->tensorflow) (3.10) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2.21.0->tensorflow) (2.5.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2.21.0->tensorflow) (2025.7.14) Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.11/dist-packages (from tensorboard<2.19,>=2.18->tensorflow) (3.8.2) Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.11/dist-packages (from tensorboard<2.19,>=2.18->tensorflow) (0.7.2) Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.11/dist-packages (from tensorboard<2.19,>=2.18->tensorflow) (3.1.3) Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.11/dist-packages (from werkzeug>=1.0.1->tensorboard<2.19,>=2.18->tensorflow) (3.0.2) Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.11/dist-packages (from rich->keras>=3.5.0->tensorflow) (3.0.0) Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.11/dist-packages (from rich->keras>=3.5.0->tensorflow) (2.19.2) Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.11/dist-packages (from markdown-it-py>=2.2.0->rich->keras>=3.5.0->tensorflow) (0.1.2)
Saving ko.jpeg to ko.jpeg Saving bangla_ocr_model.h5 to bangla_ocr_model.h5
In [ ]:
from keras.models import load_model
model = load_model("bangla_ocr_model.h5")
print("✅ Model loaded.")
WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
✅ Model loaded.
In [ ]:
# 🔍 Load and predict image
import cv2
import numpy as np
from keras.preprocessing.image import img_to_array
import matplotlib.pyplot as plt
# 🔡 Put your original training label map here
class_names = ['অ', 'আ', 'ই', 'ঈ', 'উ', 'ঊ', 'ঋ', 'এ', 'ঐ', 'ও', 'ঔ',
'ক', 'খ', 'গ', 'ঘ', 'ঙ', 'চ', 'ছ', 'জ', 'ঝ', 'ঞ',
'ট', 'ঠ', 'ড', 'ঢ', 'ণ', 'ত', 'থ', 'দ', 'ধ', 'ন',
'প', 'ফ', 'ব', 'ভ', 'ম', 'য', 'র', 'ল', 'শ', 'ষ',
'স', 'হ', 'ড়', 'ঢ়', 'য়', 'ৎ', '০', '১', '২', '৩',
'৪', '৫', '৬', '৭', '৮', '৯']
# Change to your image filename
img_path = "ko.jpeg"
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (64, 64))
img = img_to_array(img).reshape(1, 64, 64, 1) / 255.0
pred = model.predict(img)[0]
predicted_label = class_names[np.argmax(pred)]
print(" Predicted character:", predicted_label)
# 🖼 Show the image
plt.imshow(cv2.imread(img_path), cmap='gray')
plt.title(f"Prediction: {predicted_label}")
plt.axis('off')
plt.show()
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 228ms/step Predicted character: ঈ
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2440 (\N{BENGALI LETTER II}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Matplotlib currently does not support Bengali natively.
fig.canvas.print_figure(bytes_io, **kw)
In [ ]:
# Upload image from local machine
from google.colab import files
uploaded = files.upload()
# Get the uploaded filename
img_path = list(uploaded.keys())[0]
# Load and predict image
import cv2
import numpy as np
from keras.preprocessing.image import img_to_array
import matplotlib.pyplot as plt
# Class label map (update if yours is different)
class_names = ['অ', 'আ', 'ই', 'ঈ', 'উ', 'ঊ', 'ঋ', 'এ', 'ঐ', 'ও', 'ঔ',
'ক', 'খ', 'গ', 'ঘ', 'ঙ', 'চ', 'ছ', 'জ', 'ঝ', 'ঞ',
'ট', 'ঠ', 'ড', 'ঢ', 'ণ', 'ত', 'থ', 'দ', 'ধ', 'ন',
'প', 'ফ', 'ব', 'ভ', 'ম', 'য', 'র', 'ল', 'শ', 'ষ',
'স', 'হ', 'ড়', 'ঢ়', 'য়', 'ৎ', '০', '১', '২', '৩',
'৪', '৫', '৬', '৭', '৮', '৯']
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (64, 64))
img = img_to_array(img).reshape(1, 64, 64, 1) / 255.0
pred = model.predict(img)[0]
print(np.argmax(pred))
predicted_label = class_names[np.argmax(pred)]
print("Predicted character:", predicted_label)
plt.imshow(cv2.imread(img_path), cmap='gray')
plt.title(f"Prediction: {predicted_label}")
plt.axis('off')
plt.show()
Saving 20250724_150736.jpg to 20250724_150736.jpg 1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 50ms/step 29 Predicted character: ধ
In [ ]:
import cv2
import numpy as np
from keras.preprocessing.image import img_to_array
def preprocess_stylized_image(img_path):
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
# Threshold + invert
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Crop bounding box
coords = cv2.findNonZero(thresh)
x, y, w, h = cv2.boundingRect(coords)
cropped = thresh[y:y+h, x:x+w]
# Resize while keeping aspect ratio and padding
size = 64
h, w = cropped.shape
scale = size / max(h, w)
resized = cv2.resize(cropped, (int(w * scale), int(h * scale)))
# Create square canvas and center
square = np.ones((size, size), dtype=np.uint8) * 0 # black background
x_offset = (size - resized.shape[1]) // 2
y_offset = (size - resized.shape[0]) // 2
square[y_offset:y_offset+resized.shape[0], x_offset:x_offset+resized.shape[1]] = resized
# Normalize and reshape
img_array = img_to_array(square).reshape(1, 64, 64, 1) / 255.0
return img_array
In [ ]:
img = preprocess_stylized_image("ko.jpeg")
pred = model.predict(img)[0]
print("Predicted character:", class_names[np.argmax(pred)])
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 39ms/step Predicted character: ধ
In [ ]:
import cv2
import numpy as np
from keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
import matplotlib.pyplot as plt
from google.colab import files # remove this line if using Jupyter
# Class names
class_names = ['অ', 'আ', 'ই', 'ঈ', 'উ', 'ঊ', 'ঋ', 'এ', 'ঐ', 'ও', 'ঔ',
'ক', 'খ', 'গ', 'ঘ', 'ঙ', 'চ', 'ছ', 'জ', 'ঝ', 'ঞ',
'ট', 'ঠ', 'ড', 'ঢ', 'ণ', 'ত', 'থ', 'দ', 'ধ', 'ন',
'প', 'ফ', 'ব', 'ভ', 'ম', 'য', 'র', 'ল', 'শ', 'ষ',
'স', 'হ', 'ড়', 'ঢ়', 'য়', 'ৎ', '০', '১', '২', '৩',
'৪', '৫', '৬', '৭', '৮', '৯']
# Preprocessing function
def preprocess_stylized_image(img_path):
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
coords = cv2.findNonZero(thresh)
x, y, w, h = cv2.boundingRect(coords)
cropped = thresh[y:y+h, x:x+w]
size = 64
h, w = cropped.shape
scale = size / max(h, w)
resized = cv2.resize(cropped, (int(w * scale), int(h * scale)))
square = np.ones((size, size), dtype=np.uint8) * 0
x_offset = (size - resized.shape[1]) // 2
y_offset = (size - resized.shape[0]) // 2
square[y_offset:y_offset+resized.shape[0], x_offset:x_offset+resized.shape[1]] = resized
img_array = img_to_array(square).reshape(1, 64, 64, 1) / 255.0
return img_array, square
# Upload and predict
uploaded = files.upload()
img_path = next(iter(uploaded))
img_array, display_img = preprocess_stylized_image(img_path)
pred = model.predict(img_array)[0]
predicted_label = class_names[np.argmax(pred)]
print("Predicted character:", predicted_label)
plt.imshow(display_img, cmap='gray')
plt.title(f"Prediction: {predicted_label}")
plt.axis('off')
plt.show()
Saving images.png to images (3).png 1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 39ms/step Predicted character: ঈ
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2440 (\N{BENGALI LETTER II}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
In [ ]:
# STEP 1: Install dependencies
!pip install tensorflow opencv-python-headless --quiet
# STEP 2: Upload model + line image
from google.colab import files
uploaded = files.upload() # Upload both model.h5 and line image
# Identify uploaded model and image file
img_file = [f for f in uploaded if not f.endswith('.h5')][0]
# STEP 3: Load model
from tensorflow.keras.models import load_model
# STEP 4: Character label list (60 total)
class_names = ['অ', 'আ', 'ই', 'ঈ', 'উ', 'ঊ', 'ঋ', 'এ', 'ঐ', 'ও', 'ঔ',
'ক', 'খ', 'গ', 'ঘ', 'ঙ', 'চ', 'ছ', 'জ', 'ঝ', 'ঞ',
'ট', 'ঠ', 'ড', 'ঢ', 'ণ', 'ত', 'থ', 'দ', 'ধ', 'ন',
'প', 'ফ', 'ব', 'ভ', 'ম', 'য', 'র', 'ল', 'শ', 'ষ',
'স', 'হ', 'ড়', 'ঢ়', 'য়', 'ৎ', '০', '১', '২', '৩',
'৪', '৫', '৬', '৭', '৮', '৯']
# Auto-adjust class_names to model output
import numpy as np
from keras.preprocessing.image import img_to_array
import cv2
import matplotlib.pyplot as plt
model_output_classes = model.output_shape[-1]
if len(class_names) != model_output_classes:
class_names = class_names[:model_output_classes]
# STEP 5: Predict function
def predict_line(img_path):
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
if img is None:
print(f"Error: Could not load image from {img_path}")
return
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
bounding_boxes = [cv2.boundingRect(c) for c in contours]
if not bounding_boxes:
print("No characters detected.")
plt.imshow(cv2.imread(img_path), cmap='gray')
plt.title("No characters detected")
plt.axis('off')
plt.show()
return
bounding_boxes = sorted(bounding_boxes, key=lambda x: x[0]) # Left to right
predicted_text = ""
for box in bounding_boxes:
x, y, w, h = box
roi = thresh[y:y+h, x:x+w]
size = 64
h, w = roi.shape
scale = size / max(h, w)
resized = cv2.resize(roi, (int(w * scale), int(h * scale)))
square = np.zeros((size, size), dtype=np.uint8)
x_offset = (size - resized.shape[1]) // 2
y_offset = (size - resized.shape[0]) // 2
square[y_offset:y_offset+resized.shape[0], x_offset:x_offset+resized.shape[1]] = resized
img_array = img_to_array(square).reshape(1, 64, 64, 1) / 255.0
pred = model.predict(img_array, verbose=0)[0]
label = class_names[np.argmax(pred)]
predicted_text += label
print("Predicted Line:", predicted_text)
plt.imshow(cv2.imread(img_path), cmap='gray')
plt.axis('off')
plt.title(f"Predicted: {predicted_text}")
plt.show()
# Run it
predict_line(img_file)
Saving maxresdefault.jpg to maxresdefault (4).jpg
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /tmp/ipython-input-21-2206923193.py in <cell line: 0>() 77 78 # Run it ---> 79 predict_line(img_file) /tmp/ipython-input-21-2206923193.py in predict_line(img_path) 67 68 pred = model.predict(img_array, verbose=0)[0] ---> 69 label = class_names[np.argmax(pred)] 70 predicted_text += label 71 IndexError: list index out of range
In [ ]:
# STEP 1: Install dependencies
!pip install tensorflow opencv-python-headless --quiet
# STEP 2: Upload only the line image
from google.colab import files
uploaded = files.upload() # Just upload image (not model)
img_file = list(uploaded.keys())[0] # Grab the image filename
# STEP 3: Load pre-existing model (already in Colab runtime)
from tensorflow.keras.models import load_model
model = load_model('bangla_ocr_model.h5') # Use exact filename if different
# STEP 4: Character label list (adjust if your model has fewer outputs)
class_names = ['অ', 'আ', 'ই', 'ঈ', 'উ', 'ঊ', 'ঋ', 'এ', 'ঐ', 'ও', 'ঔ',
'ক', 'খ', 'গ', 'ঘ', 'ঙ', 'চ', 'ছ', 'জ', 'ঝ', 'ঞ',
'ট', 'ঠ', 'ড', 'ঢ', 'ণ', 'ত', 'থ', 'দ', 'ধ', 'ন',
'প', 'ফ', 'ব', 'ভ', 'ম', 'য', 'র', 'ল', 'শ', 'ষ',
'স', 'হ', 'ড়', 'ঢ়', 'য়', 'ৎ', '০', '১', '২', '৩',
'৪', '৫', '৬', '৭', '৮', '৯']
# Auto-adjust to model output size
import numpy as np
from keras.preprocessing.image import img_to_array
import cv2
import matplotlib.pyplot as plt
output_dim = model.output_shape[-1]
if len(class_names) != output_dim:
class_names = class_names[:output_dim]
# STEP 5: Prediction function
def predict_line(img_path):
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
if img is None:
print(f"Error loading image: {img_path}")
return
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
boxes = sorted([cv2.boundingRect(c) for c in contours], key=lambda b: b[0])
predicted_text = ""
for x, y, w, h in boxes:
roi = thresh[y:y+h, x:x+w]
size = 64
scale = size / max(w, h)
resized = cv2.resize(roi, (int(w * scale), int(h * scale)))
square = np.zeros((size, size), dtype=np.uint8)
x_offset = (size - resized.shape[1]) // 2
y_offset = (size - resized.shape[0]) // 2
square[y_offset:y_offset+resized.shape[0], x_offset:x_offset+resized.shape[1]] = resized
img_array = img_to_array(square).reshape(1, 64, 64, 1) / 255.0
pred = model.predict(img_array, verbose=0)[0]
label_idx = np.argmax(pred)
if label_idx < len(class_names):
predicted_text += class_names[label_idx]
else:
predicted_text += '?'
print("Predicted Line:", predicted_text)
plt.imshow(cv2.imread(img_path), cmap='gray')
plt.title(f"Prediction: {predicted_text}")
plt.axis('off')
plt.show()
# STEP 6: Run
predict_line(img_file)
WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
Saving maxresdefault.jpg to maxresdefault (5).jpg Predicted Line: ?১ঙৎৎৎঐৎথঙঈটৎঐৎঙ৭ঙ৭থৎথজৎরঈটথথৎ৭টঈ
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2535 (\N{BENGALI DIGIT ONE}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Matplotlib currently does not support Bengali natively.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2457 (\N{BENGALI LETTER NGA}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2510 (\N{BENGALI LETTER KHANDA TA}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2448 (\N{BENGALI LETTER AI}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2469 (\N{BENGALI LETTER THA}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2440 (\N{BENGALI LETTER II}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2463 (\N{BENGALI LETTER TTA}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2541 (\N{BENGALI DIGIT SEVEN}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2460 (\N{BENGALI LETTER JA}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 2480 (\N{BENGALI LETTER RA}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
In [ ]:
# ✅ STEP 1: Install dependencies
!pip install easyocr pix2tex transformers matplotlib -q
!apt install tesseract-ocr -y > /dev/null
# ✅ STEP 2: Upload image
from google.colab import files
uploaded = files.upload()
img_path = list(uploaded.keys())[0]
# ✅ STEP 3: Bangla OCR using EasyOCR
import easyocr
reader = easyocr.Reader(['bn'], gpu=False)
bangla_results = reader.readtext(img_path, detail=0)
bangla_text = ' '.join(bangla_results)
# ✅ STEP 4: Equation OCR using pix2tex
import torch
from PIL import Image
from pix2tex.cli import LatexOCR
ocr = LatexOCR()
eq_img = Image.open(img_path)
latex_result = ocr(eq_img)
# ✅ STEP 5: Display outputs
from IPython.display import display, Markdown
print("🔡 Bangla Text:\n", bangla_text)
print("\n🧮 LaTeX Equation:\n", latex_result)
# Render LaTeX inside notebook
display(Markdown(f"**📖 Rendered Equation:** \n\n`{latex_result}`"))
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Saving Screenshot 2025-07-24 160701.jpg to Screenshot 2025-07-24 160701.jpg
🔡 Bangla Text:
সরলরেখাটি দ্বারা উভয় অক্ষের ছেদাংশ = 2 এবং ছেদবিন্দু (2, 0) ও (0, 2) তাহলে, (1) নং বৃত্ত ও (11) ন? সরলরেখা দ্বারা আবদধ ক্ষেত্রের শ এর সীমা হবে & = [ থেকে $ = 2 পর্যন্ত এবন, ৮2 +১2 = 4 বা, ঢ2 = 4 - ভ2 বা, ড = 4 - ষ- [যেহেতু শ অক্ষের উপর ধনাত্মক] এবং ঢ = 3 - & [(ু1) নং হতে] ধরি, ঢ1 = ' 4 - স2 এবং ঢ2 = 2 - & (1) নং বৃত্ত ও (11) নং রেখা দ্বারা আবদ্ধ ক্ষুদ্রতর অংশের ক্ষেত্রফল অর্থাৎ চিত্রের ছায়াঘেরা অংশের ক্ষেত্রফল, অর্থাৎ চিত্রের ছায়াঘেরা অংশের ক্ষেত্রফল (1 -92) ধ&
🧮 LaTeX Equation:
\scriptstyle{\frac{\mu(m)b^{0}b^{0}}{\hbar\hbar^{0}}}
📖 Rendered Equation:
\scriptstyle{\frac{\mu(m)b^{0}b^{0}}{\hbar\hbar^{0}}}
In [ ]:
!pip uninstall -y docx
!pip install python-docx
Found existing installation: docx 0.2.4 Uninstalling docx-0.2.4: Successfully uninstalled docx-0.2.4 Collecting python-docx Downloading python_docx-1.2.0-py3-none-any.whl.metadata (2.0 kB) Requirement already satisfied: lxml>=3.1.0 in /usr/local/lib/python3.11/dist-packages (from python-docx) (5.4.0) Requirement already satisfied: typing_extensions>=4.9.0 in /usr/local/lib/python3.11/dist-packages (from python-docx) (4.14.1) Downloading python_docx-1.2.0-py3-none-any.whl (252 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 253.0/253.0 kB 4.3 MB/s eta 0:00:00 Installing collected packages: python-docx Successfully installed python-docx-1.2.0
In [ ]:
from docx import Document
doc = Document()
doc.add_heading('Bangla OCR + Math Equation Result', level=1)
doc.add_heading('📌 Bangla Text:', level=2)
doc.add_paragraph(bangla_text)
doc.add_heading('📌 LaTeX Equation:', level=2)
doc.add_paragraph(latex_result)
doc.save("ocr_result.docx")
from google.colab import files
files.download("ocr_result.docx")
In [ ]:
# @title 🔄 LaTeX to MathJax Converter
def latex_to_mathjax_inline(latex_str):
return f"\\({latex_str}\\)" # Inline math
def latex_to_mathjax_block(latex_str):
return f"$$ {latex_str} $$" # Block-level math
def sanitize_latex(latex_str):
import re
latex_str = re.sub(r'\\displaystyle', '', latex_str)
return latex_str.strip()
In [ ]:
latex = r"9x + 27 = 49 + x - 2 + 14\sqrt{x - 2}"
cleaned = sanitize_latex(latex)
inline_mj = latex_to_mathjax_inline(cleaned)
block_mj = latex_to_mathjax_block(cleaned)
print("Inline MathJax:\n", inline_mj)
print("Block MathJax:\n", block_mj)
Inline MathJax:
\(9x + 27 = 49 + x - 2 + 14\sqrt{x - 2}\)
Block MathJax:
$$ 9x + 27 = 49 + x - 2 + 14\sqrt{x - 2} $$
In [ ]:
html_template = f"""
<!DOCTYPE html>
<html>
<head>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
</head>
<body>
<h3>Rendered Math (Block):</h3>
<p>{block_mj}</p>
<hr>
<h3>Rendered Math (Inline):</h3>
<p>This is inline math: {inline_mj}</p>
</body>
</html>
"""
# Save to file
with open("mathjax_preview.html", "w") as f:
f.write(html_template)
# Download link
from google.colab import files
files.download("mathjax_preview.html")