Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Handwritten Character Recognition System

Overview

This project implements a Handwritten Character Recognition system using Machine Learning and Deep Learning techniques. The system is capable of recognizing individual handwritten characters (alphabets) and can be extended to recognize entire words or sentences. The core model leverages Convolutional Neural Networks (CNNs) to identify and classify characters with high accuracy.

Key Features:

  • Character Recognition: The model can recognize individual characters (A-Z, a-z).
  • Word Recognition (Optional Extension): The system can be extended to recognize entire words or sentences by combining character predictions.
  • Deep Learning Models: Built using a Convolutional Neural Network (CNN), ensuring high accuracy and robustness.
  • Dataset: The system uses publicly available datasets like EMNIST (Extended MNIST) or HWDB (Handwriting Database) for training.

Table of Contents


Installation

Prerequisites

  1. Python 3.x: This project is developed using Python 3.x.
  2. Libraries: The project requires the following libraries for machine learning and image processing:
    • tensorflow (or keras)
    • numpy
    • matplotlib
    • opencv-python
    • scikit-learn
    • pandas

You can install all required libraries by running:

pip install -r requirements.txt

requirements.txt:

tensorflow==2.7.0
numpy==1.21.2
matplotlib==3.4.3
opencv-python==4.5.3.56
scikit-learn==0.24.2
pandas==1.3.3

Usage

1. Load Pre-trained Model

The system can use a pre-trained model to recognize handwritten characters. If you are using a pre-trained model, you can load it as follows:

from tensorflow.keras.models import load_model

# Load the pre-trained model
model = load_model('path_to_model/character_recognition_model.h5')

2. Preprocess Image Input

Before feeding the image into the model, preprocess the image by converting it to grayscale, resizing it, and normalizing it:

import cv2
import numpy as np

def preprocess_image(image_path):
    # Load image and convert to grayscale
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    
    # Resize image to 28x28 (assuming model trained with MNIST size)
    image = cv2.resize(image, (28, 28))
    
    # Normalize pixel values to range [0, 1]
    image = image.astype('float32') / 255.0
    
    # Reshape for model input (batch_size, height, width, channels)
    image = np.expand_dims(image, axis=-1)
    image = np.expand_dims(image, axis=0)
    
    return image

3. Predict Handwritten Character

Once the image is preprocessed, you can make a prediction:

# Preprocess the image
processed_image = preprocess_image('path_to_image.png')

# Predict the character
prediction = model.predict(processed_image)
predicted_char = np.argmax(prediction)

print(f"Predicted Character: {chr(predicted_char + 65)}")  # Convert label to character (A=0, B=1, ...)

Model Training

1. Dataset

For character recognition, we use a dataset like EMNIST or HWDB which contains handwritten characters. These datasets can be loaded using the following:

from tensorflow.keras.datasets import emnist

# Load EMNIST dataset
(x_train, y_train), (x_test, y_test) = emnist.load_data()

2. Preprocessing the Data

Ensure the input data is normalized and reshaped for the CNN:

# Normalize images to [0, 1] range
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# Reshape for CNN (batch_size, height, width, channels)
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)

3. Building the CNN Model

We use a CNN architecture to train the model. Here's an example architecture using TensorFlow/Keras:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# Build the CNN model
model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    MaxPooling2D((2, 2)),
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D((2, 2)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(26, activation='softmax')  # 26 classes for A-Z
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

4. Train the Model

model.fit(x_train, y_train, epochs=10, batch_size=64, validation_split=0.2)
model.save('character_recognition_model.h5')

Evaluation

After training the model, evaluate its performance using test data:

# Evaluate the model on test data
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test Accuracy: {test_acc * 100:.2f}%")

Technologies

This project is built using the following technologies:

  • Python 3.x
  • TensorFlow/Keras: For building the CNN model
  • OpenCV: For image processing (e.g., resizing and grayscale conversion)
  • NumPy: For numerical operations and data handling
  • Matplotlib: For visualizing training performance and images
  • scikit-learn: For additional evaluation metrics (accuracy, precision, recall)
  • EMNIST / HWDB Dataset: Public datasets for handwritten character recognition

Contributing

We welcome contributions to improve this project. You can contribute by:

  • Improving the model architecture (e.g., using transfer learning or more advanced models).
  • Adding new functionalities such as word recognition or real-time character recognition from a webcam.
  • Enhancing the dataset by adding more handwritten samples.

To contribute, please follow these steps:

  1. Fork the repository
  2. Create a new branch (git checkout -b feature-name)
  3. Commit your changes (git commit -am 'Add feature')
  4. Push to the branch (git push origin feature-name)
  5. Create a new pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.


Customizing the Template:

  • Dataset: If you're using a different dataset, make sure to update the dataset section accordingly.
  • Model Architecture: If you change the model architecture, make sure to update the training and evaluation steps.
  • Additional Features: If you add real-time recognition or extend the model to recognize words, include the relevant instructions in the Usage section.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages