using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
class Program
{
static void Main(string[] args)
{
// Path to your ONNX model
string modelPath = "human_detection.onnx";
// Path to the input image
string imagePath = "input_image.jpg";
try
{
// Load the image and preprocess it
var inputTensor = PreprocessImage(imagePath);
// Run inference
var results = RunInference(modelPath, inputTensor);
// Post-process the results (e.g., extract bounding boxes)
PostProcessResults(results);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
static DenseTensor<float> PreprocessImage(string imagePath)
{
// Load the image using System.Drawing
using (var image = new Bitmap(Image.FromFile(imagePath)))
{
// Resize the image to the model's expected input size (e.g., 416x416 for YOLO)
int targetWidth = 416;
int targetHeight = 416;
var resizedImage = new Bitmap(image, new Size(targetWidth, targetHeight));
// Create a tensor to hold the image data
var inputTensor = new DenseTensor<float>(new[] { 1, 3, targetHeight, targetWidth });
// Normalize pixel values (e.g., scale to [0, 1] or [-1, 1] depending on the model)
for (int y = 0; y < targetHeight; y++)
{
for (int x = 0; x < targetWidth; x++)
{
Color pixel = resizedImage.GetPixel(x, y);
// Assuming the model expects RGB input normalized to [0, 1]
inputTensor[0, 0, y, x] = pixel.R / 255.0f; // Red channel
inputTensor[0, 1, y, x] = pixel.G / 255.0f; // Green channel
inputTensor[0, 2, y, x] = pixel.B / 255.0f; // Blue channel
}
}
return inputTensor;
}
}
static List<NamedOnnxValue> RunInference(string modelPath, DenseTensor<float> inputTensor)
{
// Create an inference session
using (var session = new InferenceSession(modelPath))
{
// Get the input name of the model (you can find this using Netron)
var inputMeta = session.InputMetadata;
string inputName = inputMeta.Keys.First();
// Prepare the input data
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor(inputName, inputTensor)
};
// Run the inference
using (var results = session.Run(inputs))
{
return results.ToList();
}
}
}
static void PostProcessResults(List<NamedOnnxValue> results)
{
// Process the output (this depends on your model's output format)
// For human detection models, the output typically includes:
// - Bounding box coordinates (x, y, width, height)
// - Confidence scores
// - Class probabilities
foreach (var result in results)
{
Console.WriteLine($"Output name: {result.Name}");
// Example: Extract bounding boxes and confidence scores
var tensor = result.AsTensor<float>();
var dimensions = tensor.Dimensions.ToArray();
// Assuming the output is [batch_size, num_boxes, num_classes + 5] (YOLO format)
// The last dimension contains [x, y, width, height, confidence, class_probabilities...]
for (int i = 0; i < dimensions[1]; i++)
{
float confidence = tensor[0, i, 4]; // Confidence score
if (confidence > 0.5) // Threshold for detection
{
float x = tensor[0, i, 0];
float y = tensor[0, i, 1];
float width = tensor[0, i, 2];
float height = tensor[0, i, 3];
Console.WriteLine($"Detected human at: x={x}, y={y}, width={width}, height={height}, confidence={confidence}");
}
}
}
}
}