Building an Automatic Math Problem Grading System with Python and Convolutional Neural Networks
This tutorial explains how to generate synthetic digit images, train a CNN model to recognize handwritten numbers and operators, segment scanned math worksheets using projection techniques, evaluate each expression with Python's eval, and overlay the results on the original image to provide automatic grading feedback.
The article describes a complete workflow for creating an automatic grading tool for handwritten arithmetic problems using Python.
Data Generation : Synthetic images of digits and operators are generated by rendering characters with various fonts and rotations, saved in a structured folder hierarchy (15 classes × 13 fonts × 20 angles), each image sized 24×24 pixels.
Model Construction : A TensorFlow/Keras Sequential model is built with a Rescaling layer, two Conv2D‑MaxPooling blocks, a Flatten layer, and two Dense layers (128 units and 15‑class output). The model is compiled with Adam optimizer and sparse categorical cross‑entropy loss.
Training : The generated dataset is loaded using tf.keras.preprocessing.image_dataset_from_directory , cached, shuffled, and pre‑fetched. The model is trained for 10 epochs, achieving near‑100% accuracy, and the weights are saved to a checkpoint.
Prediction : Individual digit images are loaded, the trained model predicts class probabilities, and the highest‑probability class is selected as the recognized character.
Image Segmentation : Large scanned worksheets are binarized and inverted, then vertical and horizontal projections are computed to locate rows and blocks. Functions img_y_shadow and img_x_shadow calculate pixel counts per line/column, and img2rows , row2blocks , block2chars convert projection data into bounding boxes for cropping.
Recognition of Full Worksheets : Cropped character images are fed to the CNN model in batches, producing recognized strings for each arithmetic expression.
Evaluation and Feedback : Recognized characters are assembled into expressions, evaluated with eval (after replacing multiplication and division symbols), and compared to the provided answer. The result (✓, ✗, or missing) is drawn onto the original image using cv2ImgAddText with color coding (green for correct, red for incorrect, gray for missing).
The final output image displays the original worksheet with overlaid grading symbols, demonstrating a practical application of computer vision, deep learning, and Python scripting for educational automation.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.