Unity
  • NatML for Unity
  • Preliminaries
    • Getting Started
    • Requirements
  • Workflows
    • Core Concepts
    • Fetching Models
    • Using Predictors
  • Authoring
    • Creating Predictors
    • Distributing Predictors
  • API Reference
    • IMLPredictor
    • MLModel
      • MLEdgeModel
        • Configuration
      • MLCloudModel
    • MLFeature
      • MLArrayFeature
      • MLImageFeature
      • MLStringFeature
      • MLAudioFeature
      • MLVideoFeature
      • MLDepthFeature
      • MLXRCpuDepthFeature
    • MLFeatureType
      • MLArrayType
      • MLAudioType
      • MLImageType
      • MLVideoType
      • MLStringType
    • MLPredictorExtensions
  • Integrations
    • Media Devices
    • Augmented Reality
    • Video Recording
  • Insiders
    • Changelog
    • Open Source
    • GitHub
    • Discord
    • Blog
Powered by GitBook
On this page
  • Importing NatML
  • Importing the MobileNet Predictor
  • Specifying your NatML Access Key
  • Importing a Sample Image
  • Making a Prediction

Was this helpful?

  1. Preliminaries

Getting Started

Quick Primer

PreviousNatML for UnityNextRequirements

Last updated 1 year ago

Was this helpful?

Importing NatML

To begin, by adding the following lines to your Unity project's Packages > manifest.json file:

{
  "scopedRegistries": [
   {
    "name": "NatML",
    "url": "https://registry.npmjs.com",
    "scopes": ["ai.natml"]
   }
  ],
  "dependencies": {
    "ai.natml.natml": "1.1.8",
    ...
  }
}

Once NatML has been imported, you can run ML models right in the Editor. For this example, we will classify an image using the popular architecture:

Importing the MobileNet Predictor

Packages/manifest.json
{
  ...,
  "dependencies": {
    "ai.natml.natml": "1.1.8",
    "ai.natml.vision.mobilenet-v2": "1.0.3",
    ...
  }
}

Specifying your NatML Access Key

Once you have an access key, you can add it to your Unity project in Project Settings > NatML:

Importing a Sample Image

Next, import the image below (download it into your Unity project):

Make sure that in the advanced settings, you enable the Read/Write Enabled setting:

Making a Prediction

Now, we can write a small script to classify the image:

Classifier.cs
using UnityEngine;
using NatML.Vision;

public class Classifier : MonoBehaviour {
    
    [Header(@"Prediction")]
    public Texture2D image;
    
    async void Start () {
        // Create the MobileNet predictor
        using var predictor = await MobileNetv2Predictor.Create();
        // Classify the image feature
        var (label, confidence) = predictor.Predict(image);
        // Log the result
        Debug.Log($"Image contains {label} with confidence {confidence}");     
    }
}

Now, let us add our script to the scene and assign the inspector variables:

Now, we run the script to confirm that our model predicted the image correctly!

First, we'll import the MobileNet predictor into our project by following :

The recommended way to make predictions with ML models is using . These are lightweight classes that 'know how' to format the model's input and output features.

We will fetch our models from , . To do so, we need to retrieve our access key:

the import instructions
NatML Hub
import NatML to your Unity project
MobileNet classifier
Predictors
Specifying your access key in a Unity project.
A Cat
Import the cat image.
Create an empty object and add the Classifier script we just created.
It's a cat!
Logo@natsuite/mobilenet-v2 - NatML
Get the MobileNet v2 predictor from NatML Hub.
Logohttps://hub.natml.ai/profile
Get your access key from NatML Hub