Computer vision · CLI
KNN image classifier in Rust
A binary image-classification prototype: preprocessing, sample persistence and nearest-neighbor prediction in a command-line tool.
Prototype
- Rust
- image
- ndarray
- SmartCore
- Serde
- Clap
My contribution
felpzw’s own project: CLI, normalized-pixel extraction, sample serialization and KNN integration. The reviewed public history attributes commits to felpzw; no team contribution is documented in this scope.
Problem
Explore a simple pipeline that takes two folders of labeled images and classifies a new image as GOOD or BAD. The demonstrated objective is connecting image processing and KNN in Rust without a web interface.
Context and scope
This is a command-line prototype with train and classify subcommands. The repository provides neither a reproducible evaluation dataset nor evidence of production use. Classes are labels supplied by whoever prepares the images, not an objective measure of quality.
Architecture
Clap reads arguments in main.rs. train.rs converts images to grayscale, resizes them with the Triangle filter and normalizes pixels to 0–1. ndarray organizes samples and labels; Serde/bincode writes dimensions, k and training data.
In predict.rs, the input image follows the same preprocessing. The program loads samples, rebuilds SmartCore’s KNN using Euclidean distance and predicts the class. The persisted file contains training data, not an already-fitted classifier.
- Labeled images · grayscale, resize and normalization
- Samples + labels + k · bincode file
- New image · Euclidean KNN → class 0/1
Decisions and trade-offs
Grayscale pixels and fixed dimensions simplify the representation. The trade-off is losing color and some spatial detail; there is no learned feature extraction.
Persisting samples, labels and parameters makes the flow easy to inspect, but file size grows with the training set. Every classify invocation rebuilds the classifier; the project does not measure that cost.
k and resolution are configurable through the CLI. This enables experimentation, but parameter choices require validation on separate data. These trade-offs are an analysis of the implementation, not benchmark results.
Tests and evidence
No automated tests, train/test split, confusion matrix or accuracy metrics were found in the reviewed code. Documentation lists unit tests as a future improvement. This was a static review: the commands below were not executed.
A next validation should use synthetic or authorized images, hold out samples from training and check preprocessing, persistence and invalid inputs. This is a test plan, not existing coverage.
Limitations
A prototype without accuracy validation. The output field named “confidence” prints the integer class label 0/1, not a calculated probability or confidence score. It must not be read as model performance.
Model construction/saving uses unwrap, and validation of k, dimensions and empty folders is incomplete. Files are opened before their extensions are checked. An x86_64-specific import in main.rs also needs review for other architectures.
The README lists dependency versions that differ from Cargo.toml; use the manifest and Cargo.lock to reproduce this revision. This study publishes no benchmark, hosted demo or clinical validation.
Explore the project
Read train.rs, model.rs and predict.rs in that order to follow the data lifecycle. Compare the output in main.rs with the integer returned by prediction.
To experiment, clone the repository at the indicated revision and use a Rust toolchain supporting edition 2024. These commands illustrate the existing CLI and may require prototype fixes. Prepare your own data/good and data/bad folders with synthetic or authorized images and a separate test image; the dataset is not included.
cargo run --locked -- train --good-images ./data/good --bad-images ./data/bad --output model.bin --width 64 --height 64 --k 3
cargo run --locked -- classify --model model.bin --image ./data/test_image.pngSources checked on 2026-09-25 · Content v1 · Revision 87924a3.