Update RunYOLO.cs
Browse files- RunYOLO.cs +26 -16
RunYOLO.cs
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
|
2 |
using Unity.Sentis;
|
3 |
using UnityEngine;
|
4 |
using UnityEngine.UI;
|
@@ -17,6 +17,7 @@ using UnityEngine.Video;
|
|
17 |
|
18 |
public class RunYOLO : MonoBehaviour
|
19 |
{
|
|
|
20 |
const string modelName = "yolov7-tiny.sentis";
|
21 |
// Change this to the name of the video you put in StreamingAssets folder:
|
22 |
const string videoName = "giraffes.mp4";
|
@@ -25,7 +26,8 @@ public class RunYOLO : MonoBehaviour
|
|
25 |
// Create a Raw Image in the scene and link it here:
|
26 |
public RawImage displayImage;
|
27 |
// Link to a bounding box texture here:
|
28 |
-
public Sprite
|
|
|
29 |
// Link to the font for the labels:
|
30 |
public Font font;
|
31 |
|
@@ -53,17 +55,18 @@ public class RunYOLO : MonoBehaviour
|
|
53 |
public string label;
|
54 |
public float confidence;
|
55 |
}
|
56 |
-
|
57 |
void Start()
|
58 |
{
|
59 |
Application.targetFrameRate = 60;
|
60 |
Screen.orientation = ScreenOrientation.LandscapeLeft;
|
61 |
-
|
62 |
//Parse neural net labels
|
63 |
labels = labelsAsset.text.Split('\n');
|
64 |
|
65 |
//Load model
|
66 |
-
model = ModelLoader.Load(
|
|
|
67 |
|
68 |
targetRT = new RenderTexture(imageWidth, imageHeight, 0);
|
69 |
|
@@ -74,6 +77,11 @@ public class RunYOLO : MonoBehaviour
|
|
74 |
engine = WorkerFactory.CreateWorker(backend, model);
|
75 |
|
76 |
SetupInput();
|
|
|
|
|
|
|
|
|
|
|
77 |
}
|
78 |
void SetupInput()
|
79 |
{
|
@@ -112,7 +120,7 @@ public class RunYOLO : MonoBehaviour
|
|
112 |
|
113 |
//Read output tensors
|
114 |
var output = engine.PeekOutput() as TensorFloat;
|
115 |
-
output.
|
116 |
|
117 |
float displayWidth = displayImage.rectTransform.rect.width;
|
118 |
float displayHeight = displayImage.rectTransform.rect.height;
|
@@ -120,15 +128,17 @@ public class RunYOLO : MonoBehaviour
|
|
120 |
float scaleX = displayWidth / imageWidth;
|
121 |
float scaleY = displayHeight / imageHeight;
|
122 |
|
|
|
|
|
123 |
//Draw the bounding boxes
|
124 |
-
for (int n = 0; n <
|
125 |
{
|
126 |
var box = new BoundingBox
|
127 |
{
|
128 |
-
centerX = ((output[n, 1] + output[n, 3])*scaleX - displayWidth) / 2,
|
129 |
-
centerY = ((output[n, 2] + output[n, 4])*scaleY - displayHeight) / 2,
|
130 |
-
width = (output[n, 3] - output[n, 1])*scaleX,
|
131 |
-
height = (output[n, 4] - output[n, 2])*scaleY,
|
132 |
label = labels[(int)output[n, 5]],
|
133 |
confidence = Mathf.FloorToInt(output[n, 6] * 100 + 0.5f)
|
134 |
};
|
@@ -136,7 +146,7 @@ public class RunYOLO : MonoBehaviour
|
|
136 |
}
|
137 |
}
|
138 |
|
139 |
-
public void DrawBox(BoundingBox box
|
140 |
{
|
141 |
//Create the bounding box graphic or get from pool
|
142 |
GameObject panel;
|
@@ -155,7 +165,7 @@ public class RunYOLO : MonoBehaviour
|
|
155 |
//Set box size
|
156 |
RectTransform rt = panel.GetComponent<RectTransform>();
|
157 |
rt.sizeDelta = new Vector2(box.width, box.height);
|
158 |
-
|
159 |
//Set label text
|
160 |
var label = panel.GetComponentInChildren<Text>();
|
161 |
label.text = box.label + " (" + box.confidence + "%)";
|
@@ -169,7 +179,7 @@ public class RunYOLO : MonoBehaviour
|
|
169 |
panel.AddComponent<CanvasRenderer>();
|
170 |
Image img = panel.AddComponent<Image>();
|
171 |
img.color = color;
|
172 |
-
img.sprite =
|
173 |
img.type = Image.Type.Sliced;
|
174 |
panel.transform.SetParent(displayLocation, false);
|
175 |
|
@@ -198,7 +208,7 @@ public class RunYOLO : MonoBehaviour
|
|
198 |
|
199 |
public void ClearAnnotations()
|
200 |
{
|
201 |
-
foreach(var box in boxPool)
|
202 |
{
|
203 |
box.SetActive(false);
|
204 |
}
|
@@ -208,4 +218,4 @@ public class RunYOLO : MonoBehaviour
|
|
208 |
{
|
209 |
engine?.Dispose();
|
210 |
}
|
211 |
-
}
|
|
|
1 |
+
using System.Collections.Generic;
|
2 |
using Unity.Sentis;
|
3 |
using UnityEngine;
|
4 |
using UnityEngine.UI;
|
|
|
17 |
|
18 |
public class RunYOLO : MonoBehaviour
|
19 |
{
|
20 |
+
public ModelAsset modelAsset;
|
21 |
const string modelName = "yolov7-tiny.sentis";
|
22 |
// Change this to the name of the video you put in StreamingAssets folder:
|
23 |
const string videoName = "giraffes.mp4";
|
|
|
26 |
// Create a Raw Image in the scene and link it here:
|
27 |
public RawImage displayImage;
|
28 |
// Link to a bounding box texture here:
|
29 |
+
public Sprite borderSprite;
|
30 |
+
public Texture2D borderTexture;
|
31 |
// Link to the font for the labels:
|
32 |
public Font font;
|
33 |
|
|
|
55 |
public string label;
|
56 |
public float confidence;
|
57 |
}
|
58 |
+
|
59 |
void Start()
|
60 |
{
|
61 |
Application.targetFrameRate = 60;
|
62 |
Screen.orientation = ScreenOrientation.LandscapeLeft;
|
63 |
+
|
64 |
//Parse neural net labels
|
65 |
labels = labelsAsset.text.Split('\n');
|
66 |
|
67 |
//Load model
|
68 |
+
model = ModelLoader.Load(modelAsset);
|
69 |
+
//model = ModelLoader.Load(Application.streamingAssetsPath + "/" + modelName);
|
70 |
|
71 |
targetRT = new RenderTexture(imageWidth, imageHeight, 0);
|
72 |
|
|
|
77 |
engine = WorkerFactory.CreateWorker(backend, model);
|
78 |
|
79 |
SetupInput();
|
80 |
+
|
81 |
+
if (borderSprite == null)
|
82 |
+
{
|
83 |
+
borderSprite = Sprite.Create(borderTexture, new Rect(0, 0, borderTexture.width, borderTexture.height), new Vector2(borderTexture.width / 2, borderTexture.height / 2));
|
84 |
+
}
|
85 |
}
|
86 |
void SetupInput()
|
87 |
{
|
|
|
120 |
|
121 |
//Read output tensors
|
122 |
var output = engine.PeekOutput() as TensorFloat;
|
123 |
+
output.CompleteOperationsAndDownload();
|
124 |
|
125 |
float displayWidth = displayImage.rectTransform.rect.width;
|
126 |
float displayHeight = displayImage.rectTransform.rect.height;
|
|
|
128 |
float scaleX = displayWidth / imageWidth;
|
129 |
float scaleY = displayHeight / imageHeight;
|
130 |
|
131 |
+
|
132 |
+
int foundBoxes = output.shape[0];
|
133 |
//Draw the bounding boxes
|
134 |
+
for (int n = 0; n < foundBoxes; n++)
|
135 |
{
|
136 |
var box = new BoundingBox
|
137 |
{
|
138 |
+
centerX = ((output[n, 1] + output[n, 3]) * scaleX - displayWidth) / 2,
|
139 |
+
centerY = ((output[n, 2] + output[n, 4]) * scaleY - displayHeight) / 2,
|
140 |
+
width = (output[n, 3] - output[n, 1]) * scaleX,
|
141 |
+
height = (output[n, 4] - output[n, 2]) * scaleY,
|
142 |
label = labels[(int)output[n, 5]],
|
143 |
confidence = Mathf.FloorToInt(output[n, 6] * 100 + 0.5f)
|
144 |
};
|
|
|
146 |
}
|
147 |
}
|
148 |
|
149 |
+
public void DrawBox(BoundingBox box, int id)
|
150 |
{
|
151 |
//Create the bounding box graphic or get from pool
|
152 |
GameObject panel;
|
|
|
165 |
//Set box size
|
166 |
RectTransform rt = panel.GetComponent<RectTransform>();
|
167 |
rt.sizeDelta = new Vector2(box.width, box.height);
|
168 |
+
|
169 |
//Set label text
|
170 |
var label = panel.GetComponentInChildren<Text>();
|
171 |
label.text = box.label + " (" + box.confidence + "%)";
|
|
|
179 |
panel.AddComponent<CanvasRenderer>();
|
180 |
Image img = panel.AddComponent<Image>();
|
181 |
img.color = color;
|
182 |
+
img.sprite = borderSprite;
|
183 |
img.type = Image.Type.Sliced;
|
184 |
panel.transform.SetParent(displayLocation, false);
|
185 |
|
|
|
208 |
|
209 |
public void ClearAnnotations()
|
210 |
{
|
211 |
+
foreach (var box in boxPool)
|
212 |
{
|
213 |
box.SetActive(false);
|
214 |
}
|
|
|
218 |
{
|
219 |
engine?.Dispose();
|
220 |
}
|
221 |
+
}
|