Paul Bird commited on
Commit
92c260e
1 Parent(s): 8a51653

Upload RunYOLO.cs

Browse files
Files changed (1) hide show
  1. RunYOLO.cs +43 -18
RunYOLO.cs CHANGED
@@ -40,6 +40,7 @@ public class RunYOLO : MonoBehaviour
40
 
41
  private VideoPlayer video;
42
 
 
43
  //bounding box data
44
  public struct BoundingBox
45
  {
@@ -60,7 +61,7 @@ public class RunYOLO : MonoBehaviour
60
  labels = labelsAsset.text.Split('\n');
61
 
62
  //Load model
63
- model = ModelLoader.Load(Application.streamingAssetsPath + "/" + modelName);
64
 
65
  targetRT = new RenderTexture(imageWidth, imageHeight, 0);
66
 
@@ -123,51 +124,75 @@ public class RunYOLO : MonoBehaviour
123
  label = labels[(int)output[n, 5]],
124
  confidence = Mathf.FloorToInt(output[n, 6] * 100 + 0.5f)
125
  };
126
- DrawBox(box);
127
  }
128
  }
129
 
130
- public void DrawBox(BoundingBox box)
131
  {
132
- Color color = Color.yellow;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
- //Create the bounding box graphic
135
  var panel = new GameObject("ObjectBox");
136
  panel.AddComponent<CanvasRenderer>();
137
  Image img = panel.AddComponent<Image>();
138
  img.color = color;
139
- img.sprite = boxTexture;
 
140
  panel.transform.SetParent(displayLocation, false);
141
- panel.transform.localPosition = new Vector3(box.centerX, -box.centerY);
142
- RectTransform rt = panel.GetComponent<RectTransform>();
143
- rt.sizeDelta = new Vector2(box.width, box.height);
144
 
145
- //Add the class label
 
146
  var text = new GameObject("ObjectLabel");
147
  text.AddComponent<CanvasRenderer>();
148
- Text txt = text.AddComponent<Text>();
149
  text.transform.SetParent(panel.transform, false);
 
150
  txt.font = font;
151
- txt.text = box.label + " (" + box.confidence + "%)";
152
  txt.color = color;
153
  txt.fontSize = 40;
154
  txt.horizontalOverflow = HorizontalWrapMode.Overflow;
 
155
  RectTransform rt2 = text.GetComponent<RectTransform>();
156
  rt2.offsetMin = new Vector2(20, rt2.offsetMin.y);
157
  rt2.offsetMax = new Vector2(0, rt2.offsetMax.y);
158
- rt2.offsetMax = new Vector2(rt2.offsetMax.x, 30);
159
  rt2.offsetMin = new Vector2(rt2.offsetMin.x, 0);
160
- rt2.anchorMin = new Vector2(0,0);
 
161
  rt2.anchorMax = new Vector2(1, 1);
162
 
163
- img.sprite = boxTexture;
164
- img.type = Image.Type.Sliced;
165
  }
166
 
167
  public void ClearAnnotations()
168
  {
169
- foreach (Transform child in displayLocation) {
170
- Destroy(child.gameObject);
 
171
  }
172
  }
173
 
 
40
 
41
  private VideoPlayer video;
42
 
43
+ List<GameObject> boxPool = new List<GameObject>();
44
  //bounding box data
45
  public struct BoundingBox
46
  {
 
61
  labels = labelsAsset.text.Split('\n');
62
 
63
  //Load model
64
+ model = ModelLoader.Load(Application.streamingAssetsPath +"/"+ modelName);
65
 
66
  targetRT = new RenderTexture(imageWidth, imageHeight, 0);
67
 
 
124
  label = labels[(int)output[n, 5]],
125
  confidence = Mathf.FloorToInt(output[n, 6] * 100 + 0.5f)
126
  };
127
+ DrawBox(box, n);
128
  }
129
  }
130
 
131
+ public void DrawBox(BoundingBox box , int id)
132
  {
133
+ //Create the bounding box graphic or get from pool
134
+ GameObject panel;
135
+ if (id < boxPool.Count)
136
+ {
137
+ panel = boxPool[id];
138
+ panel.SetActive(true);
139
+ }
140
+ else
141
+ {
142
+ panel = CreateNewBox(Color.yellow);
143
+ }
144
+ //Set box position
145
+ panel.transform.localPosition = new Vector3(box.centerX, -box.centerY);
146
+
147
+ //Set box size
148
+ RectTransform rt = panel.GetComponent<RectTransform>();
149
+ rt.sizeDelta = new Vector2(box.width, box.height);
150
+
151
+ //Set label text
152
+ var label = panel.GetComponentInChildren<Text>();
153
+ label.text = box.label + " (" + box.confidence + "%)";
154
+ }
155
+
156
+ public GameObject CreateNewBox(Color color)
157
+ {
158
+ //Create the box and set image
159
 
 
160
  var panel = new GameObject("ObjectBox");
161
  panel.AddComponent<CanvasRenderer>();
162
  Image img = panel.AddComponent<Image>();
163
  img.color = color;
164
+ img.sprite = boxTexture;
165
+ img.type = Image.Type.Sliced;
166
  panel.transform.SetParent(displayLocation, false);
 
 
 
167
 
168
+ //Create the label
169
+
170
  var text = new GameObject("ObjectLabel");
171
  text.AddComponent<CanvasRenderer>();
 
172
  text.transform.SetParent(panel.transform, false);
173
+ Text txt = text.AddComponent<Text>();
174
  txt.font = font;
 
175
  txt.color = color;
176
  txt.fontSize = 40;
177
  txt.horizontalOverflow = HorizontalWrapMode.Overflow;
178
+
179
  RectTransform rt2 = text.GetComponent<RectTransform>();
180
  rt2.offsetMin = new Vector2(20, rt2.offsetMin.y);
181
  rt2.offsetMax = new Vector2(0, rt2.offsetMax.y);
 
182
  rt2.offsetMin = new Vector2(rt2.offsetMin.x, 0);
183
+ rt2.offsetMax = new Vector2(rt2.offsetMax.x, 30);
184
+ rt2.anchorMin = new Vector2(0, 0);
185
  rt2.anchorMax = new Vector2(1, 1);
186
 
187
+ boxPool.Add(panel);
188
+ return panel;
189
  }
190
 
191
  public void ClearAnnotations()
192
  {
193
+ foreach(var box in boxPool)
194
+ {
195
+ box.SetActive(false);
196
  }
197
  }
198