This project implements a Convolutional Neural Network (CNN) to recognize handwritten digits (0–9) using the MNIST dataset. The model is trained to classify grayscale images of size 28×28 pixels and can also predict custom handwritten digit images.
- Load and preprocess MNIST dataset
- Build CNN model using TensorFlow and Keras
- Train and evaluate the model
- Achieve approximately 99% accuracy
- Predict individual test images
- Generate confusion matrix
- Test custom handwritten digit images
The project uses the MNIST dataset, which contains:
- 60,000 training images
- 10,000 testing images
- 28×28 grayscale images
- 10 classes (digits 0–9)
The CNN model consists of:
- Convolutional Layer (32 filters, 5×5 kernel, ReLU activation)
- MaxPooling Layer
- Dropout Layer (0.2)
- Flatten Layer
- Dense Layer (128 neurons, ReLU activation)
- Output Layer (Softmax activation)
- Python 3.10
- TensorFlow / Keras
- NumPy
- Matplotlib
- Scikit-learn
- OpenCV (for custom image testing)
- Reshape images to (28, 28, 1)
- Normalize pixel values from 0–255 to 0–1
- One-hot encode labels
- Resize and center custom images before prediction
model.fit(X_train, y_train,
validation_data=(X_test, y_test),
epochs=10,
batch_size=200)scores = model.evaluate(X_test, y_test)
print("Test Accuracy:", scores[1] * 100)The model achieves approximately 99% accuracy on test data.
Steps:
- Write a digit on white paper
- Capture and crop the image
- Convert to grayscale
- Resize to 28×28
- Normalize and reshape
- Predict using:
prediction = model.predict(image)
print("Predicted Digit:", np.argmax(prediction))Used to analyze classification performance:
from sklearn.metrics import confusion_matrixThis helps visualize model accuracy for each digit class.
- High accuracy (approximately 99%)
- Good generalization on test data
- Works with properly preprocessed custom images