UnityPaul commited on
Commit
f86399a
1 Parent(s): 043c7e4

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +4 -3
  2. RunHandLandmark.cs +13 -19
  3. hand_landmark.sentis +2 -2
README.md CHANGED
@@ -4,7 +4,7 @@ license: apache-2.0
4
  library_name: unity-sentis
5
  ---
6
 
7
- # Hand Landmark from Google Mediapipe validated for Unity Sentis (Version 1.3.0-pre.3*)
8
  *Version 1.3.0 Sentis files are not compatible with Sentis 1.4.0 and need to be recreated/downloaded
9
 
10
  This is the [Hand Landmark model](https://developers.google.com/mediapipe/solutions/vision/hand_landmarker) from Google in the Sentis format.
@@ -15,8 +15,9 @@ The model detects 3D markers on a hand centered in an image. You could use these
15
 
16
  ## How to Use
17
  * Create a new scene in Unity 2023
18
- * Install `com.unity.sentis` version `1.3.0-pre.3` from the package manager
19
- * Put the hand_landmark.sentis file in the `Assets/StreamingAssets` folder
 
20
  * Put a video in the `Assets/StreamingAssets` folder and set `videoName` variable to the video name
21
  * Create a RawImage and place it in your scene. Link to this image in the `previewUI` field.
22
 
 
4
  library_name: unity-sentis
5
  ---
6
 
7
+ # Hand Landmark from Google Mediapipe validated for Unity Sentis (Version 1.4.0-pre.3*)
8
  *Version 1.3.0 Sentis files are not compatible with Sentis 1.4.0 and need to be recreated/downloaded
9
 
10
  This is the [Hand Landmark model](https://developers.google.com/mediapipe/solutions/vision/hand_landmarker) from Google in the Sentis format.
 
15
 
16
  ## How to Use
17
  * Create a new scene in Unity 2023
18
+ * Install `com.unity.sentis` version `1.4.0-pre.3` from the package manager
19
+ * Add the c# file to the Main Camera
20
+ * Drag the hand_landmark.sentis file in asset field
21
  * Put a video in the `Assets/StreamingAssets` folder and set `videoName` variable to the video name
22
  * Create a RawImage and place it in your scene. Link to this image in the `previewUI` field.
23
 
RunHandLandmark.cs CHANGED
@@ -2,6 +2,7 @@ using UnityEngine;
2
  using Unity.Sentis;
3
  using UnityEngine.Video;
4
  using UnityEngine.UI;
 
5
  using Lays = Unity.Sentis.Layers;
6
  using System.Collections.Generic;
7
 
@@ -12,7 +13,7 @@ using System.Collections.Generic;
12
  * Basic inference script for blaze hand landmarks
13
  *
14
  * Put this script on the Main Camera
15
- * Put hand_landmark.sentis in the Assets/StreamingAssets folder
16
  * Create a RawImage of in the scene
17
  * Put a link to that image in previewUI
18
  * Put a video in Assets/StreamingAssets folder and put the name of it int videoName
@@ -23,11 +24,14 @@ using System.Collections.Generic;
23
 
24
  public class RunHandLandmark : MonoBehaviour
25
  {
 
 
 
26
  //Drag a link to a raw image here:
27
  public RawImage previewUI = null;
28
 
29
  // Put your bounding box sprite image here
30
- public Sprite faceTexture;
31
 
32
  // 6 optional sprite images (left eye, right eye, nose, mouth, left ear, right ear)
33
  public Sprite[] markerTextures;
@@ -53,9 +57,6 @@ public class RunHandLandmark : MonoBehaviour
53
  //Holds image size
54
  const int size = 224;
55
 
56
- Ops ops;
57
- ITensorAllocator allocator;
58
-
59
  Model model;
60
 
61
  //webcam device name:
@@ -74,8 +75,6 @@ public class RunHandLandmark : MonoBehaviour
74
  List<GameObject> boxPool = new();
75
  void Start()
76
  {
77
- allocator = new TensorCachingAllocator();
78
-
79
  //(Note: if using a webcam on mobile get permissions here first)
80
 
81
  targetTexture = new RenderTexture(resolution.x, resolution.y, 0);
@@ -88,12 +87,12 @@ public class RunHandLandmark : MonoBehaviour
88
 
89
  void SetupModel()
90
  {
91
- model = ModelLoader.Load(Application.streamingAssetsPath + "/hand_landmark.sentis");
 
92
  }
93
  public void SetupEngine()
94
  {
95
  worker = WorkerFactory.CreateWorker(backend, model);
96
- ops = WorkerFactory.CreateOps(backend, allocator);
97
  }
98
 
99
  void SetupInput()
@@ -190,7 +189,7 @@ public class RunHandLandmark : MonoBehaviour
190
  width = 8f * scale.x,
191
  height = 8f * scale.y,
192
  };
193
- DrawBox(marker, j < markerTextures.Length ? markerTextures[j] : faceTexture, j);
194
  }
195
  }
196
 
@@ -199,10 +198,7 @@ public class RunHandLandmark : MonoBehaviour
199
  var transform = new TextureTransform();
200
  transform.SetDimensions(size, size, 3);
201
  transform.SetTensorLayout(0, 1, 2, 3);
202
- using var image0 = TextureConverter.ToTensor(source, transform);
203
-
204
- // Pre-process the image to make input in range (-1..1)
205
- using var image = ops.Mad(image0, 2f, -1f);
206
 
207
  worker.Execute(image);
208
 
@@ -211,7 +207,7 @@ public class RunHandLandmark : MonoBehaviour
211
  ClearAnnotations();
212
 
213
  Vector2 markerScale = previewUI.rectTransform.rect.size/ size;
214
- landmarks.MakeReadable();
215
  DrawLandmarks(landmarks, markerScale);
216
 
217
  bool showExtraInformation = false;
@@ -219,8 +215,8 @@ public class RunHandLandmark : MonoBehaviour
219
  {
220
  using var A = worker.PeekOutput("Identity_1") as TensorFloat;
221
  using var B = worker.PeekOutput("Identity_2") as TensorFloat;
222
- A.MakeReadable();
223
- B.MakeReadable();
224
  Debug.Log("A,B=" + A[0, 0] + "," + B[0, 0]);
225
  }
226
  }
@@ -263,8 +259,6 @@ public class RunHandLandmark : MonoBehaviour
263
  void CleanUp()
264
  {
265
  closing = true;
266
- ops?.Dispose();
267
- allocator?.Dispose();
268
  if (webcam) Destroy(webcam);
269
  if (video) Destroy(video);
270
  RenderTexture.active = null;
 
2
  using Unity.Sentis;
3
  using UnityEngine.Video;
4
  using UnityEngine.UI;
5
+ using System.IO;
6
  using Lays = Unity.Sentis.Layers;
7
  using System.Collections.Generic;
8
 
 
13
  * Basic inference script for blaze hand landmarks
14
  *
15
  * Put this script on the Main Camera
16
+ * Drag the sentis file onto the modelAsset field
17
  * Create a RawImage of in the scene
18
  * Put a link to that image in previewUI
19
  * Put a video in Assets/StreamingAssets folder and put the name of it int videoName
 
24
 
25
  public class RunHandLandmark : MonoBehaviour
26
  {
27
+ //Draw the *.sentis or *.onnx model asset here:
28
+ public ModelAsset asset;
29
+ string modelName = "hand_landmark.sentis";
30
  //Drag a link to a raw image here:
31
  public RawImage previewUI = null;
32
 
33
  // Put your bounding box sprite image here
34
+ public Sprite boxSprite;
35
 
36
  // 6 optional sprite images (left eye, right eye, nose, mouth, left ear, right ear)
37
  public Sprite[] markerTextures;
 
57
  //Holds image size
58
  const int size = 224;
59
 
 
 
 
60
  Model model;
61
 
62
  //webcam device name:
 
75
  List<GameObject> boxPool = new();
76
  void Start()
77
  {
 
 
78
  //(Note: if using a webcam on mobile get permissions here first)
79
 
80
  targetTexture = new RenderTexture(resolution.x, resolution.y, 0);
 
87
 
88
  void SetupModel()
89
  {
90
+ model = ModelLoader.Load(asset);
91
+ //model = ModelLoader.Load(Path.Join(Application.streamingAssetsPath ,modelName));
92
  }
93
  public void SetupEngine()
94
  {
95
  worker = WorkerFactory.CreateWorker(backend, model);
 
96
  }
97
 
98
  void SetupInput()
 
189
  width = 8f * scale.x,
190
  height = 8f * scale.y,
191
  };
192
+ DrawBox(marker, j < markerTextures.Length ? markerTextures[j] : boxSprite, j);
193
  }
194
  }
195
 
 
198
  var transform = new TextureTransform();
199
  transform.SetDimensions(size, size, 3);
200
  transform.SetTensorLayout(0, 1, 2, 3);
201
+ using var image = TextureConverter.ToTensor(source, transform);
 
 
 
202
 
203
  worker.Execute(image);
204
 
 
207
  ClearAnnotations();
208
 
209
  Vector2 markerScale = previewUI.rectTransform.rect.size/ size;
210
+ landmarks.CompleteOperationsAndDownload();
211
  DrawLandmarks(landmarks, markerScale);
212
 
213
  bool showExtraInformation = false;
 
215
  {
216
  using var A = worker.PeekOutput("Identity_1") as TensorFloat;
217
  using var B = worker.PeekOutput("Identity_2") as TensorFloat;
218
+ A.CompleteOperationsAndDownload();
219
+ B.CompleteOperationsAndDownload();
220
  Debug.Log("A,B=" + A[0, 0] + "," + B[0, 0]);
221
  }
222
  }
 
259
  void CleanUp()
260
  {
261
  closing = true;
 
 
262
  if (webcam) Destroy(webcam);
263
  if (video) Destroy(video);
264
  RenderTexture.active = null;
hand_landmark.sentis CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:584d24cd0814e354d9f14b74a53a921c5ff0c31ca282872cc0ca3b6dc21119de
3
- size 3879993
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dc2d912227dd19ba9482b0ec0fd2c69d2665191d3473925b25d7ec1d0b04cd5d
3
+ size 3812936