Skip to content

Instantly share code, notes, and snippets.

Avatar

Yusuf Olokoba olokobayusuf

View GitHub Profile
@olokobayusuf
olokobayusuf / MLPredcitor.cs
Created May 10, 2021
Creating a predictor for making predictions with ML models.
View MLPredcitor.cs
// Create a classification predictor
var labels = new [] { "cat", "dog", ... };
var predictor = new MLClassificationPredictor(model, labels);
@olokobayusuf
olokobayusuf / MLModules.cs
Created Apr 24, 2021
Modules which simplify model usage in NatML.
View MLModules.cs
// Create a classifier module
var labels = new [] { "goldfish", "tiger shark", ... };
var classifier = new MLClassifier("/path/to/classifier.onnx", labels);
// Classify an image
Texture2D image = ...;
var (label, confidence) = classifier.Predict(image);
Debug.Log($"Image contains {label} with confidence: {confidence}");
@olokobayusuf
olokobayusuf / MLPredict.cs
Last active Sep 12, 2021
Make predictions with a NatML model.
View MLPredict.cs
// If you have some feature
Texture2D image = ...;
// You can predict easily
var (label, confidence) = predictor.Predict(image);
Debug.Log($"Image contains {label} with confidence {confidence}");
@olokobayusuf
olokobayusuf / HelloML.cs
Created Apr 24, 2021
The core API in NatML.
View HelloML.cs
// Create an ML model
var model = new MLModel("/path/to/model.onnx");
// Print a model metadata value
Debug.Log("The model was created by: " + model["Author"]);
// Check the expected input size
var inputType = model.inputs[0] as MLImageType;
Debug.Log($"Model expects image with size: {inputType.width}x{inputType.height}");
@olokobayusuf
olokobayusuf / ThreadedAsyncTextureInput.cs
Last active Apr 21, 2021
Texture input that uses asynchronous readbacks, but commits frames on a background thread.
View ThreadedAsyncTextureInput.cs
using NatSuite.Recorders.Inputs;
/// <summary>
/// Recorder input for recording video frames from textures.
/// </summary>
public class ThreadedAsyncTextureInput : ITextureInput {
#region --Client API--
/// <summary>
/// Create a texture input which performs asynchronous readbacks.
@olokobayusuf
olokobayusuf / Galaxy.txt
Created Jun 2, 2020
Testing the supported resolutions of Unity's WebCamTexture.
View Galaxy.txt
Camera2: Hardware level: 1
06-02 10:49:07.625 21849 21865 I Unity : Camera2: FrameSize 4032 x 3024 [2.5824869781268642]
06-02 10:49:07.625 21849 21865 I Unity : Camera2: FrameSize 4032 x 2268 [2.2948049056750834]
06-02 10:49:07.625 21849 21865 I Unity : Camera2: FrameSize 3984 x 2988 [2.558534596033433]
06-02 10:49:07.625 21849 21865 I Unity : Camera2: FrameSize 3264 x 2448 [2.1598687907924505]
06-02 10:49:07.626 21849 21865 I Unity : Camera2: FrameSize 3264 x 1836 [1.8721867183406695]
06-02 10:49:07.626 21849 21865 I Unity : Camera2: FrameSize 3024 x 3024 [2.2948049056750834]
06-02 10:49:07.626 21849 21865 I Unity : Camera2: FrameSize 2976 x 2976 [2.2628042229822007]
06-02 10:49:07.626 21849 21865 I Unity : Camera2: FrameSize 2880 x 2160 [1.9095425048844386]
06-02 10:49:07.626 21849 21865 I Unity : Camera2: FrameSize 2592 x 1944 [1.698821473568786]
@olokobayusuf
olokobayusuf / CameraTextureInput.cs
Last active May 8, 2020
A recorder input that blits the camera view instead of re-rendering it. It performs letterboxing for aspect ratio mismatches.
View CameraTextureInput.cs
/*
* NatCorder
* Copyright (c) 2020 Yusuf Olokoba.
*/
namespace NatSuite.Recorders.Inputs {
using UnityEngine;
using UnityEngine.Rendering;
@olokobayusuf
olokobayusuf / StreamCamera.cs
Created Feb 15, 2020
Streaming the camera preview from a camera device in NatDevice.
View StreamCamera.cs
async void StartPreview () {
// Start the camera preview
ICameraDevice device = ...;
Texture2D previewTexture = await device.StartRunning();
// Display the preview texture on a UI panel
rawImage.texture = previewTexture;
}
@olokobayusuf
olokobayusuf / ICameraDevice.cs
Created Feb 15, 2020
The fundamental camera device interface in NatDevice, which all camera devices implement.
View ICameraDevice.cs
/// <summary>
/// Camera device which provides pixel buffers.
/// </summary>
public interface ICameraDevice : IMediaDevice {
/// <summary>
/// Is the camera front facing?
/// </summary>
bool frontFacing { get; }
@olokobayusuf
olokobayusuf / RecordAudio.cs
Last active Feb 15, 2020
Streaming audio sample buffers from an audio device in NatDevice.
View RecordAudio.cs
void StartRecording () {
// Start recording audio from the microphone
IAudioDevice device = ...;
device.StartRunning(OnSampleBuffer);
}
void OnSampleBuffer (float[] sampleBuffer, long timestamp) {
// `sampleBuffer` is linear PCM, interleaved by channel
}