Small Streamlit app that lets you draw a digit (0–9) and predicts it using a KNN model.
- app.py — Streamlit frontend
- knn_digit_model.joblib — trained KNN model (expected in project root)
- scaler.joblib — feature scaler used before prediction (expected in project root)
- requirements.txt — Python dependencies
Install dependencies (Windows):
python -m venv .venv
.\.venv\Scripts\Activate
pip install -r requirements.txtIf opencv-python fails to install, ensure you are using a supported Python version and run:
pip install opencv-pythonFrom project root:
.\.venv\Scripts\Activate
streamlit run app.pyOpen the displayed local URL in your browser.
- Draw a digit (0–9) on the canvas.
- The app preprocesses the canvas image to an 8x8 feature vector, scales it with
scaler.joblib, and predicts withknn_digit_model.joblib. - The predicted digit is shown below the canvas.
- Make sure
knn_digit_model.joblibandscaler.joblibare in the same folder asapp.py. If you used different filenames or a subfolder, update the paths inapp.py. - The canvas returns an RGBA image; the app converts it to grayscale before resizing. If predictions seem wrong, you may need to adjust preprocessing (resize method, inversion/scaling) to match how the model was trained.
- If you get errors loading the joblib files, confirm they were saved with the same scikit-learn / joblib versions used here.
- Use
opencv-python(notcv2-python) in requirements if installation issues occur.
If you want to retrain using scikit-learn's digits dataset:
- Load dataset from
sklearn.datasets.load_digits() - Preprocess (flatten 8x8 images), fit a
StandardScaler, then aKNeighborsClassifier - Save with joblib:
import joblib
joblib.dump(model, "knn_digit_model.joblib")
joblib.dump(scaler, "scaler.joblib")MIT