### Install OpenVINO C# API NuGet Packages Source: https://github.com/guojin-yan/openvino-csharp-api/blob/csharp3.2/README_EN.md Installs the necessary OpenVINO C# API package and the runtime package for Windows. For other platforms or devices, different runtime packages need to be installed. ```bash dotnet add package OpenVINO.CSharp.API dotnet add package OpenVINO.runtime.win (Install different runtime packages for different platforms/devices) ``` -------------------------------- ### Initialize OpenVINO Core and Load Dynamic Libraries in C# Source: https://github.com/guojin-yan/openvino-csharp-api/blob/csharp3.2/README_EN.md Demonstrates two methods for initializing the OpenVINO Core in C#: auto-loading via NuGet packages (recommended) and manual specification of the dynamic library path for custom installations on Linux/macOS. It highlights potential DllNotFoundException issues and their resolutions. ```csharp using OpenVinoSharp; // Method 1: Auto-load (Recommended) // Core will auto-load OpenVINO dynamic libraries, but you need to install runtime packages first: // NuGet: OpenVINO.runtime.win / OpenVINO.runtime.ubuntu / OpenVINO.runtime.macos etc. using var core = new Core(); // Method 2: Manually specify dynamic library path (Linux/macOS custom installation) // If runtime package not installed, or library not in default search path, initialize manually // Linux example: Ov.Initialize("/opt/intel/openvino/lib/openvino_c.so"); // Linux environment variable setup (add to ~/.bashrc for permanent effect): // export LD_LIBRARY_PATH=/opt/intel/openvino/lib:$LD_LIBRARY_PATH // Windows example: Ov.Initialize(".\dll\win-x64/openvino_c.dll"); ``` -------------------------------- ### Perform Asynchronous Inference with OpenVINO C# API Source: https://github.com/guojin-yan/openvino-csharp-api/blob/csharp3.2/README_EN.md Shows how to perform asynchronous inference using the OpenVINO C# API. It includes starting the asynchronous operation, waiting for its completion with a timeout, and retrieving the output tensor for further processing. ```csharp // Start async inference request.start_async(); // Wait for completion (with timeout) bool completed = request.wait_for(5000); // 5 second timeout if (completed) { var output = request.get_output_tensor(); // Process results... } ``` -------------------------------- ### Initialize OpenVINO Runtime and Manage Devices Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt Demonstrates how to initialize the OpenVINO Core runtime, retrieve version information, query available hardware devices, and configure device-specific properties. It also shows the proper method for resource cleanup using Core.shutdown(). ```csharp using OpenVinoSharp; using OpenVinoSharp.Internal; // Method 1: Default initialization (auto-loads native library) using var core = new Core(); // Method 2: Initialize with custom native library path (Linux/macOS) Ov.Initialize("/opt/intel/openvino/lib/openvino_c.so"); using var core2 = new Core(); // Get OpenVINO version information Version version = Ov.get_openvino_version(); Console.WriteLine($"OpenVINO Build: {version.buildNumber}"); Console.WriteLine($"Description: {version.description}"); // Get available devices List devices = core.get_available_devices(); foreach (var device in devices) { Console.WriteLine($"Available device: {device}"); var deviceVersion = core.get_versions(device); Console.WriteLine($" Version: {deviceVersion.Value.description}"); } // Set device properties core.set_property("CPU", "PERF_COUNT", "YES"); core.set_property("CPU", "CACHE_DIR", "./model_cache"); // Get device property string cacheDir = core.get_property("CPU", "CACHE_DIR"); // Shutdown and release resources Core.shutdown(); ``` -------------------------------- ### Build OpenVINO C# API Project Source: https://github.com/guojin-yan/openvino-csharp-api/blob/csharp3.2/README_EN.md Provides step-by-step instructions for building the OpenVINO C# API project. It includes cloning the repository, restoring dependencies, building the project in release mode, and packing the NuGet package. ```bash # Clone repository git clone https://github.com/guojin-yan/OpenVINO-CSharp-API.git cd OpenVINO-CSharp-API # Restore dependencies dotnet restore # Build project dotnet build -c Release # Pack NuGet package dotnet pack -c Release ``` -------------------------------- ### Configure Logging for OpenVINO C# API Source: https://github.com/guojin-yan/openvino-csharp-api/blob/csharp3.2/README_EN.md Explains how to configure logging for the OpenVINO C# API. It covers setting the minimum log level, enabling timestamps, and defining a custom log callback function for integrating with external logging frameworks like NLog or Serilog. ```csharp using OpenVinoSharp.Internal; // Set minimum log level OvLogger.MinLevel = LogLevel.DEBUG; // Enable timestamps OvLogger.EnableTimestamp = true; // Set custom log callback (integrate with NLog/Serilog, etc.) OvLogger.SetCallback((level, message) => { Console.WriteLine($"[{level}] {message}"); }); ``` -------------------------------- ### Load and Inspect Models Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt Illustrates how to load deep learning models from files or memory buffers using the read_model method. It covers various formats including OpenVINO IR, ONNX, and PaddlePaddle, and demonstrates how to inspect model metadata such as input/output sizes and names. ```csharp using OpenVinoSharp; using var core = new Core(); // Read model from OpenVINO IR format (XML + BIN) using var model = core.read_model("model.xml", "model.bin"); // Read model from ONNX format (BIN path is optional) using var onnxModel = core.read_model("model.onnx"); // Read model with weights tensor from memory byte[] xmlData = File.ReadAllBytes("model.xml"); using var weightsTensor = new Tensor(new Shape(new long[] { 1024 }), ElementType.U8); using var memoryModel = core.read_model(xmlData, weightsTensor); // Read file content as byte array byte[] modelBytes = Ov.content_from_file("model.xml"); // Get model information Console.WriteLine($"Model name: {model.get_friendly_name()}"); Console.WriteLine($"Input count: {model.get_inputs_size()}"); Console.WriteLine($"Output count: {model.get_outputs_size()}"); Console.WriteLine($"Is dynamic: {model.is_dynamic()}"); // Get input/output information using var input = model.input(0); Console.WriteLine($"Input name: {input.get_any_name()}"); using var output = model.output(0); Console.WriteLine($"Output name: {output.get_any_name()}"); ``` -------------------------------- ### Complete YOLO Object Detection Inference with OpenVINO C# API Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt Demonstrates the full inference pipeline for YOLO object detection using the OpenVINO C# API. This includes initializing OpenVINO, loading and compiling a model, preprocessing an image, running inference, and postprocessing the results to detect objects. It utilizes classes like Core, Model, CompiledModel, InferRequest, and Tensor for the process. ```csharp using OpenVinoSharp; using OpenVinoSharp.Internal; using System; using System.Collections.Generic; using System.Diagnostics; // Initialize OpenVINO using var core = new Core(); // Enable model caching for faster subsequent loads core.set_property("CPU", "CACHE_DIR", "./model_cache"); // Read and compile model using var model = core.read_model("yolov8n.xml"); using var compiled = core.compile_model(model, "CPU"); using var request = compiled.create_infer_request(); // Get input shape for preprocessing using var inputInfo = compiled.get_input(0); var inputShape = inputInfo.get_shape(); int inputHeight = (int)inputShape[2]; int inputWidth = (int)inputShape[3]; // Preprocess image (letterbox resize + normalize) float[] inputData = PreprocessImage("image.jpg", inputWidth, inputHeight, out float scale, out int offsetX, out int offsetY); // Set input and run inference var sw = Stopwatch.StartNew(); using (var inputTensor = request.get_input_tensor()) { inputTensor.set_data(inputData); } request.infer(); sw.Stop(); Console.WriteLine($"Inference time: {sw.ElapsedMilliseconds}ms"); // Get output and postprocess using var outputTensor = request.get_output_tensor(); var outputShape = outputTensor.shape; int numDetections = (int)outputShape[1]; int detectionLength = (int)outputShape[2]; float[] output = outputTensor.get_float_data(); var detections = new List(); for (int i = 0; i < numDetections; i++) { int idx = i * detectionLength; float confidence = output[idx + 4]; if (confidence < 0.25f) continue; float x1 = (output[idx + 0] - offsetX) / scale; float y1 = (output[idx + 1] - offsetY) / scale; float x2 = (output[idx + 2] - offsetX) / scale; float y2 = (output[idx + 3] - offsetY) / scale; int classId = (int)output[idx + 5]; detections.Add(new Detection { X = x1, Y = y1, Width = x2 - x1, Height = y2 - y1, Confidence = confidence, ClassId = classId }); } // Apply NMS and output resultsdetections = ApplyNMS(detections, 0.5f); Console.WriteLine($"Detected {detections.Count} objects"); foreach (var det in detections) { Console.WriteLine($" Class {det.ClassId}: {det.Confidence:P1} at ({det.X:F0}, {det.Y:F0})"); } ``` -------------------------------- ### C# Inference Code with OpenVINO C# API Source: https://github.com/guojin-yan/openvino-csharp-api/blob/csharp3.2/README_EN.md Demonstrates loading a model, creating an inference request, setting input data, executing inference, and retrieving output using the OpenVINO C# API. Supports .xml and .onnx model formats. ```csharp using OpenVinoSharp; // Load model (supports .xml/.onnx formats) using var core = new Core(); var model = core.compile_model("yolov8n.xml", "CPU"); // Create inference request and execute using var request = model.create_infer_request(); request.set_input_tensor(new Tensor(shape, imageData)); request.infer(); // Get detection results var output = request.get_output_tensor().get_data(); ``` -------------------------------- ### Utilize Object Pool for Batch Inference in C# Source: https://github.com/guojin-yan/openvino-csharp-api/blob/csharp3.2/README_EN.md Demonstrates how to use an `InferRequestPool` for efficient batch processing with the OpenVINO C# API. It covers creating the pool with specified initial and maximum sizes, and running inference by providing functions to set input tensors and process output tensors. ```csharp // Create inference request pool using var pool = new InferRequestPool(compiledModel, initialSize: 4, maxSize: 16); // Execute inference using object pool pool.RunInference( request => request.set_input_tensor(input), request => { var output = request.get_output_tensor(); ProcessResults(output); } ); ``` -------------------------------- ### Compile OpenVINO Model for Target Device (C#) Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt Compiles an OpenVINO model for a specified device, optimizing it for hardware execution. Supports default settings, automatic device selection, direct file compilation, and custom properties like performance counting and stream configuration. It also covers exporting and importing compiled models, and setting/getting compiled model properties. ```csharp using OpenVinoSharp; using System.Collections.Generic; using var core = new Core(); using var model = core.read_model("yolov8n.xml"); // Compile for CPU with default settings using var compiledCPU = core.compile_model(model, "CPU"); // Compile for AUTO device (automatic device selection) using var compiledAuto = core.compile_model(model); // Compile directly from file path using var compiledFromFile = core.compile_model("yolov8n.xml", "CPU"); // Compile with custom properties var properties = new Dictionary { { "PERF_COUNT", "YES" }, { "CACHE_DIR", "./cache" }, { "NUM_STREAMS", "2" } }; using var compiledWithProps = core.compile_model(model, "CPU", properties); // Get compiled model information Console.WriteLine($"Inputs: {compiledCPU.get_inputs_size()}"); Console.WriteLine($"Outputs: {compiledCPU.get_outputs_size()}"); // Export compiled model for later import compiledCPU.export_model("compiled_model.blob"); // Import previously exported model using var imported = core.import_model("compiled_model.blob", "CPU"); // Get/set compiled model properties compiledCPU.set_property("PERFORMANCE_HINT", "THROUGHPUT"); string hint = compiledCPU.get_property("PERFORMANCE_HINT"); ``` -------------------------------- ### Implement Zero-Copy Tensor Operations in C# Source: https://github.com/guojin-yan/openvino-csharp-api/blob/csharp3.2/README_EN.md Illustrates high-performance tensor operations using zero-copy techniques with `Span` in C#. This method allows direct access to the tensor's underlying memory, avoiding data copying for operations like in-place normalization. ```csharp // Use Span to directly access underlying memory, avoiding array copies Span data = tensor.get_span(); for (int i = 0; i < data.Length; i++) { data[i] = data[i] / 255.0f; // In-place normalization } ``` -------------------------------- ### Execute Inference Request (Synchronous & Asynchronous C#) Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt Handles inference execution using the InferRequest class, supporting both synchronous and asynchronous modes. Prepares input data, sets input tensors, performs inference, retrieves output tensors and results. Asynchronous inference includes callback notifications and a wait-for mechanism with cancellation. It also demonstrates the async/await pattern and retrieving profiling information. ```csharp using OpenVinoSharp; using System.Threading; using System.Threading.Tasks; using var core = new Core(); using var compiled = core.compile_model("yolov8n.xml", "CPU"); using var request = compiled.create_infer_request(); // Prepare input data (1 batch, 3 channels, 640x640) float[] inputData = new float[1 * 3 * 640 * 640]; // ... fill inputData with preprocessed image ... // Set input tensor using var inputTensor = request.get_input_tensor(); inputTensor.set_data(inputData); // Synchronous inference request.infer(); // Get output tensor and results using var outputTensor = request.get_output_tensor(); float[] results = outputTensor.get_float_data(); // Asynchronous inference with callback bool completed = false; request.set_callback(() => { Console.WriteLine("Inference completed!"); completed = true; }); request.start_async(); // Wait for completion with timeout (5 seconds) bool success = request.wait_for(5000); if (!success) { request.cancel(); } // Clear callback request.set_callback(null); // .NET 5+: Async/await pattern with CancellationToken using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); try { await request.infer_async(cts.Token); var asyncOutput = request.get_output_tensor(); // Process results... } catch (OperationCanceledException) { Console.WriteLine("Inference was cancelled"); } // Get performance profiling information ProfilingInfo[] profiling = request.get_profiling_info(); foreach (var info in profiling.OrderByDescending(p => p.real_time).Take(5)) { Console.WriteLine($"Layer: {info.node_name}, Time: {info.real_time}us, Type: {info.exec_type}"); } ``` -------------------------------- ### Configure Logging with OvLogger in C# Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt The OvLogger class provides thread-safe and configurable logging for the OpenVINO C# API. It supports setting minimum log levels, enabling/disabling formatting options like timestamps and prefixes, checking log level enablement, and setting custom callbacks for logging. ```csharp using OpenVinoSharp.Internal; // Set minimum log level OvLogger.MinLevel = LogLevel.DEBUG; // Show all logs OvLogger.MinLevel = LogLevel.WARNING; // Only warnings and errors // Enable/disable formatting options OvLogger.EnableTimestamp = true; OvLogger.EnableLevelPrefix = true; // Check if level is enabled (for expensive operations) if (OvLogger.IsDebugEnabled) { OvLogger.Debug($"Complex calculation: {ExpensiveOperation()}"); } // Log at different levels OvLogger.Debug("Debug message"); OvLogger.Info("Application started"); OvLogger.Warn("Configuration missing, using defaults"); OvLogger.Error("Operation failed: {0}", errorMessage); OvLogger.Fatal("Critical system failure"); // Formatted logging OvLogger.Info("Processed {0} items in {1}ms", count, elapsed); // Set custom callback for file logging OvLogger.SetCallback((level, message) => { File.AppendAllText("app.log", $"{DateTime.Now} [{level}] {message}\n"); }); // Clear callback OvLogger.ClearCallback(); // Enable native OpenVINO library logging OvLogger.EnableNativeCallback(); // Reset native callback OvLogger.ResetNativeCallback(); ``` -------------------------------- ### Perform Dynamic Model Reshaping Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt The reshape method modifies model input dimensions at runtime, enabling support for dynamic batch sizes or varying input resolutions before compilation. ```csharp using OpenVinoSharp; using System.Collections.Generic; using var core = new Core(); using var model = core.read_model("model.xml"); // Check if model has dynamic shapes if (model.is_dynamic()) { Console.WriteLine("Model has dynamic input shapes"); } // Reshape single input by name using var newShape = new Shape(new long[] { 1, 3, 640, 640 }); model.reshape("input", newShape); // Reshape using dimension array model.reshape("input", new long[] { 4, 3, 224, 224 }); // Reshape all inputs to same shape model.reshape(newShape); // Batch reshape multiple inputs var shapes = new Dictionary { { "input1", new Shape(new long[] { 1, 3, 224, 224 }) }, { "input2", new Shape(new long[] { 1, 256 }) } }; model.reshape(shapes); // Compile reshaped model using var compiled = core.compile_model(model, "CPU"); ``` -------------------------------- ### Manage Tensor Data Containers Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt The Tensor class handles multi-dimensional array data for model inputs and outputs. It supports efficient data transfer using .NET Span and Memory types for zero-copy operations. ```csharp using OpenVinoSharp; using System; // Create tensor from shape and element type using var shape = new Shape(new long[] { 1, 3, 640, 640 }); using var tensor = new Tensor(shape, ElementType.F32); // Create tensor with initial data float[] imageData = new float[1 * 3 * 640 * 640]; using var dataTensor = new Tensor(shape, imageData); // Get tensor properties Console.WriteLine($"Shape: {tensor.shape}"); Console.WriteLine($"Size: {tensor.size} elements"); Console.WriteLine($"Byte size: {tensor.byte_size} bytes"); Console.WriteLine($"Element type: {tensor.element_type}"); // Set data using arrays float[] input = new float[tensor.size]; tensor.set_data(input); // .NET 5+: Zero-copy using Span Span dataSpan = tensor.get_span(); for (int i = 0; i < dataSpan.Length; i++) { dataSpan[i] = dataSpan[i] / 255.0f; // In-place normalization } // .NET 5+: Set data from ReadOnlySpan (zero-copy) ReadOnlySpan inputSpan = imageData.AsSpan(); tensor.set_data(inputSpan); // Get data as typed arrays float[] floatData = tensor.get_float_data(); byte[] byteData = tensor.get_byte_data(); int[] intData = tensor.get_int_data(); // Get generic typed data float[] genericData = tensor.get_data((int)tensor.size); // Zero-allocation: get data into existing buffer Span buffer = stackalloc float[1000]; tensor.get_data_to(buffer); // Get raw data pointer for advanced scenarios IntPtr dataPtr = tensor.data(); ``` -------------------------------- ### Manage Inference Requests with InferRequestPool in C# Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt The InferRequestPool class optimizes high-concurrency scenarios by managing a pool of reusable inference requests. It reduces allocation overhead and supports synchronous, non-blocking, and asynchronous request retrieval, along with convenience methods for running inferences. ```csharp using OpenVinoSharp; using System.Threading.Tasks; using var core = new Core(); using var model = core.read_model("model.xml"); using var compiled = core.compile_model(model, "CPU"); // Create pool with initial and max sizes using var pool = new InferRequestPool(compiled, initialSize: 4, maxSize: 16); Console.WriteLine($"Pool size: {pool.Count}"); Console.WriteLine($"Available: {pool.AvailableCount}"); // Synchronous rent/return pattern var request = pool.Rent(); try { using var input = request.get_input_tensor(); input.set_data(new float[1 * 3 * 640 * 640]); request.infer(); using var output = request.get_output_tensor(); // Process results... } finally { pool.Return(request); // Always return to pool } // Non-blocking try rent if (pool.TryRent(out var req)) { try { /* use req */ } finally { pool.Return(req); } } else { Console.WriteLine("Pool exhausted, try again later"); } // Async rent with cancellation using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); var asyncRequest = await pool.RentAsync(cts.Token); try { /* use asyncRequest */ } finally { pool.Return(asyncRequest); } // Convenience method: auto rent/return pool.RunInference( request => request.set_input_tensor(inputTensor), request => { var output = request.get_output_tensor(); // Process output... } ); // Async convenience method await pool.RunInferenceAsync( request => request.set_input_tensor(inputTensor), request => { /* process output */ } ); // Clear pool (dispose all requests) pool.Clear(); ``` -------------------------------- ### Define Tensor Dimensions with Shape Source: https://context7.com/guojin-yan/openvino-csharp-api/llms.txt The Shape class defines the structure of tensors, offering factory methods for standard image formats like NCHW and NHWC. It supports indexer access and dimension retrieval. ```csharp using OpenVinoSharp; // Create shape from dimension array using var shape = new Shape(new long[] { 1, 3, 640, 640 }); // Use factory methods for common formats using var nchw = Shape.nchw(1, 3, 224, 224); using var nhwc = Shape.nhwc(1, 224, 224, 3); using var chw = Shape.chw(3, 640, 640); using var hwc = Shape.hwc(640, 640, 3); using var hw = Shape.hw(480, 640); // Create from int array using var intShape = Shape.FromIntArray(new int[] { 1, 3, 224, 224 }); // Access dimensions using indexer long batch = shape[0]; long channels = shape[1]; long height = shape[2]; long width = shape[3]; // Get all dimensions long[] dims = shape.get_dims(); long rank = shape.get_rank(); long totalElements = shape.get_total_elements(); // String representation Console.WriteLine(shape.ToString()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.