Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UI = UnityEngine.UI;
- public class IRCamTest : MonoBehaviour
- {
- [SerializeField] Camera cam;
- [SerializeField] Vector2Int _resolution = new Vector2Int(1280, 720);
- [SerializeField] int _decimation = 4;
- [SerializeField] float _tagSize = 0.05f;
- [SerializeField] Material _tagMaterial = null;
- [SerializeField] UI.Text _debugText = null;
- [SerializeField] UI.RawImage _webcamPreview = null;
- RenderTexture _webcamBuffer;
- Texture2D output;
- Texture2D source;
- Color32[] _readBuffer;
- // AprilTag detector and drawer
- AprilTag.TagDetector _detector;
- TagDrawer _drawer;
- // Start is called before the first frame update
- void Start()
- {
- //Set up detector and drawer
- _detector = new AprilTag.TagDetector(_resolution.x, _resolution.y, _decimation);
- _drawer = new TagDrawer(_tagMaterial);
- //Create output texture and assign to rawimage
- _webcamBuffer = new RenderTexture(_resolution.x, _resolution.y, 0);
- _webcamPreview.texture = _webcamBuffer;
- //2D tex for conversion purposes
- output = new Texture2D(_resolution.x, _resolution.y);
- //Init this. Important, dont init this and it crashes because you're feeding it a null array
- _readBuffer = new Color32[_resolution.x * _resolution.y];
- }
- void OnDestroy()
- {
- _detector.Dispose();
- _drawer.Dispose();
- }
- //Get texture from sensor (runs once on sensor connect)
- public void GetData (Texture _source)
- {
- source = (Texture2D)_source;
- }
- // Update is called once per frame
- void Update()
- {
- //Takes a few frames to get the sensor tex
- if (source == null)
- {
- return;
- }
- //Get latest read from source texture
- _readBuffer = source.GetPixels32();
- //Assign it to the output
- output.SetPixels32(_readBuffer);
- output.Apply();
- Graphics.Blit(output, _webcamBuffer);
- //Get up to date FOV. Realsense D430 horizontal FOV = 86 degrees
- var fov = cam.fieldOfView * Mathf.Deg2Rad;
- _detector.ProcessImage(_readBuffer, fov, _tagSize);
- //Left this in, but changed so much, not sure if it works.
- foreach (var tag in _detector.DetectedTags)
- _drawer.Draw(tag.ID, tag.Position, tag.Rotation, _tagSize);
- if (Time.frameCount % 30 == 0)
- {
- //Output found tags.
- _debugText.text = $"Tags: {_detector.DetectedTags.Count()}";
- foreach (var tag in _detector.DetectedTags)
- {
- _debugText.text += $"\n{tag.ID}, {tag.Position}, {tag.Rotation}";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement