/
Utils.cs
257 lines (219 loc) · 7.36 KB
/
Utils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum Flip {
NONE,
VERTICAL,
HORIZONTAL
}
public static class Utils
{
private static Color32[] rotateSquare(Color32[] arr, double phi, Texture2D tex)
{
int x;
int y;
int i;
int j;
double sn = Math.Sin(phi);
double cs = Math.Cos(phi);
Color32[] arr2 = tex.GetPixels32();
int W = tex.width;
int H = tex.height;
int xc = W / 2;
int yc = H / 2;
for (j = 0; j < H; j++)
{
for (i = 0; i < W; i++)
{
arr2[j * W + i] = new Color32(0, 0, 0, 0);
x = (int)(cs * (i - xc) + sn * (j - yc) + xc);
y = (int)(-sn * (i - xc) + cs * (j - yc) + yc);
if ((x > -1) && (x < W) && (y > -1) && (y < H))
{
arr2[j * W + i] = arr[y * W + x];
}
}
}
return arr2;
}
public static Color32[] RotateImage(Texture2D tex, int angle)
{
Color32[] pix1 = tex.GetPixels32();
int W = tex.width;
int H = tex.height;
int x = 0;
int y = 0;
Color32[] pix2 = rotateSquare(pix1, (Math.PI / 180 * (double)angle), tex);
for (int j = 0; j < H; j++)
{
for (var i = 0; i < W; i++)
{
pix1[x + i + W * (j + y)] = pix2[i + j * W];
}
}
//tex.SetPixels32(pix1);
//tex.Apply();
//return tex.GetPixels32();
return pix1;
}
public static Color32[] GetPixels(Texture2D tex, int width, int height, int angle, Flip flip)
{
Rect texR = new Rect(0, 0, width, height);
tex.filterMode = FilterMode.Trilinear;
tex.Apply(true);
RenderTexture rtt = new RenderTexture(width, height, 32);
Graphics.SetRenderTarget(rtt);
switch(flip)
{
case Flip.VERTICAL:
GL.LoadPixelMatrix(0, 1, 0, 1);
break;
case Flip.HORIZONTAL:
GL.LoadPixelMatrix(1, 0, 1, 0);
break;
default:
GL.LoadPixelMatrix(0, 1, 1, 0);
break;
}
GL.Clear(true, true, new Color(0, 0, 0, 0));
Graphics.DrawTexture(new Rect(0, 0, 1, 1), tex);
tex.Resize(width, height);
tex.ReadPixels(texR, 0, 0, true);
tex.Apply(true);
var pixels = RotateImage(tex, angle);
UnityEngine.Object.Destroy(tex);
tex = null;
UnityEngine.Object.Destroy(rtt);
rtt = null;
return pixels;
}
public static byte[] DecodeTexture(Texture2D texture, int width, int height, int angle, Flip flip)
{
var pixels = GetPixels(texture, width, height, angle, flip);
int pixel = 0;
int index = 0;
byte[] imgData = new byte[height * width * 3];
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
var color = pixels[pixel++];
imgData[index++] = color.r;
imgData[index++] = color.g;
imgData[index++] = color.b;
}
}
return imgData;
}
public static float[] DecodeTexture(Texture2D texture, int width, int height,
float mean, float std, int angle, Flip flip)
{
var pixels = GetPixels(texture, width, height, angle, flip);
int pixel = 0;
int index = 0;
float[] imgData = new float[height * width * 3];
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
var color = pixels[pixel++];
imgData[index++] = (color.r - mean) / std;
imgData[index++] = (color.g - mean) / std;
imgData[index++] = (color.b - mean) / std;
}
}
return imgData;
}
public static void Log(List<Dictionary<string, object>> results)
{
results.ForEach(result =>
{
var confidence = result["confidenceInClass"];
var detectedClass = result["detectedClass"];
var rect = (Dictionary<string, float>)result["rect"];
Debug.Log($"{detectedClass} - {confidence} - {rect["x"]} - {rect["y"]} - {rect["w"]} - {rect["h"]}");
});
}
public static void Log(List<KeyValuePair<string, float>> results)
{
results.ForEach(result =>
{
Debug.Log($"{result.Key} - {result.Value}");
});
}
static List<KeyValuePair<Color, Texture2D>> textures = new List<KeyValuePair<Color, Texture2D>>();
public static void DrawRect(Rect area, int frameWidth, Color color)
{
var tmp = textures.Find(t => t.Key == color);
Texture2D texture;
if (tmp.Equals(default(KeyValuePair<Color, Texture2D>)))
{
texture = new Texture2D(1, 1);
texture.SetPixel(0, 0, color);
texture.Apply();
textures.Add(new KeyValuePair<Color, Texture2D>(color, texture));
}
else
{
texture = tmp.Value;
}
Rect lineArea = area;
lineArea.height = frameWidth; //Top line
GUI.DrawTexture(lineArea, texture);
lineArea.y = area.yMax - frameWidth; //Bottom
GUI.DrawTexture(lineArea, texture);
lineArea = area;
lineArea.width = frameWidth; //Left
GUI.DrawTexture(lineArea, texture);
lineArea.x = area.xMax - frameWidth;//Right
GUI.DrawTexture(lineArea, texture);
}
public static void DrawText(Rect area, string text, GUIStyle style)
{
GUI.Label(area, text, style);
}
public static void DrawOutput(IList outputs, int width, int height, Color color)
{
var list = outputs as List<Dictionary<string, object>>;
list.ForEach(output =>
{
var rect = output["rect"] as Dictionary<string, float>;
Utils.DrawRect(
new Rect(
rect["x"] * width,
rect["y"] * height,
rect["w"] * width,
rect["h"] * height),
5,
color);
var style = new GUIStyle();
style.fontSize = 50;
style.normal.textColor = color;
Utils.DrawText(
new Rect(
rect["x"] * width + 5,
rect["y"] * height + 5,
0,
0),
$"{output["detectedClass"]} - {output["confidenceInClass"]}",
style);
});
}
public static void DrawOutput(IList outputs, Vector2 position, Color color)
{
var list = outputs as List<KeyValuePair<string, float>>;
var cnt = 0;
list.ForEach(output =>
{
var style = new GUIStyle();
style.fontSize = 50;
style.normal.textColor = color;
Utils.DrawText(
new Rect(position.x, position.y + cnt * 70, 0, 0),
$"{output.Key} - {output.Value}",
style);
cnt++;
});
}
}