https://github.com/mrnhtyzgld/onnx_cpp_inference
C++ demo that loads a model, reads an image and a mask, runs inference, and saves the result as a PNG with a timestamped filename. Works with single-image input and supports either 1-channel or 3-channel outputs.
https://github.com/mrnhtyzgld/onnx_cpp_inference
cpp inference onnx
Last synced: about 2 months ago
JSON representation
C++ demo that loads a model, reads an image and a mask, runs inference, and saves the result as a PNG with a timestamped filename. Works with single-image input and supports either 1-channel or 3-channel outputs.
- Host: GitHub
- URL: https://github.com/mrnhtyzgld/onnx_cpp_inference
- Owner: mrnhtyzgld
- Created: 2025-09-19T12:44:40.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-09-19T13:32:12.000Z (10 months ago)
- Last Synced: 2025-09-19T14:56:53.262Z (10 months ago)
- Topics: cpp, inference, onnx
- Language: C++
- Homepage:
- Size: 883 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ONNX Runtime + OpenCV Inference (LaMa) — C++ Mini Demo
Small C++ example that loads a model, reads an image and a mask, runs inference, and saves the result as a timestamped PNG. Works with single-image input and supports either 1-channel or 3-channel outputs.
## Prerequisites
- C++
- OpenCV (core, imgcodecs, dnn)
- ONNX Runtime (C++ API)
## Features
- Minimal single-file C++ demo
- Image + mask preprocessing with OpenCV `blobFromImage`
- Zero-copy tensor wrapping for inputs
- ONNX Runtime inference with graph optimizations
- Output handling for `C=1` (grayscale) and `C=3` (color)
- Safe float→8-bit scaling and PNG export (timestamped)
- Basic input/output shape logging and validation
- Error handling with detailed ORT exception messages
## Build
Ensure CMake can locate both OpenCV and ONNX Runtime. If ONNX Runtime isn’t globally discoverable, pass `-Donnxruntime_DIR=/path/to/onnxruntime/cmake` (directory containing `onnxruntime-config.cmake`). For OpenCV, pass `-DOpenCV_DIR=/path/to/opencv`.
```bash
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release \
-Donnxruntime_DIR=/path/to/onnxruntime/cmake \
-DOpenCV_DIR=/path/to/opencv ..
cmake --build . --config Release
```
## Run
Place files here:
```
./models/lama_fp32.onnx
./images/input_image.jpg
./images/dilated_mask.png
```
Then run:
```
./onnx_inference
```
Console will print input/output shapes and save PNG(s) under "./outputs/" with names like:
```
output_HHMMSS_0.png
```