-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathDetectionTest.cs
52 lines (43 loc) · 1.48 KB
/
DetectionTest.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
using UnityEngine;
using System.Linq;
using UI = UnityEngine.UI;
using Klak.TestTools;
sealed class DetectionTest : MonoBehaviour
{
[SerializeField] ImageSource _source = null;
[SerializeField] int _decimation = 4;
[SerializeField] float _tagSize = 0.05f;
[SerializeField] Material _tagMaterial = null;
[SerializeField] UI.RawImage _webcamPreview = null;
[SerializeField] UI.Text _debugText = null;
AprilTag.TagDetector _detector;
TagDrawer _drawer;
void Start()
{
var dims = _source.OutputResolution;
_detector = new AprilTag.TagDetector(dims.x, dims.y, _decimation);
_drawer = new TagDrawer(_tagMaterial);
}
void OnDestroy()
{
_detector.Dispose();
_drawer.Dispose();
}
void LateUpdate()
{
_webcamPreview.texture = _source.Texture;
// Source image acquisition
var image = _source.Texture.AsSpan();
if (image.IsEmpty) return;
// AprilTag detection
var fov = Camera.main.fieldOfView * Mathf.Deg2Rad;
_detector.ProcessImage(image, fov, _tagSize);
// Detected tag visualization
foreach (var tag in _detector.DetectedTags)
_drawer.Draw(tag.ID, tag.Position, tag.Rotation, _tagSize);
// Profile data output (with 30 frame interval)
if (Time.frameCount % 30 == 0)
_debugText.text = _detector.ProfileData.Aggregate
("Profile (usec)", (c, n) => $"{c}\n{n.name} : {n.time}");
}
}