VB Project Eye: Complete Guide to Building an Image Recognition App
Overview
VB Project Eye is a Visual Basic-based image recognition application that detects and classifies objects in images. This guide walks through planning, required tools, core concepts, step-by-step implementation, sample code snippets, testing, and deployment—so you can build a working image recognition app using Visual Basic (VB.NET) and modern machine learning libraries.
What you’ll build
- A desktop VB.NET app that loads images, runs a pre-trained image classification model, displays top predictions with confidence scores, and saves results.
- Optional features: webcam capture, batch processing, and simple training/transfer learning workflow.
Tools & libraries
- IDE: Visual Studio 2022 or later (Community edition is fine).
- Language: VB.NET (target .NET ⁄7 or later).
- ML runtime: ONNX Runtime (recommended) or ML.NET for easier .NET integration.
- Pre-trained model: MobileNetV2, ResNet50, or custom ONNX model trained for your classes. Use models from ONNX Model Zoo or export from PyTorch/TensorFlow to ONNX.
- Imaging: System.Drawing.Common or OpenCV (via Emgu CV) for image preprocessing and webcam support.
- Optional: NuGet packages: Microsoft.ML.OnnxRuntime, System.Drawing.Common, Emgu.CV (if using OpenCV), Newtonsoft.Json (for config/results).
Architecture & data flow
- UI (WinForms/WPF) — image selection, camera capture, and result display.
- Preprocessor — resize, normalize, and convert image to model input tensor.
- Inference engine — run ONNX model with ONNX Runtime and get raw outputs.
- Postprocessor — apply softmax, map indices to labels, pick top-N results.
- Storage — log results to JSON or CSV; optionally save annotated images.
Step-by-step implementation
1) Project setup
- Create a new VB.NET WinForms or WPF project in Visual Studio (.NET 6 or 7).
- Add NuGet packages:
- Microsoft.ML.OnnxRuntime
- System.Drawing.Common
- Newtonsoft.Json (optional)
- Emgu.CV (optional for webcam)
2) UI layout
- Main window components:
- PictureBox (or Image control) to show selected image.
- Button: “Load Image” — opens file dialog.
- Button: “Use Webcam” — starts webcam capture (optional).
- Button: “Run Recognition” — invokes inference.
- ListBox or Label area to show top predictions and confidence.
- ProgressBar or status label for processing state.
3) Load model and labels
- Place your ONNX model file (e.g., mobilenetv2.onnx) in the project or a known path.
- Load class labels from a text file (one label per line) into a string array.
Sample VB.NET snippet to load labels:
vb
Dim labels() As String = IO.File.ReadAllLines(“labels.txt”)
Load ONNX model session:
vb
Imports Microsoft.ML.OnnxRuntime Dim session As InferenceSession = New InferenceSession(“mobilenetv2.onnx”)
4) Image preprocessing
- Typical steps: load image → resize to model input (e.g., 224×224) → convert to float32 → normalize (mean/std) → reorder channels if needed → create tensor.
Example function to create input tensor (assume RGB, 224×224, float32):
vb
Imports System.Drawing Imports Microsoft.ML.OnnxRuntime.TensorsFunction ImageToTensor(img As Bitmap, targetW As Integer, targetH As Integer) As DenseTensor(Of Single)Dim resized As New Bitmap(img, New Size(targetW, targetH)) Dim tensor As New DenseTensor(Of Single)(New Integer() {1, 3, targetH, targetW}) For y As Integer = 0 To targetH - 1 For x As Integer = 0 To targetW - 1 Dim px As Color = resized.GetPixel(x, y) ' Normalize to 0-1 and optionally apply mean/std tensor(0, 0, y, x) = px.R / 255.0F tensor(0, 1, y, x) = px.G / 255.0F tensor(0, 2, y, x) = px.B / 255.0F Next Next Return tensorEnd Function
Adjust channel order/normalization according to your model.
5) Run inference
- Prepare input container and run:
vb
Imports Microsoft.ML.OnnxRuntime Imports Microsoft.ML.OnnxRuntime.TensorsDim inputName As String = session.InputMetadata.Keys.First() Dim tensor = ImageToTensor(myBitmap, 224, 224) Dim inputs = New List(Of NamedOnnxValue) From {
NamedOnnxValue.CreateFromTensor(Of Single)(inputName, tensor)} Using results = session.Run(inputs)
Dim outputName = session.OutputMetadata.Keys.First() Dim outputTensor = results.First().AsEnumerable(Of Single)().ToArray() ' process outputTensorEnd Using
6) Postprocessing
- Convert raw logits to probabilities using softmax, then map top indices to labels:
vb
Function Softmax(logits() As Single) As Single() Dim max = logits.Max()Dim exps = logits.Select(Function(l) Math.Exp(l - max)).ToArray() Dim sumExp = exps.Sum() Return exps.Select(Function(e) CType(e / sumExp, Single)).ToArray()End Function
- Pick top-5:
vb
Dim probs = Softmax(outputTensor) Dim topN = probs.Select(Function(p, i) New With {Key .Index = i, Key .Prob = p}) _.OrderByDescending(Function(x) x.Prob).Take(5)7) Display results
- Show labels and confidence in UI list, e.g., “Tabby cat — 93.5%”.
- Optionally draw bounding boxes (if using object detection model) or overlay text on image and save.
8) Webcam support (optional)
- Use Emgu.CV or OpenCV to capture frames, then run inference on each frame at a reduced frame rate for performance.
- Be careful to run inference on a background thread to keep UI responsive.
9) Performance tips
- Use a lightweight model (MobileNet, EfficientNet-lite) for real-time apps.
- Use GPU-accelerated ONNX Runtime if available (install CUDA/DirectML execution providers).
- Resize and batch inputs efficiently; reuse memory buffers where possible.
- Run inference on a background thread or Task to avoid freezing UI.
10) Optional: Transfer learning / custom training
- If you need custom classes, fine-tune a model in PyTorch/TensorFlow, export to ONNX, then use in VB app.
- For small datasets, use transfer learning on MobileNet/ResNet head; augment images and validate carefully.
Example project structure
- /models/mobilenetv2.onnx
- /labels/labels.txt
- /src/MainForm.vb (UI & event handlers)
- /src/Inference.vb (model load, preprocess, postprocess)
- /output/results.json (inference logs)
Testing & validation
- Validate with a held-out test set; compute accuracy, precision/recall where applicable.
- Test different lighting conditions and image sizes.
- Check model robustness to rotations, blurring, and occlusion.
Deployment
- Publish as a self-contained .NET desktop app (single-folder).
- Include model and label files or allow remote model updates via secure download.
- If distributing widely, provide clear instructions for installing GPU runtime if using GPU acceleration.
Troubleshooting
- Wrong predictions: check preprocessing (channel order, normalization).
- Poor performance: use smaller model or enable GPU execution provider.
- Model load errors: confirm ONNX ops compatibility with chosen runtime.
Next steps and enhancements
- Add object detection (e.g., YOLO/SSD ONNX models) for bounding boxes.
- Build a simple annotation tool to collect labeled images for retraining.
- Add cloud inference option (secure API) for heavier models if local resources are limited.
This guide gives a practical, end-to-end path to build VB Project Eye—an image recognition desktop app in VB.NET using ONNX Runtime. Use the code snippets and architecture notes as a foundation, adapt preprocessing to your model, and iterate on model choice for your accuracy/performance needs.
Leave a Reply