Project title: Tamil Handwritten recognition using hybrid CNN-GRU and CNN-LSTM
model. I had a Tamil charcter dataset
to achieve or proposed system: TO tamil handwritten charcter or text to
Phonetics.
Dataset: Are you using an existing Tamil handwritten character dataset (like HP
Labs India’s dataset or something custom), or are you collecting your own
samples?
CNN: This would extract spatial features from the handwritten character images,
like edges, curves, or strokes specific to Tamil script.
GRU and LSTM: These recurrent layers could help capture the sequential
dependencies in the characters (e.g., how strokes connect or vary), with GRU
being lighter and LSTM potentially handling longer dependencies.
Hybrid Model: Combining CNN-GRU and CNN-LSTM—do you plan to ensemble their
outputs, stack them, or compare their performance?
Goal: Accurate recognition of Tamil characters, possibly with metrics like
accuracy, precision, or F1-score.
# Mount Google Drive
from [Link] import drive
[Link]('/content/drive')
# Import necessary libraries
import os
import shutil
import cv2
import numpy as np
from matplotlib import pyplot as plt
from [Link] import Sequential
from [Link] import Conv2D, MaxPooling2D, Flatten, Reshape, GRU,
LSTM, Dense, Dropout
from [Link] import LabelEncoder
from sklearn.model_selection import train_test_split
# Define dataset paths
dataset_path = "/content/drive/My Drive/Tamil_dataset"
labeled_dataset_path = "/content/drive/My Drive/Tamil_dataset_labeled"
preprocessed_dataset_path = "/content/drive/My Drive/Tamil_dataset_preprocessed"
# Tamil Uyir Ezhuthukkal and Ayyutha Ezhuthu mapped to phonetics and characters
tamil_to_phonetic = {
"1": "a", # அ
"2": "aa", # ஆ
"3": "i", # இ
"4": "ii", # ஈ
"5": "u", # உ
"6": "uu", # ஊ
"7": "e", # எ
"8": "ee", # ஏ
"9": "ai", # ஐ
"10": "o", # ஒ
"11": "oo", # ஓ
"12": "au", # ஔ
"13": "ah" # ஃ
}
tamil_unicode = {
"1": "அ", "2": "ஆ", "3": "இ", "4": "ஈ", "5": "உ", "6": "ஊ",
"7": "எ", "8": "ஏ", "9": "ஐ", "10": "ஒ", "11": "ஓ", "12": "ஔ", "13":
"ஃ"
}
# Step 1: Verify and Label Dataset
if not [Link](dataset_path):
print(f"Dataset path not found: {dataset_path}. Please upload your
dataset.")
else:
folders = sorted([Link](dataset_path))
print("Folders in dataset:", folders)
[Link](labeled_dataset_path, exist_ok=True)
for folder in folders:
folder_path = [Link](dataset_path, folder)
new_folder_name = tamil_to_phonetic.get(folder, folder)
new_folder_path = [Link](labeled_dataset_path, new_folder_name)
[Link](new_folder_path, exist_ok=True)
for img_file in [Link](folder_path):
old_img_path = [Link](folder_path, img_file)
new_img_name = f"{new_folder_name}_{img_file}"
new_img_path = [Link](new_folder_path, new_img_name)
[Link](old_img_path, new_img_path)
print("Renaming and labeling completed!")
# Step 2: Preprocess Images
IMG_SIZE = (64, 64) # Define image size
[Link](preprocessed_dataset_path, exist_ok=True)
def preprocess_image(image_path, target_size):
image = [Link](image_path, cv2.IMREAD_GRAYSCALE)
if image is None:
return None
image = [Link](image, target_size)
image = image / 255.0 # Normalize to [0, 1]
return image
preprocessed_images = []
labels = []
for label in [Link](labeled_dataset_path):
label_path = [Link](labeled_dataset_path, label)
preprocessed_label_path = [Link](preprocessed_dataset_path, label)
[Link](preprocessed_label_path, exist_ok=True)
for img_file in [Link](label_path):
img_path = [Link](label_path, img_file)
preprocessed_img = preprocess_image(img_path, IMG_SIZE)
if preprocessed_img is not None:
preprocessed_images.append(preprocessed_img)
[Link](label)
preprocessed_img_path = [Link](preprocessed_label_path,
img_file)
[Link](preprocessed_img_path, preprocessed_img * 255)
preprocessed_images = [Link](preprocessed_images)
labels = [Link](labels)
[Link]([Link](preprocessed_dataset_path, "preprocessed_images.npy"),
preprocessed_images)
[Link]([Link](preprocessed_dataset_path, "[Link]"), labels)
print("Data preprocessing completed!")
print(f"Preprocessed images shape: {preprocessed_images.shape}")
print(f"Labels shape: {[Link]}")
# Step 3: Prepare Data for Training
label_encoder = LabelEncoder()
encoded_labels = label_encoder.fit_transform(labels)
preprocessed_images = preprocessed_images.reshape(-1, IMG_SIZE[0], IMG_SIZE[1],
1)
X_train_val, X_test, y_train_val, y_test = train_test_split(preprocessed_images,
encoded_labels, test_size=0.2, random_state=42)
X_train, X_val, y_train, y_val = train_test_split(X_train_val, y_train_val,
test_size=0.2, random_state=42)
# Step 4: Define CNN-GRU Model
model_cnn_gru = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_SIZE[0], IMG_SIZE[1],
1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Reshape((-1, 64)),
GRU(128, return_sequences=False),
Dense(64, activation='relu'),
Dropout(0.5),
Dense(len(label_encoder.classes_), activation='softmax')
])
model_cnn_gru.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model_cnn_gru.summary()
# Step 5: Define CNN-LSTM Model
model_cnn_lstm = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_SIZE[0], IMG_SIZE[1],
1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Reshape((-1, 64)),
LSTM(128, return_sequences=False),
Dense(64, activation='relu'),
Dropout(0.5),
Dense(len(label_encoder.classes_), activation='softmax')
])
model_cnn_lstm.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model_cnn_lstm.summary()
# Step 6: Train Models
history_cnn_gru = model_cnn_gru.fit(X_train, y_train, validation_data=(X_val,
y_val), epochs=20, batch_size=32, verbose=1)
history_cnn_lstm = model_cnn_lstm.fit(X_train, y_train, validation_data=(X_val,
y_val), epochs=20, batch_size=32, verbose=1)
# Step 7: Evaluate Models
test_loss_cnn_gru, test_acc_cnn_gru = model_cnn_gru.evaluate(X_test, y_test,
verbose=0)
print("CNN-GRU Test Accuracy:", test_acc_cnn_gru)
test_loss_cnn_lstm, test_acc_cnn_lstm = model_cnn_lstm.evaluate(X_test, y_test,
verbose=0)
print("CNN-LSTM Test Accuracy:", test_acc_cnn_lstm)
# Step 8: Predict on a Single Image
def preprocess_input_image(image_path):
image = [Link](image_path, cv2.IMREAD_GRAYSCALE)
if image is None:
print(f"Error: Could not load image at {image_path}")
return None
image = [Link](image, IMG_SIZE)
image = image / 255.0
image = [Link](1, IMG_SIZE[0], IMG_SIZE[1], 1)
return image
input_image_path = "/content/drive/My Drive/tamil_ah.png" # Update this path as
needed
input_image = preprocess_input_image(input_image_path)
if input_image is not None:
prediction = model_cnn_gru.predict(input_image, verbose=0)
predicted_label = [Link](prediction, axis=1)[0]
predicted_phonetic = label_encoder.inverse_transform([predicted_label])[0]
predicted_tamil_char = tamil_unicode.get([k for k, v in
tamil_to_phonetic.items() if v == predicted_phonetic][0], "Unknown")
print("Predicted Phonetic Name:", predicted_phonetic)
print("Predicted Tamil Character:", predicted_tamil_char)
[Link](input_image.reshape(IMG_SIZE[0], IMG_SIZE[1]), cmap='gray')
[Link](f"Predicted: {predicted_tamil_char} ({predicted_phonetic})")
[Link]('off')
[Link]()
# Step 9: Save Models (Optional)
model_cnn_gru.save('/content/drive/My Drive/cnn_gru_model.h5')
model_cnn_lstm.save('/content/drive/My Drive/cnn_lstm_model.h5')
print("Models saved successfully!") ..........it has an predict wrong .
pls check and analyse the code. focus on training part