Skip to content

Commit f3008fa

Browse files
committed
Minor updates
1 parent 93a9e72 commit f3008fa

File tree

15 files changed

+816
-471
lines changed

15 files changed

+816
-471
lines changed

.github/workflows/publish.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Publish to NPM
2+
3+
on:
4+
release:
5+
types: [published]
6+
workflow_dispatch:
7+
8+
jobs:
9+
npm:
10+
runs-on: ubuntu-latest
11+
env:
12+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-node@v2
16+
with:
17+
registry-url: 'https://registry.npmjs.org'
18+
- run: npm publish --access public
19+
working-directory: Packages/ai.natml.vision.nanodet

Assets/NanoDetDetection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2022 NatML Inc. All Rights Reserved.
44
*/
55

6-
namespace NatML.Visualizers {
6+
namespace NatML.Examples.Visualizers {
77

88
using UnityEngine;
99
using UnityEngine.UI;

Assets/NanoDetSample.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ namespace NatML.Examples {
1010
using NatML.Devices.Outputs;
1111
using NatML.Features;
1212
using NatML.Vision;
13-
using NatML.Visualizers;
13+
using Visualizers;
1414

1515
public sealed class NanoDetSample : MonoBehaviour {
1616

1717
[Header(@"UI")]
1818
public NanoDetVisualizer visualizer;
1919

20-
CameraDevice cameraDevice;
21-
TextureOutput textureOutput;
20+
private CameraDevice cameraDevice;
21+
private TextureOutput cameraTextureOutput;
2222

23-
MLModelData modelData;
24-
MLModel model;
25-
NanoDetPredictor predictor;
23+
private MLModelData modelData;
24+
private MLModel model;
25+
private NanoDetPredictor predictor;
2626

2727
async void Start () {
2828
// Request permissions
@@ -35,11 +35,11 @@ async void Start () {
3535
var query = new MediaDeviceQuery(MediaDeviceCriteria.CameraDevice);
3636
cameraDevice = query.current as CameraDevice;
3737
// Start the preview
38-
textureOutput = new TextureOutput();
39-
cameraDevice.StartRunning(textureOutput);
38+
cameraTextureOutput = new TextureOutput();
39+
cameraDevice.StartRunning(cameraTextureOutput);
4040
// Display the camera preview
41-
var previewTexture = await textureOutput;
42-
visualizer.Render(previewTexture);
41+
var cameraTexture = await cameraTextureOutput;
42+
visualizer.image = cameraTexture;
4343
// Create the NanoDet predictor
4444
modelData = await MLModelData.FromHub("@natsuite/nanodet");
4545
model = modelData.Deserialize();
@@ -51,15 +51,18 @@ void Update () {
5151
if (predictor == null)
5252
return;
5353
// Create input feature
54-
var inputFeature = new MLImageFeature(textureOutput.texture);
55-
(inputFeature.mean, inputFeature.std) = modelData.normalization;
56-
inputFeature.aspectMode = modelData.aspectMode;
54+
var imageFeature = new MLImageFeature(cameraTextureOutput.texture);
55+
(imageFeature.mean, imageFeature.std) = modelData.normalization;
56+
imageFeature.aspectMode = modelData.aspectMode;
5757
// Detect
58-
var detections = predictor.Predict(inputFeature);
58+
var detections = predictor.Predict(imageFeature);
5959
// Visualize
60-
visualizer.Render(textureOutput.texture, detections);
60+
visualizer.Render(detections);
6161
}
6262

63-
void OnDisable () => model?.Dispose();
63+
void OnDisable () {
64+
// Dispose the model
65+
model?.Dispose();
66+
}
6467
}
6568
}

Assets/NanoDetVisualizer.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2022 NatML Inc. All Rights Reserved.
44
*/
55

6-
namespace NatML.Visualizers {
6+
namespace NatML.Examples.Visualizers {
77

88
using System.Collections.Generic;
99
using UnityEngine;
@@ -14,22 +14,33 @@ namespace NatML.Visualizers {
1414
[RequireComponent(typeof(RawImage), typeof(AspectRatioFitter))]
1515
public sealed class NanoDetVisualizer : MonoBehaviour {
1616

17+
#region --Inspector--
18+
public NanoDetDetection detectionPrefab;
19+
#endregion
20+
21+
1722
#region --Client API--
23+
/// <summary>
24+
/// Detection source image.
25+
/// </summary>
26+
public Texture2D image {
27+
get => rawImage.texture as Texture2D;
28+
set {
29+
rawImage.texture = value;
30+
aspectFitter.aspectRatio = (float)value.width / value.height;
31+
}
32+
}
33+
1834
/// <summary>
1935
/// Render a set of object detections.
2036
/// </summary>
2137
/// <param name="image">Image which detections are made on.</param>
2238
/// <param name="detections">Detections to render.</param>
23-
public void Render (Texture image, params (Rect rect, string label, float score)[] detections) {
39+
public void Render (params (Rect rect, string label, float score)[] detections) {
2440
// Delete current
2541
foreach (var rect in currentRects)
2642
GameObject.Destroy(rect.gameObject);
2743
currentRects.Clear();
28-
// Display image
29-
var rawImage = GetComponent<RawImage>();
30-
var aspectFitter = GetComponent<AspectRatioFitter>();
31-
rawImage.texture = image;
32-
aspectFitter.aspectRatio = (float)image.width / image.height;
3344
// Render rects
3445
var imageRect = new Rect(0, 0, image.width, image.height);
3546
foreach (var detection in detections) {
@@ -43,8 +54,14 @@ public void Render (Texture image, params (Rect rect, string label, float score)
4354

4455

4556
#region --Operations--
46-
[SerializeField] NanoDetDetection detectionPrefab;
47-
List<NanoDetDetection> currentRects = new List<NanoDetDetection>();
57+
private RawImage rawImage;
58+
private AspectRatioFitter aspectFitter;
59+
private readonly List<NanoDetDetection> currentRects = new List<NanoDetDetection>();
60+
61+
void Awake () {
62+
rawImage = GetComponent<RawImage>();
63+
aspectFitter = GetComponent<AspectRatioFitter>();
64+
}
4865
#endregion
4966
}
5067
}
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
## 1.0.2
2-
+ Upgraded to NatML 1.0.11.
3-
4-
## 1.0.1
5-
+ Upgraded to NatML 1.0.9.
6-
71
## 1.0.0
82
+ First release.

Packages/ai.natml.vision.nanodet/README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# NanoDet
2-
[NanoDet](https://github.com/RangiLyu/nanodet) high performance general object detection. This package requires [NatML](https://github.com/natmlx/NatML).
2+
[NanoDet](https://github.com/RangiLyu/nanodet) high performance general object detection.
3+
4+
## Installing NanoDet
5+
Add the following items to your Unity project's `Packages/manifest.json`:
6+
```json
7+
{
8+
"scopedRegistries": [
9+
{
10+
"name": "NatML",
11+
"url": "https://registry.npmjs.com",
12+
"scopes": ["ai.natml"]
13+
}
14+
],
15+
"dependencies": {
16+
"ai.natml.vision.nanodet": "1.0.0"
17+
}
18+
}
19+
```
20+
321

422
## Detecting Objects in an Image
523
First, create the NanoDet predictor:
@@ -16,7 +34,7 @@ Then detect objects in the image:
1634
```csharp
1735
// Create image feature
1836
Texture2D image = ...;
19-
var imageFeature = new MLImageFeature(image); // This also accepts a `Color32[]` or `byte[]`
37+
var imageFeature = new MLImageFeature(image);
2038
(imageFeature.mean, imageFeature.std) = modelData.normalization;
2139
imageFeature.aspectMode = modelData.aspectMode;
2240
// Detect objects
@@ -27,13 +45,12 @@ imageFeature.aspectMode = modelData.aspectMode;
2745
___
2846

2947
## Requirements
30-
- Unity 2020.3+
31-
- [NatML 1.0.11+](https://github.com/natmlx/NatML)
48+
- Unity 2021.2+
3249

3350
## Quick Tips
51+
- Join the [NatML community on Discord](https://hub.natml.ai/community).
3452
- Discover more ML models on [NatML Hub](https://hub.natml.ai).
3553
- See the [NatML documentation](https://docs.natml.ai/unity).
36-
- Join the [NatML community on Discord](https://discord.gg/y5vwgXkz2f).
3754
- Discuss [NatML on Unity Forums](https://forum.unity.com/threads/open-beta-natml-machine-learning-runtime.1109339/).
3855
- Contact us at [hi@natml.ai](mailto:hi@natml.ai).
3956

Packages/ai.natml.vision.nanodet/Runtime/NanoDetPredictor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public NanoDetPredictor (MLModel model, string[] labels, float minScore = 0.35f,
4343
this.anchors8 = GenerateAnchors(inputType.width, inputType.height, 8);
4444
this.anchors16 = GenerateAnchors(inputType.width, inputType.height, 16);
4545
this.anchors32 = GenerateAnchors(inputType.width, inputType.height, 32);
46+
this.candidateBoxes = new List<Rect>();
47+
this.candidateScores = new List<float>();
48+
this.candidateLabels = new List<string>();
4649
}
4750

4851
/// <summary>
@@ -70,13 +73,13 @@ public unsafe (Rect rect, string label, float score)[] Predict (params MLFeature
7073
var displacements8 = new MLArrayFeature<float>(outputFeatures[3]); // (1,1600,32)
7174
var displacements16 = new MLArrayFeature<float>(outputFeatures[4]); // (1,400,32)
7275
var displacements32 = new MLArrayFeature<float>(outputFeatures[5]); // (1,100,32)
73-
var candidateBoxes = new List<Rect>();
74-
var candidateScores = new List<float>();
75-
var candidateLabels = new List<string>();
7676
var softmax = stackalloc float[8];
7777
var boxEdges = stackalloc float[4];
7878
var featureWidthInv = 1f / inputType.width;
7979
var featureHeightInv = 1f / inputType.height;
80+
candidateBoxes.Clear();
81+
candidateScores.Clear();
82+
candidateLabels.Clear();
8083
foreach (var (stride, logits, displacements, anchors) in new [] {
8184
(8, logits8, displacements8, anchors8),
8285
(16, logits16, displacements16, anchors16),
@@ -136,6 +139,9 @@ public unsafe (Rect rect, string label, float score)[] Predict (params MLFeature
136139
private readonly Vector2[] anchors8;
137140
private readonly Vector2[] anchors16;
138141
private readonly Vector2[] anchors32;
142+
private readonly List<Rect> candidateBoxes;
143+
private readonly List<float> candidateScores;
144+
private readonly List<string> candidateLabels;
139145

140146
void IDisposable.Dispose () { } // Not used
141147

Packages/ai.natml.vision.nanodet/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"version": "1.0.0",
44
"displayName": "NanoDet",
55
"description": "Anchor-free ultra-fast object detection.",
6-
"unity": "2020.3",
6+
"unity": "2021.2",
77
"dependencies": {
8-
"ai.natml.natml": "1.0.17"
8+
"ai.natml.natml": "1.0.18"
99
},
1010
"keywords": [
1111
"natml",
@@ -24,5 +24,5 @@
2424
"url": "https://github.com/natmlx"
2525
},
2626
"license": "Apache-2.0",
27-
"repository": "github:natmlx/NatML"
27+
"repository": "github:natmlx/NanoDet"
2828
}

Packages/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
}
88
],
99
"dependencies": {
10-
"ai.natml.natdevice": "1.2.3",
10+
"ai.natml.natdevice": "1.2.5",
1111
"com.unity.collab-proxy": "1.15.17",
1212
"com.unity.feature.development": "1.0.1",
1313
"com.unity.ide.rider": "3.0.14",

Packages/packages-lock.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
"dependencies": {
33
"ai.natml.hub": {
44
"version": "1.0.12",
5-
"depth": 2,
5+
"depth": 1,
66
"source": "registry",
77
"dependencies": {},
88
"url": "https://registry.npmjs.com"
99
},
1010
"ai.natml.natdevice": {
11-
"version": "1.2.3",
11+
"version": "1.2.5",
1212
"depth": 0,
1313
"source": "registry",
1414
"dependencies": {
15-
"ai.natml.hub": "1.0.10"
15+
"ai.natml.hub": "1.0.12"
1616
},
1717
"url": "https://registry.npmjs.com"
1818
},
1919
"ai.natml.natml": {
20-
"version": "1.0.17",
20+
"version": "1.0.18",
2121
"depth": 1,
2222
"source": "registry",
2323
"dependencies": {
@@ -30,7 +30,7 @@
3030
"depth": 0,
3131
"source": "embedded",
3232
"dependencies": {
33-
"ai.natml.natml": "1.0.17"
33+
"ai.natml.natml": "1.0.18"
3434
}
3535
},
3636
"com.unity.collab-proxy": {

ProjectSettings/ProjectSettings.asset

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ PlayerSettings:
137137
16:9: 1
138138
Others: 1
139139
bundleVersion: 0.1
140-
preloadedAssets: []
140+
preloadedAssets:
141+
- {fileID: 0}
141142
metroInputSource: 0
142143
wsaTransparentSwapchain: 0
143144
m_HolographicPauseOnTrackingLoss: 1
@@ -791,12 +792,12 @@ PlayerSettings:
791792
webGLNameFilesAsHashes: 0
792793
webGLDataCaching: 1
793794
webGLDebugSymbols: 0
794-
webGLEmscriptenArgs:
795+
webGLEmscriptenArgs: ' --bind '
795796
webGLModulesDirectory:
796797
webGLTemplate: APPLICATION:Default
797798
webGLAnalyzeBuildSize: 0
798799
webGLUseEmbeddedResources: 0
799-
webGLCompressionFormat: 1
800+
webGLCompressionFormat: 2
800801
webGLWasmArithmeticExceptions: 0
801802
webGLLinkerTarget: 1
802803
webGLThreadsSupport: 0

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,37 @@
11
# NanoDet
2-
*INCOMPLETE*
2+
3+
![demo](demo.gif)
4+
5+
Anchor-free ultra-fast object detection.
6+
7+
## Running the Sample
8+
Retrieve your access key from [NatML Hub](https://hub.natml.ai/profile) and add it to your Project Settings. Note that running the sample code requires an active [NatML Cloud](https://www.natml.ai/pricing) subscription:
9+
10+
![project settings](https://github.com/natmlx/NatDevice/raw/main/.media/key.png)
11+
12+
## Using the Predictor in a Different Project
13+
[See the predictor README for more details](Packages/ai.natml.vision.nanodet/README.md).
14+
15+
## Requirements
16+
- Unity 2021.2+
17+
18+
## Supported Platforms
19+
- Android API level 24+
20+
- iOS 13+
21+
- macOS 10.15+ (Apple Silicon and Intel)
22+
- Windows 10+ (64-bit only)
23+
- WebGL:
24+
- Chrome 91+
25+
- Firefox 90+
26+
27+
## Resources
28+
- Join the [NatML community on Discord](https://hub.natml.ai/community).
29+
- See the [NatML documentation](https://docs.natml.ai/unity).
30+
- See the [NatDevice documentation](https://docs.natml.ai/natdevice).
31+
- Check out [NatML on GitHub](https://github.com/natmlx).
32+
- Read the [NatML blog](https://blog.natml.ai/).
33+
- Discuss [NatML on Unity Forums](https://forum.unity.com/threads/open-beta-natml-machine-learning-runtime.1109339/).
34+
- Discuss [NatDevice on Unity Forums](https://forum.unity.com/threads/natdevice-media-device-api.374690/).
35+
- Contact us at [hi@natml.ai](mailto:hi@natml.ai).
36+
37+
Thank you very much!

0 commit comments

Comments
 (0)