Paul Bird commited on
Commit
8fd532f
1 Parent(s): 67c8fc8

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. RunYOLO.cs +180 -0
  3. classes.txt +80 -0
  4. yolov7-tiny.onnx +3 -0
  5. yolov7-tiny.sentis +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ yolov7-tiny.sentis filter=lfs diff=lfs merge=lfs -text
RunYOLO.cs ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Collections.Generic;
2
+ using Unity.Sentis;
3
+ using UnityEngine;
4
+ using UnityEngine.UI;
5
+ using UnityEngine.Video;
6
+
7
+ /*
8
+ * YOLO inference script
9
+ * =====================
10
+ *
11
+ * Place this script on the Main Camera.
12
+ *
13
+ * Place the yolov7-tiny.sentis file and a *.mp4 video file in the Assets/StreamingAssets folder
14
+ *
15
+ */
16
+
17
+
18
+ public class RunYOLO : MonoBehaviour
19
+ {
20
+ const string modelName = "yolov7-tiny.sentis";
21
+ const string videoName = "giraffes.mp4";
22
+ // Link the classes.txt here:
23
+ public TextAsset labelsAsset;
24
+ // Create a Raw Image of size 640x640 and link it here:
25
+ public RawImage displayImage;
26
+ // Link to a bounding box texture here:
27
+ public Sprite boxTexture;
28
+ // Link to the font for the labels:
29
+ public Font font;
30
+
31
+ private Transform displayLocation;
32
+ private Model model;
33
+ private IWorker engine;
34
+ private string[] labels;
35
+ private RenderTexture targetRT;
36
+ const BackendType backend = BackendType.GPUCompute;
37
+ private const int imageWidth = 640;
38
+ private const int imageHeight = 640;
39
+
40
+ private VideoPlayer video;
41
+
42
+ //bounding box data
43
+ public struct BoundingBox
44
+ {
45
+ public float centerX;
46
+ public float centerY;
47
+ public float width;
48
+ public float height;
49
+ public string label;
50
+ public float confidence;
51
+ }
52
+
53
+ void Start()
54
+ {
55
+ Application.targetFrameRate = 60;
56
+ Screen.orientation = ScreenOrientation.LandscapeLeft;
57
+
58
+ //Parse neural net labels
59
+ labels = labelsAsset.text.Split('\n');
60
+
61
+ //Load model
62
+ model = ModelLoader.Load(Application.streamingAssetsPath + "/" + modelName);
63
+
64
+ targetRT = new RenderTexture(imageWidth, imageHeight, 0);
65
+
66
+ //Create image to display video
67
+ displayLocation = displayImage.transform;
68
+
69
+ //Create engine to run model
70
+ engine = WorkerFactory.CreateWorker(backend, model);
71
+
72
+ SetupInput();
73
+ }
74
+ void SetupInput()
75
+ {
76
+ video = gameObject.AddComponent<VideoPlayer>();
77
+ video.renderMode = VideoRenderMode.APIOnly;
78
+ video.source = VideoSource.Url;
79
+ video.url = Application.streamingAssetsPath + "/" + videoName;
80
+ video.isLooping = true;
81
+ video.Play();
82
+ }
83
+
84
+ private void Update()
85
+ {
86
+ ExecuteML();
87
+
88
+ if (Input.GetKeyDown(KeyCode.Escape))
89
+ {
90
+ Application.Quit();
91
+ }
92
+ }
93
+
94
+ public void ExecuteML()
95
+ {
96
+ ClearAnnotations();
97
+
98
+ if (video && video.texture)
99
+ {
100
+ float aspect = video.width * 1f / video.height;
101
+ Graphics.Blit(video.texture, targetRT, new Vector2(1f / aspect, 1), new Vector2(0, 0));
102
+ displayImage.texture = targetRT;
103
+ }
104
+ else return;
105
+
106
+ using var input = TextureConverter.ToTensor(targetRT, imageWidth, imageHeight, 3);
107
+ engine.Execute(input);
108
+
109
+ //Read output tensors
110
+ var output = engine.PeekOutput() as TensorFloat;
111
+ output.MakeReadable();
112
+
113
+ //Draw the bounding boxes
114
+ for (int n = 0; n < output.shape[0]; n++)
115
+ {
116
+ var box = new BoundingBox
117
+ {
118
+ centerX = (output[n, 1] + output[n, 3]) / 2 - 320,
119
+ centerY = (output[n, 2] + output[n, 4]) / 2 - 320,
120
+ width = output[n, 3] - output[n, 1],
121
+ height = output[n, 4] - output[n, 2],
122
+ label = labels[(int)output[n, 5]],
123
+ confidence = output[n, 6]
124
+ };
125
+ DrawBox(box);
126
+ }
127
+
128
+ input.Dispose();
129
+ }
130
+
131
+ public void DrawBox(BoundingBox box)
132
+ {
133
+ Color color = Color.yellow;
134
+
135
+ GameObject panel = new GameObject("ObjectBox");
136
+ panel.AddComponent<CanvasRenderer>();
137
+ Image img = panel.AddComponent<Image>();
138
+ img.color = color;
139
+ img.sprite = boxTexture;
140
+
141
+ panel.transform.SetParent(displayLocation, false);
142
+ panel.transform.localPosition = new Vector3(box.centerX, -box.centerY);
143
+
144
+ RectTransform rt = panel.GetComponent<RectTransform>();
145
+ rt.sizeDelta = new Vector2(box.width, box.height);
146
+
147
+ //add class label
148
+ var text = new GameObject("ObjectLabel");
149
+ text.AddComponent<CanvasRenderer>();
150
+ Text txt = text.AddComponent<Text>();
151
+ text.transform.SetParent(panel.transform, false);
152
+ txt.font = font;
153
+ txt.text = box.label;
154
+ txt.color = color;
155
+ txt.fontSize = 40;
156
+ txt.horizontalOverflow = HorizontalWrapMode.Overflow;
157
+ RectTransform rt2 = text.GetComponent<RectTransform>();
158
+ rt2.offsetMin = new Vector2(20, rt2.offsetMin.y);
159
+ rt2.offsetMax = new Vector2(0, rt2.offsetMax.y);
160
+ rt2.offsetMax = new Vector2(rt2.offsetMax.x, 30);
161
+ rt2.offsetMin = new Vector2(rt2.offsetMin.x, 0);
162
+ rt2.anchorMin = new Vector2(0,0);
163
+ rt2.anchorMax = new Vector2(1, 1);
164
+
165
+ img.sprite = boxTexture;
166
+ img.type = Image.Type.Sliced;
167
+ }
168
+
169
+ public void ClearAnnotations()
170
+ {
171
+ foreach (Transform child in displayLocation) {
172
+ Destroy(child.gameObject);
173
+ }
174
+ }
175
+
176
+ private void OnDestroy()
177
+ {
178
+ engine?.Dispose();
179
+ }
180
+ }
classes.txt ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ person
2
+ bicycle
3
+ car
4
+ motorbike
5
+ aeroplane
6
+ bus
7
+ train
8
+ truck
9
+ boat
10
+ traffic light
11
+ fire hydrant
12
+ stop sign
13
+ parking meter
14
+ bench
15
+ bird
16
+ cat
17
+ dog
18
+ horse
19
+ sheep
20
+ cow
21
+ elephant
22
+ bear
23
+ zebra
24
+ giraffe
25
+ backpack
26
+ umbrella
27
+ handbag
28
+ tie
29
+ suitcase
30
+ frisbee
31
+ skis
32
+ snowboard
33
+ sports ball
34
+ kite
35
+ baseball bat
36
+ baseball glove
37
+ skateboard
38
+ surfboard
39
+ tennis racket
40
+ bottle
41
+ wine glass
42
+ cup
43
+ fork
44
+ knife
45
+ spoon
46
+ bowl
47
+ banana
48
+ apple
49
+ sandwich
50
+ orange
51
+ broccoli
52
+ carrot
53
+ hot dog
54
+ pizza
55
+ donut
56
+ cake
57
+ chair
58
+ sofa
59
+ potted plant
60
+ bed
61
+ dining table
62
+ toilet
63
+ tv monitor
64
+ laptop
65
+ mouse
66
+ remote
67
+ keyboard
68
+ cell phone
69
+ microwave
70
+ oven
71
+ toaster
72
+ sink
73
+ refrigerator
74
+ book
75
+ clock
76
+ vase
77
+ scissors
78
+ teddy bear
79
+ hair drier
80
+ toothbrush
yolov7-tiny.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:82d96bbabd9b406ce9acd563b49fda29d2b9992536c090722e728788ddaf4632
3
+ size 25000320
yolov7-tiny.sentis ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4afd2f2bdc2eaba69f00ce2e19f8393df60cf9904f221af057aebafe82af552a
3
+ size 25074104