Advertisement
Guest User

IR Cam Test using Keijiro's Unity Apriltag plugin

a guest
Feb 5th, 2021
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UI = UnityEngine.UI;
  6.  
  7. public class IRCamTest : MonoBehaviour
  8. {
  9.     [SerializeField] Camera cam;
  10.     [SerializeField] Vector2Int _resolution = new Vector2Int(1280, 720);
  11.     [SerializeField] int _decimation = 4;
  12.     [SerializeField] float _tagSize = 0.05f;
  13.     [SerializeField] Material _tagMaterial = null;
  14.     [SerializeField] UI.Text _debugText = null;
  15.     [SerializeField] UI.RawImage _webcamPreview = null;
  16.     RenderTexture _webcamBuffer;
  17.     Texture2D output;
  18.     Texture2D source;
  19.  
  20.     Color32[] _readBuffer;
  21.  
  22.     // AprilTag detector and drawer
  23.     AprilTag.TagDetector _detector;
  24.     TagDrawer _drawer;
  25.  
  26.     // Start is called before the first frame update
  27.     void Start()
  28.     {
  29.         //Set up detector and drawer
  30.         _detector = new AprilTag.TagDetector(_resolution.x, _resolution.y, _decimation);
  31.         _drawer = new TagDrawer(_tagMaterial);
  32.         //Create output texture and assign to rawimage
  33.         _webcamBuffer = new RenderTexture(_resolution.x, _resolution.y, 0);
  34.         _webcamPreview.texture = _webcamBuffer;
  35.         //2D tex for conversion purposes
  36.         output = new Texture2D(_resolution.x, _resolution.y);
  37.         //Init this. Important, dont init this and it crashes because you're feeding it a null array
  38.         _readBuffer = new Color32[_resolution.x * _resolution.y];
  39.     }
  40.  
  41.     void OnDestroy()
  42.     {
  43.         _detector.Dispose();
  44.         _drawer.Dispose();
  45.     }
  46.  
  47.     //Get texture from sensor (runs once on sensor connect)
  48.     public void GetData (Texture _source)
  49.     {
  50.         source = (Texture2D)_source;
  51.     }
  52.  
  53.     // Update is called once per frame
  54.     void Update()
  55.     {
  56.         //Takes a few frames to get the sensor tex
  57.         if (source == null)
  58.         {
  59.             return;
  60.         }
  61.  
  62.         //Get latest read from source texture
  63.         _readBuffer = source.GetPixels32();
  64.        
  65.         //Assign it to the output
  66.         output.SetPixels32(_readBuffer);
  67.         output.Apply();
  68.         Graphics.Blit(output, _webcamBuffer);
  69.  
  70.         //Get up to date FOV. Realsense D430 horizontal FOV = 86 degrees
  71.         var fov = cam.fieldOfView * Mathf.Deg2Rad;
  72.         _detector.ProcessImage(_readBuffer, fov, _tagSize);
  73.  
  74.         //Left this in, but changed so much, not sure if it works.
  75.         foreach (var tag in _detector.DetectedTags)
  76.             _drawer.Draw(tag.ID, tag.Position, tag.Rotation, _tagSize);
  77.  
  78.  
  79.         if (Time.frameCount % 30 == 0)
  80.         {
  81.             //Output found tags.
  82.             _debugText.text = $"Tags: {_detector.DetectedTags.Count()}";
  83.             foreach (var tag in _detector.DetectedTags)
  84.             {
  85.                 _debugText.text += $"\n{tag.ID}, {tag.Position}, {tag.Rotation}";
  86.             }
  87.         }
  88.            
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement