👉 Try the demo: https://github.com/LM-Kit/lm-kit-net-samples/tree/main/console_net/vision/background-removal/u2net_background_remover
On-Device Background Removal for C# .NET Applications
🎯 Purpose of the Demo
An interactive console app that cuts out the foreground subject and writes a transparent-background PNG. The U2-Net segmentation model loads once at startup, then the menu offers single-image and folder modes.
All processing runs on-device.
👥 Industry Target Audience
- E-commerce product photography: bulk-cut catalog photos.
- Identity / KYC: clean up document selfies before downstream analysis.
- Marketing automation: retouch creatives at scale without per-image cost.
- Photo apps: power "remove background" features on user devices.
- Compliance: keep customer photos local instead of sending to a cloud API.
🚀 Problem Solved
Cloud background-removal APIs charge per image, require uploading customer photos, and add latency. LM-Kit ships the U2-Net model and a clean API surface: load, call, save. The demo turns it into a prompt-driven app that scales from a single test image to a folder of thousands.
💻 Application Overview
Interactive menu (no command-line arguments) with two modes:
| Mode | What it does |
|---|---|
| File | Cut out a single image. Prompts for input and output path. |
| Folder | Process every supported image (.png, .jpg, .jpeg, .bmp, .webp, .tif, .tiff) in a folder. Output goes to a chosen directory. |
| Quit | Exit. |
The U2-Net model loads once at startup. Each cut-out reports elapsed milliseconds.
✨ Key Features
LM.LoadFromModelID("u2net")for the segmentation model.BackgroundDetection(model)engine.RemoveBackground(image)(sync) andRemoveBackgroundAsync(image)(async).ImageBuffer.LoadAsRGB/SaveAsPnground-trip.
🧠 Model
u2net(bundled segmentation model; ~170 MB, CPU-friendly).
🛠️ Getting Started
📋 Prerequisites
- .NET 8.0 or later
▶️ Running the Application
git clone https://github.com/LM-Kit/lm-kit-net-samples
cd lm-kit-net-samples/console_net/vision/background-removal/u2net_background_remover
dotnet run
Pick a mode from the menu and follow the prompts.