Detection


This document explains how to use SaigieVision to 'Detect' an Object in the form of a rectangle in an image.
If you have gone through the 'Classification' provided earlier, you can understand 'Detection' without difficulty because 'Detection' differs only in 'Inspection Option' and 'Report'.

Below is sample code for 'Detection'.

//Initialize
SaigeVision.Core.SaigeVisionApp.Init();

//Prepare
var gpuService = new NvGpuService();
gpuService.DetectConnectedGPUs();

SrImage data = new SrImage("../../../sample.png");
var modelUri = new Uri(@".../../../model.SrDet");

//Inspect
using (DetectionEngine engine = EngineLoader.LoadFrom<DetectionEngine>(modelUri))
{
    DetInspectionOptions option = engine.ReadInspectionOptions();
    option.TimeLimit = TimeSpan.FromSeconds(5);
    option.MaxNumberOfBoxes[0] = 0;
    option.MaxNumberOfBoxes[1] = 5;

    engine.AllocateNetwork(gpuService.CurrentDetectedGPUs[0], option);

    DetectionReport report = engine.Inspect(data);

    Console.WriteLine($"Number of detected boxes: {report.Prediction.Count}");
    foreach (var box in report.Prediction)
    {
        Console.WriteLine("------------------------");
        Console.WriteLine($"Labeled class name: {box.Class.Name}");
        Console.WriteLine($"Bounds: {box.Bounds}");
        Console.WriteLine($"Score: {box.Score}");
    }
}

//Clean up (optional)
data.DisposeAll();

Initialize

In order to use SaigeVision.Core, you must initialize SaigeVision.Core with SaigeVisionApp.Init() once at runtime.

It is recommended that you do your initialization in your application's main()

If you're using a platform other than a Console App and have difficulty finding the main() function (WPF, WinForms, etc...), you can use any location, but preferably one that guarantees a "onc and only" call.

It should also be before using the EngineLoader

Prepare

This is the process of preparing all the data needed to use DetectionEngine. In general, we require three things

  • GPU to use for the inference
  • Sample images to use for inference
  • A model (.srDet file) to use for inference

SaigeVision only supports GPUs made by NVIDIA

Also, SaigeVision is using all image objects through SrImage. SrImage has done a lot of work under the hood to optimize performance through .Net tools like ArrayPool<T>.

It supports a variety of constructors, so be sure to check out the API docs for SrImage.

Inspection

The 'Inspection' part starts by loading the DetectionEngine via the EngineLoader.

For the 'Inspection' part, we'll break down the procedure as follows to make it easier to understand.


1. Load engine

This example uses the C# language's using statement for safety. If you don't think the using statement is appropriate in your scenario, you can ignore it. However, if you do not use the using statement, the DetectionEngine must be managed and DetectionEngine.Dispose() when it is finished using it.

2. Inspection Options

The sample code is loading an Engine and then loading DetInspectionOptions through that Engine.

The DetectionEngine can specify a number of options when performing an inspection. These options affect the actual execution of the inspection and are reflected in the report.

In this example, we are assigning an option value to 'Additional score' as shown in the code that follows.

The options created (DetInspectionOptions) can be set via Engine's AllocateNetowrk().

  • The result of any 'Inspection' after setting is the value with that Option applied.


In certain scenarios, you may need to record these Option values in the model. LoadInspectionOptions() is a function that loads the most recent Option values recorded in the model.

If you want the options to be written to the model, use WirteInspectionOptions(). You can also revert to the default options with WriteInspectionOptionsAsDefault().

  • If you don't specify Options via AllocateNetowrk(), it will automatically use the Options recorded in the Model.

  • AllocateNetowrk() can take some time. This is usually enough to cause a thread to block.


3. Inspect

As you can see in the code, the main function that performs the 'Inspection' is engine.Inspect(). All kinds of Engines have this common Inspect() function. They are also nested in different forms for different uses. They can be optionally used in different scenarios, such as asynchronously or to inspect multiple images.

The DetectionReport reports all the results returned by Inspect().


Clean up (Optional)

In this example, we're Dispose() of all the SrImage we've used for inspections because we no longer have a use for them.



See also