Unclaimed Package Is this your package? Claim it to unlock full analytics and manage your listing.
Claim This Package

Install via UPM

Add to Unity Package Manager using this URL

https://www.pkglnk.dev/detection.git
ML Detection

README Markdown

Copy this to your project's README.md

Style
Preview
pkglnk installs badge
## Installation

Add **ML Detection** to your Unity project via Package Manager:

1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/detection.git
```

[![pkglnk](https://www.pkglnk.dev/badge/detection.svg?style=pkglnk)](https://www.pkglnk.dev/pkg/detection)

README

ML Detection

detector

This is a library for classifying images in Unity Engine using NatML. The following ML models are included:

Setup Instructions

This package requires NatML, so make sure NatML is imported into your project. Then in your project's Packages/manifest.json file, add the following:

{
  "dependencies": {
    "com.natsuite.ml.detection": "git+https://github.com/natsuite/ML-Detection"
  }
}

Detecting Objects in an Image

First, assign the object detection model (in the ML folder) to an MLModelData field in your script:

using NatSuite.ML;
using NatSuite.ML.Vision;

public class Classifier : MonoBehaviour {

    public MLModelData modelData; // Assign this in the Inspector
}

Then create a detection predictor corresponding to the model:

void Start () {
    var model = modelData.Deserialize();
    var predictor = new TinyYOLOv3Predictor(model, modelData.labels);
}

Detect objects in an image:

Texture2D image = ...;
(string label, Rect rect, float score)[] detections = predictor.Predict(image);

Visualizing Detections

To visualize the detections, we will use Unity UI along with Visualizers included in NatML. First, create a UI RawImage to display our image. Then add an MLDetectionVisualizer component:

panel

Next, we have to create a prefab rectangle that will be instantiated for every detected object. You can customize this as you desire, but for this example we will create a simple semi-transparent UI rectangle:

rect

We need to add a UI text that will display the detected object's label:

text

Now, add an MLDetectionRect component to our prefab and assign the Label Text field:

detection rect

Finally, assign the prefab to the MLDetectionVisualizer and disable it in the scene:

assign

Now, we're ready to visualize detected objects. To do so, simply call the Render method on the visualizer:

using System.Collections.Generic;   // For access to `List<>`
using NatSuite.ML.Visualizers;      // For access to `MLDetectionVisualizer`

MLDetectionVisualizer visualizer;   // Assign in Inspector

void Detect () {
    // Detect
    Texture2D image = ...;
    (string label, Rect rect, float score)[] detections = predictor.Predict(image);
    // Visualize
    var visualizations = new List<(Rect, string)>();
    foreach (var (label, rect, confidence) in detections) {
        var visualizationText = $"{label}: {confidence}";
        visualizations.Add((rect, visualizationText));
    }
    visualizer.Render(previewTexture, visualizations.ToArray());
}

Requirements

  • Unity 2019.2+
  • NatML 1.0+

Supported Platforms

  • Android 7.0 Nougat or newer (API level 24+)
  • iOS 13+
  • macOS 10.15+
  • Windows 10 64-bit

Quick Tips

Comments

No comments yet. Be the first!