Skip to content

Commit 43160e7

Browse files
committed
v1.0.0
0 parents  commit 43160e7

File tree

100 files changed

+22447
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+22447
-0
lines changed

Assets/InferenceEngineWithOpenCVForUnity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/InferenceEngineWithOpenCVForUnity/Face.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using System;
2+
using System.Globalization;
3+
using Unity.InferenceEngine;
4+
using Unity.Mathematics;
5+
using UnityEngine;
6+
7+
namespace InferenceEngineWithOpenCVForUnity.Face
8+
{
9+
10+
public static class BlazeUtils
11+
{
12+
// matrix utility
13+
public static float2x3 mul(float2x3 a, float2x3 b)
14+
{
15+
return new float2x3(
16+
a[0][0] * b[0][0] + a[1][0] * b[0][1],
17+
a[0][0] * b[1][0] + a[1][0] * b[1][1],
18+
a[0][0] * b[2][0] + a[1][0] * b[2][1] + a[2][0],
19+
a[0][1] * b[0][0] + a[1][1] * b[0][1],
20+
a[0][1] * b[1][0] + a[1][1] * b[1][1],
21+
a[0][1] * b[2][0] + a[1][1] * b[2][1] + a[2][1]
22+
);
23+
}
24+
25+
public static float2 mul(float2x3 a, float2 b)
26+
{
27+
return new float2(
28+
a[0][0] * b.x + a[1][0] * b.y + a[2][0],
29+
a[0][1] * b.x + a[1][1] * b.y + a[2][1]
30+
);
31+
}
32+
33+
public static float3 mul(float2x3 a, float3 b)
34+
{
35+
return new float3(
36+
a[0][0] * b.x + a[1][0] * b.y + a[2][0],
37+
a[0][1] * b.x + a[1][1] * b.y + a[2][1],
38+
b.z
39+
);
40+
}
41+
42+
public static float2x3 RotationMatrix(float theta)
43+
{
44+
var sinTheta = math.sin(theta);
45+
var cosTheta = math.cos(theta);
46+
return new float2x3(
47+
cosTheta, -sinTheta, 0,
48+
sinTheta, cosTheta, 0
49+
);
50+
}
51+
52+
public static float2x3 TranslationMatrix(float2 delta)
53+
{
54+
return new float2x3(
55+
1, 0, delta.x,
56+
0, 1, delta.y
57+
);
58+
}
59+
60+
public static float2x3 ScaleMatrix(float2 scale)
61+
{
62+
return new float2x3(
63+
scale.x, 0, 0,
64+
0, scale.y, 0
65+
);
66+
}
67+
68+
// model filtering utility
69+
static FunctionalTensor ScoreFiltering(FunctionalTensor rawScores, float scoreThreshold)
70+
{
71+
return Functional.Sigmoid(Functional.Clamp(rawScores, -scoreThreshold, scoreThreshold));
72+
}
73+
74+
public static (FunctionalTensor, FunctionalTensor, FunctionalTensor) ArgMaxFiltering(FunctionalTensor rawBoxes, FunctionalTensor rawScores)
75+
{
76+
var detectionScores = ScoreFiltering(rawScores, 100f); // (1, 896, 1)
77+
var bestScoreIndex = Functional.ArgMax(rawScores, 1).Squeeze();
78+
79+
var selectedBoxes = Functional.IndexSelect(rawBoxes, 1, bestScoreIndex).Unsqueeze(0); // (1, 1, 16)
80+
var selectedScores = Functional.IndexSelect(detectionScores, 1, bestScoreIndex).Unsqueeze(0); // (1, 1, 1)
81+
82+
return (bestScoreIndex, selectedScores, selectedBoxes);
83+
}
84+
85+
public static (FunctionalTensor, FunctionalTensor, FunctionalTensor) NMSFiltering(FunctionalTensor rawBoxes, FunctionalTensor rawScores, FunctionalTensor anchors, float inputSize, float iouThreshold, float scoreThreshold)
86+
{
87+
var xCenter = rawBoxes[0, .., 0] + anchors[.., 0] * inputSize;
88+
var yCenter = rawBoxes[0, .., 1] + anchors[.., 1] * inputSize;
89+
90+
var widthHalf = 0.5f * rawBoxes[0, .., 2];
91+
var heightHalf = 0.5f * rawBoxes[0, .., 3];
92+
93+
var nmsBoxes = Functional.Stack(new[]
94+
{
95+
yCenter - heightHalf,
96+
xCenter - widthHalf,
97+
yCenter + heightHalf,
98+
xCenter + widthHalf
99+
}, 1);
100+
101+
var nmsScores = Functional.Squeeze(ScoreFiltering(rawScores, 100f));
102+
var selectedIndices = Functional.NMS(nmsBoxes, nmsScores, iouThreshold, scoreThreshold); // (N);
103+
104+
var selectedBoxes = Functional.IndexSelect(rawBoxes, 1, selectedIndices).Unsqueeze(0); // (1, N, 16)
105+
var selectedScores = Functional.IndexSelect(rawScores, 1, selectedIndices).Unsqueeze(0); // (1, N, 1)
106+
107+
return (selectedIndices, selectedScores, selectedBoxes);
108+
}
109+
110+
// image transform utility
111+
static ComputeShader s_ImageTransformShader = Resources.Load<ComputeShader>("ComputeShaders/ImageTransform");
112+
static int s_ImageSample = s_ImageTransformShader.FindKernel("ImageSample");
113+
static int s_Optr = Shader.PropertyToID("Optr");
114+
static int s_X_tex2D = Shader.PropertyToID("X_tex2D");
115+
static int s_O_height = Shader.PropertyToID("O_height");
116+
static int s_O_width = Shader.PropertyToID("O_width");
117+
static int s_O_channels = Shader.PropertyToID("O_channels");
118+
static int s_X_height = Shader.PropertyToID("X_height");
119+
static int s_X_width = Shader.PropertyToID("X_width");
120+
static int s_affineMatrix = Shader.PropertyToID("affineMatrix");
121+
122+
static int IDivC(int v, int div)
123+
{
124+
return (v + div - 1) / div;
125+
}
126+
127+
public static void SampleImageAffine(Texture srcTexture, Tensor<float> dstTensor, float2x3 M)
128+
{
129+
var tensorData = ComputeTensorData.Pin(dstTensor, false);
130+
131+
s_ImageTransformShader.SetTexture(s_ImageSample, s_X_tex2D, srcTexture);
132+
s_ImageTransformShader.SetBuffer(s_ImageSample, s_Optr, tensorData.buffer);
133+
134+
s_ImageTransformShader.SetInt(s_O_height, dstTensor.shape[1]);
135+
s_ImageTransformShader.SetInt(s_O_width, dstTensor.shape[2]);
136+
s_ImageTransformShader.SetInt(s_O_channels, dstTensor.shape[3]);
137+
s_ImageTransformShader.SetInt(s_X_height, srcTexture.height);
138+
s_ImageTransformShader.SetInt(s_X_width, srcTexture.width);
139+
140+
s_ImageTransformShader.SetMatrix(s_affineMatrix, new Matrix4x4(new Vector4(M[0][0], M[0][1]), new Vector4(M[1][0], M[1][1]), new Vector4(M[2][0], M[2][1]), Vector4.zero));
141+
142+
s_ImageTransformShader.Dispatch(s_ImageSample, IDivC(dstTensor.shape[1], 8), IDivC(dstTensor.shape[1], 8), 1);
143+
}
144+
145+
public static float[,] LoadAnchors(string csv, int numAnchors)
146+
{
147+
var anchors = new float[numAnchors, 4];
148+
var anchorLines = csv.Split('\n');
149+
150+
for (var i = 0; i < numAnchors; i++)
151+
{
152+
var anchorValues = anchorLines[i].Split(',');
153+
for (var j = 0; j < 4; j++)
154+
{
155+
anchors[i, j] = float.Parse(anchorValues[j], CultureInfo.InvariantCulture);
156+
}
157+
}
158+
159+
return anchors;
160+
}
161+
}
162+
}

Assets/InferenceEngineWithOpenCVForUnity/Face/BlazeUtils.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/InferenceEngineWithOpenCVForUnity/Face/Data.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)