Skip to content

Instantly share code, notes, and snippets.

View olokobayusuf's full-sized avatar

Yusuf Olokoba olokobayusuf

View GitHub Profile
@olokobayusuf
olokobayusuf / GIFCreator.cs
Created November 20, 2023 13:42
Creating an animated GIF image from an array of textures in Unity with VideoKit.
public static async Task<MediaAsset> CreateGIF (Texture2D[] textures) {
// Make sure `textures` is not empty
// Make sure every texture has the same size (width and height)
// Make sure every texture has the `TextureFormat.RGBA32` format
// Create recorder
var width = textures[0].width;
var height = textures[0].height;
var frameRate = 5;
var recorder = await MediaRecorder.Create(
MediaFormat.GIF,
@olokobayusuf
olokobayusuf / RenderTextureDetect.cs
Created February 2, 2023 16:49
Performing object detection on the contents of a render texture with the YOLOX model.
void Detect (RenderTexture renderTexture) {
// Create the YOLOX model
var model = await MLEdgeModel.Create("@natsuite/yolox");
// Create the YOLOX predictor
var predictor = new YOLOXPredictor(model);
// Readback the render texture
AsyncGPUReadback.Request(renderTexture, request => {
// Create an image feature
var feature = new MLImageFeature(request.GetData<byte>(), request.width, request.height);
// Predict
@olokobayusuf
olokobayusuf / MatOutput.cs
Created April 17, 2022 23:06
Integrating NatDevice and OpenCV for Unity with a camera device output that streams camera images into an OpenCV matrix.
/*
* NatDevice
* Copyright (c) 2022 NatML Inc. All Rights Reserved.
*/
namespace NatSuite.Devices.Outputs {
using System;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.UtilsModule;
@olokobayusuf
olokobayusuf / MLPredict.cs
Last active September 24, 2021 20:00
Creating a predictor for making predictions with ML models.
// Create a classification predictor
var labels = new [] { "cat", "dog", ... };
var predictor = new MobileNetv2Predictor(model, labels);
@olokobayusuf
olokobayusuf / MLModules.cs
Created April 24, 2021 20:22
Modules which simplify model usage in NatML.
// 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 September 12, 2021 08:05
Make predictions with a NatML model.
// 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
Last active September 21, 2021 23:49
The core API in NatML.
// Load the ML model data
var modelData = MLModelData.FromFile("/path/to/model.onnx");
// Deserialize the model
var model = modelData.Deserialize();
// 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 April 21, 2021 13:13
Texture input that uses asynchronous readbacks, but commits frames on a background thread.
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 June 2, 2020 14:50
Testing the supported resolutions of Unity's WebCamTexture.
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 15:35
A recorder input that blits the camera view instead of re-rendering it. It performs letterboxing for aspect ratio mismatches.
/*
* NatCorder
* Copyright (c) 2020 Yusuf Olokoba.
*/
namespace NatSuite.Recorders.Inputs {
using UnityEngine;
using UnityEngine.Rendering;