/
PhoneARCamera.cs
436 lines (372 loc) · 14.2 KB
/
PhoneARCamera.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using Unity.Barracuda;
using System.IO;
using TFClassify;
using System.Linq;
using System.Collections;
public class PhoneARCamera : MonoBehaviour
{
[SerializeField]
ARCameraManager m_CameraManager;
/// <summary>
/// Get or set the <c>ARCameraManager</c>.
/// </summary>
public ARCameraManager cameraManager
{
get => m_CameraManager;
set => m_CameraManager = value;
}
[SerializeField]
RawImage m_RawImage;
/// <summary>
/// The UI RawImage used to display the image on screen. (deprecated)
/// </summary>
public RawImage rawImage
{
get { return m_RawImage; }
set { m_RawImage = value; }
}
public enum Detectors{
Yolo2_tiny,
Yolo3_tiny
};
public Detectors selected_detector;
public Detector detector = null;
public float shiftX = 0f;
public float shiftY = 0f;
public float scaleFactor = 1;
public Color colorTag = new Color(0.3843137f, 0, 0.9333333f);
private static GUIStyle labelStyle;
private static Texture2D boxOutlineTexture;
// bounding boxes detected for current frame
private IList<BoundingBox> boxOutlines;
// bounding boxes detected across frames
public List<BoundingBox> boxSavedOutlines = new List<BoundingBox>();
// lock model when its inferencing a frame
private bool isDetecting = false;
// the number of frames that bounding boxes stay static
private int staticNum = 0;
public bool localization = false;
Texture2D m_Texture;
void OnEnable()
{
if (m_CameraManager != null)
{
m_CameraManager.frameReceived += OnCameraFrameReceived;
}
boxOutlineTexture = new Texture2D(1, 1);
boxOutlineTexture.SetPixel(0, 0, this.colorTag);
boxOutlineTexture.Apply();
labelStyle = new GUIStyle();
labelStyle.fontSize = 50;
labelStyle.normal.textColor = this.colorTag;
if (selected_detector == Detectors.Yolo2_tiny)
{
detector = GameObject.Find("Detector Yolo2-tiny").GetComponent<DetectorYolo2>();
}
else if (selected_detector == Detectors.Yolo3_tiny)
{
detector = GameObject.Find("Detector Yolo3-tiny").GetComponent<DetectorYolo3>();
}
else
{
Debug.Log("DEBUG: Invalid detector model");
}
this.detector.Start();
CalculateShift(this.detector.IMAGE_SIZE);
}
void OnDisable()
{
if (m_CameraManager != null)
{
m_CameraManager.frameReceived -= OnCameraFrameReceived;
}
}
public void OnRefresh()
{
Debug.Log("DEBUG: onRefresh, removing anchors and boundingboxes");
localization = false;
staticNum = 0;
// clear boubding box containers
boxSavedOutlines.Clear();
boxOutlines.Clear();
// clear anchor
AnchorCreator anchorCreator = FindObjectOfType<AnchorCreator>();
anchorCreator.RemoveAllAnchors();
}
unsafe void OnCameraFrameReceived(ARCameraFrameEventArgs eventArgs)
{
// Attempt to get the latest camera image. If this method succeeds,
// it acquires a native resource that must be disposed (see below).
XRCpuImage image;
if (!cameraManager.TryAcquireLatestCpuImage(out image))
{
return;
}
// Once we have a valid XRCameraImage, we can access the individual image "planes"
// (the separate channels in the image). XRCameraImage.GetPlane provides
// low-overhead access to this data. This could then be passed to a
// computer vision algorithm. Here, we will convert the camera image
// to an RGBA texture (and draw it on the screen).
// Choose an RGBA format.
// See XRCameraImage.FormatSupported for a complete list of supported formats.
var format = TextureFormat.RGBA32;
if (m_Texture == null || m_Texture.width != image.width || m_Texture.height != image.height)
{
m_Texture = new Texture2D(image.width, image.height, format, false);
}
// Convert the image to format, flipping the image across the Y axis.
// We can also get a sub rectangle, but we'll get the full image here.
var conversionParams = new XRCpuImage.ConversionParams(image, format, XRCpuImage.Transformation.None);
// Texture2D allows us write directly to the raw texture data
// This allows us to do the conversion in-place without making any copies.
var rawTextureData = m_Texture.GetRawTextureData<byte>();
try
{
image.Convert(conversionParams, new IntPtr(rawTextureData.GetUnsafePtr()), rawTextureData.Length);
}
finally
{
// We must dispose of the XRCameraImage after we're finished
// with it to avoid leaking native resources.
image.Dispose();
}
// Apply the updated texture data to our texture
m_Texture.Apply();
// If bounding boxes are static for certain frames, start localization
if (staticNum > 150)
{
localization = true;
}
else
{
// detect object and create current frame outlines
TFDetect();
// merging outliens across frames
GroupBoxOutlines();
}
// Set the RawImage's texture so we can visualize it.
m_RawImage.texture = m_Texture;
}
public void OnGUI()
{
// Do not draw bounding boxes after localization.
if (localization)
{
return;
}
if (this.boxSavedOutlines != null && this.boxSavedOutlines.Any())
{
foreach (var outline in this.boxSavedOutlines)
{
DrawBoxOutline(outline, scaleFactor, shiftX, shiftY);
}
}
}
// merging bounding boxes and save result to boxSavedOutlines
private void GroupBoxOutlines()
{
// if savedoutlines is empty, add current frame outlines if possible.
if (this.boxSavedOutlines.Count == 0)
{
// no bounding boxes in current frame
if (this.boxOutlines == null || this.boxOutlines.Count == 0)
{
return;
}
// deep copy current frame bounding boxes
foreach (var outline in this.boxOutlines)
{
this.boxSavedOutlines.Add(outline);
}
return;
}
// adding current frame outlines to existing savedOulines and merge if possible.
bool addOutline = false;
foreach (var outline1 in this.boxOutlines)
{
bool unique = true;
List<BoundingBox> itemsToAdd = new List<BoundingBox>();
List<BoundingBox> itemsToRemove = new List<BoundingBox>();
foreach (var outline2 in this.boxSavedOutlines)
{
// if two bounding boxes are for the same object, use high confidnece one
if (IsSameObject(outline1, outline2))
{
unique = false;
if (outline1.Confidence > outline2.Confidence + 0.05F) //& outline2.Confidence < 0.5F)
{
Debug.Log("DEBUG: add detected boxes in this frame.");
Debug.Log($"DEBUG: Add Label: {outline1.Label}. Confidence: {outline1.Confidence}.");
Debug.Log($"DEBUG: Remove Label: {outline2.Label}. Confidence: {outline2.Confidence}.");
itemsToRemove.Add(outline2);
itemsToAdd.Add(outline1);
addOutline = true;
staticNum = 0;
break;
}
}
}
this.boxSavedOutlines.RemoveAll(item => itemsToRemove.Contains(item));
this.boxSavedOutlines.AddRange(itemsToAdd);
// if outline1 in current frame is unique, add it permanently
if (unique)
{
Debug.Log($"DEBUG: add detected boxes in this frame");
addOutline = true;
staticNum = 0;
this.boxSavedOutlines.Add(outline1);
Debug.Log($"Add Label: {outline1.Label}. Confidence: {outline1.Confidence}.");
}
}
if (!addOutline)
{
staticNum += 1;
}
// merge same bounding boxes
// remove will cause duplicated bounding box?
List<BoundingBox> temp = new List<BoundingBox>();
foreach (var outline1 in this.boxSavedOutlines)
{
if (temp.Count == 0)
{
temp.Add(outline1);
continue;
}
List<BoundingBox> itemsToAdd = new List<BoundingBox>();
List<BoundingBox> itemsToRemove = new List<BoundingBox>();
foreach (var outline2 in temp)
{
if (IsSameObject(outline1, outline2))
{
if (outline1.Confidence > outline2.Confidence)
{
itemsToRemove.Add(outline2);
itemsToAdd.Add(outline1);
Debug.Log("DEBUG: merge bounding box conflict!!!");
}
}
else
{
itemsToAdd.Add(outline1);
}
}
temp.RemoveAll(item => itemsToRemove.Contains(item));
temp.AddRange(itemsToAdd);
}
this.boxSavedOutlines = temp;
}
// For two bounding boxes, if at least one center is inside the other box,
// treate them as the same object.
private bool IsSameObject(BoundingBox outline1, BoundingBox outline2)
{
var xMin1 = outline1.Dimensions.X * this.scaleFactor + this.shiftX;
var width1 = outline1.Dimensions.Width * this.scaleFactor;
var yMin1 = outline1.Dimensions.Y * this.scaleFactor + this.shiftY;
var height1 = outline1.Dimensions.Height * this.scaleFactor;
float center_x1 = xMin1 + width1 / 2f;
float center_y1 = yMin1 + height1 / 2f;
var xMin2 = outline2.Dimensions.X * this.scaleFactor + this.shiftX;
var width2 = outline2.Dimensions.Width * this.scaleFactor;
var yMin2 = outline2.Dimensions.Y * this.scaleFactor + this.shiftY;
var height2 = outline2.Dimensions.Height * this.scaleFactor;
float center_x2 = xMin2 + width2 / 2f;
float center_y2 = yMin2 + height2 / 2f;
bool cover_x = (xMin2 < center_x1) && (center_x1 < (xMin2 + width2));
bool cover_y = (yMin2 < center_y1) && (center_y1 < (yMin2 + height2));
bool contain_x = (xMin1 < center_x2) && (center_x2 < (xMin1 + width1));
bool contain_y = (yMin1 < center_y2) && (center_y2 < (yMin1 + height1));
return (cover_x && cover_y) || (contain_x && contain_y);
}
private void CalculateShift(int inputSize)
{
int smallest;
if (Screen.width < Screen.height)
{
smallest = Screen.width;
this.shiftY = (Screen.height - smallest) / 2f;
}
else
{
smallest = Screen.height;
this.shiftX = (Screen.width - smallest) / 2f;
}
this.scaleFactor = smallest / (float)inputSize;
}
private void TFDetect()
{
if (this.isDetecting)
{
return;
}
this.isDetecting = true;
StartCoroutine(ProcessImage(this.detector.IMAGE_SIZE, result =>
{
StartCoroutine(this.detector.Detect(result, boxes =>
{
this.boxOutlines = boxes;
Resources.UnloadUnusedAssets();
this.isDetecting = false;
}));
}));
}
private IEnumerator ProcessImage(int inputSize, System.Action<Color32[]> callback)
{
Coroutine croped = StartCoroutine(TextureTools.CropSquare(m_Texture,
TextureTools.RectOptions.Center, snap =>
{
var scaled = Scale(snap, inputSize);
var rotated = Rotate(scaled.GetPixels32(), scaled.width, scaled.height);
callback(rotated);
}));
yield return croped;
}
private void DrawBoxOutline(BoundingBox outline, float scaleFactor, float shiftX, float shiftY)
{
var x = outline.Dimensions.X * scaleFactor + shiftX;
var width = outline.Dimensions.Width * scaleFactor;
var y = outline.Dimensions.Y * scaleFactor + shiftY;
var height = outline.Dimensions.Height * scaleFactor;
DrawRectangle(new Rect(x, y, width, height), 10, this.colorTag);
DrawLabel(new Rect(x, y - 80, 200, 20), $"Localizing {outline.Label}: {(int)(outline.Confidence * 100)}%");
}
public static void DrawRectangle(Rect area, int frameWidth, Color color)
{
Rect lineArea = area;
lineArea.height = frameWidth;
GUI.DrawTexture(lineArea, boxOutlineTexture); // Top line
lineArea.y = area.yMax - frameWidth;
GUI.DrawTexture(lineArea, boxOutlineTexture); // Bottom line
lineArea = area;
lineArea.width = frameWidth;
GUI.DrawTexture(lineArea, boxOutlineTexture); // Left line
lineArea.x = area.xMax - frameWidth;
GUI.DrawTexture(lineArea, boxOutlineTexture); // Right line
}
private static void DrawLabel(Rect position, string text)
{
GUI.Label(position, text, labelStyle);
}
private Texture2D Scale(Texture2D texture, int imageSize)
{
var scaled = TextureTools.scaled(texture, imageSize, imageSize, FilterMode.Bilinear);
return scaled;
}
private Color32[] Rotate(Color32[] pixels, int width, int height)
{
var rotate = TextureTools.RotateImageMatrix(
pixels, width, height, 90);
// var flipped = TextureTools.FlipYImageMatrix(rotate, width, height);
//flipped = TextureTools.FlipXImageMatrix(flipped, width, height);
// return flipped;
return rotate;
}
}