-
Notifications
You must be signed in to change notification settings - Fork 2
/
OpenVinoYolov8Classification.cs
executable file
·71 lines (67 loc) · 2.88 KB
/
OpenVinoYolov8Classification.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using OpenCvSharp;
using Sdcb.OpenVINO;
using Sdcb.OpenVINO.Extensions.OpenCvSharp4;
using System.Xml.Linq;
using System.Xml.XPath;
namespace OpenVinoYOLO
{
public class OpenVinoYolov8Classification
{
string[] classes { get; set; }
Model model { get; set; }
PrePostProcessor prePostProcessor { get; set; }
PreProcessInputInfo preProcessInputInfo { get; set; }
CompiledModel compiledModel { get; set; }
InferRequest inferRequest { get; set; }
Shape inputShape { get; set; }
double inputWidthInv { get; set; }
double inputHeightInv { get; set; }
double _255_inv = 1.0 / 255.0;
public OpenVinoYolov8Classification(string model_xml_path, bool use_gpu)
{
classes = XDocument.Load(model_xml_path).XPathSelectElement(@"/net/rt_info/model_info/labels")!.Attribute("value")!.Value.Split(" ");
model = OVCore.Shared.ReadModel(model_xml_path);
prePostProcessor = model.CreatePrePostProcessor();
preProcessInputInfo = prePostProcessor.Inputs.Primary;
preProcessInputInfo.TensorInfo.Layout = Layout.NHWC;
preProcessInputInfo.ModelInfo.Layout = Layout.NCHW;
model = prePostProcessor.BuildModel();
compiledModel = OVCore.Shared.CompileModel(model, use_gpu ? "GPU" : "CPU");
inferRequest = compiledModel.CreateInferRequest();
inputShape = model.Inputs.Primary.Shape;
inputWidthInv = 1.0 / inputShape[2];
inputHeightInv = 1.0 / inputShape[1];
}
public YoloClassifyPrediction Predict(Mat image)
{
using Mat resized = image.Resize(new OpenCvSharp.Size(inputShape[2], inputShape[1]));
Size2f sizeRatio = new(image.Width * inputWidthInv, image.Height * inputHeightInv);
using Mat F32 = new();
resized.ConvertTo(F32, MatType.CV_32FC3, _255_inv);
using Tensor input = F32.AsTensor();
inferRequest.Inputs.Primary = input;
inferRequest.Run();
using Tensor output = inferRequest.Outputs.Primary;
return IndexOfMax(output.GetData<float>());
}
public YoloClassifyPrediction IndexOfMax(ReadOnlySpan<float> data)
{
if (data.Length == 0) throw new ArgumentException("The provided data span is null or empty.");
int maxIndex = 0;
float maxValue = data[0];
for (int i = 1; i < data.Length; i++)
{
if (data[i] > maxValue)
{
maxValue = data[i];
maxIndex = i;
}
if (maxValue > .5f)
{
break;
}
}
return new(classes[maxIndex], maxIndex, maxValue);
}
}
}