Removed TOTU 103
This commit is contained in:
46
Assets/Scripts/ObjectDetection/DrawRect.cs
Normal file
46
Assets/Scripts/ObjectDetection/DrawRect.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2022-2024 Niantic.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class DrawRect : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject _rectanglePrefab;
|
||||
|
||||
private List<UIRectObject> _rectangleObjects = new List<UIRectObject>();
|
||||
private List<int> _openIndices = new List<int>();
|
||||
|
||||
public void CreateRect(Rect rect, Color color, string text)
|
||||
{
|
||||
if (_openIndices.Count == 0)
|
||||
{
|
||||
var newRect = Instantiate(_rectanglePrefab, parent: this.transform).GetComponent<UIRectObject>();
|
||||
|
||||
_rectangleObjects.Add(newRect);
|
||||
_openIndices.Add(_rectangleObjects.Count - 1);
|
||||
}
|
||||
|
||||
// Treat the first index as a queue
|
||||
int index = _openIndices[0];
|
||||
_openIndices.RemoveAt(0);
|
||||
|
||||
UIRectObject rectangle = _rectangleObjects[index];
|
||||
rectangle.SetRectTransform(rect);
|
||||
rectangle.SetColor(color);
|
||||
rectangle.SetText(text);
|
||||
rectangle.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void ClearRects()
|
||||
{
|
||||
for (var i = 0; i < _rectangleObjects.Count; i++)
|
||||
{
|
||||
_rectangleObjects[i].gameObject.SetActive(false);
|
||||
_openIndices.Add(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/Scripts/ObjectDetection/DrawRect.cs.meta
Normal file
11
Assets/Scripts/ObjectDetection/DrawRect.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a81c7be58bc4e46f0af9022ac8704397
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
150
Assets/Scripts/ObjectDetection/ObjectDetectionSample.cs
Normal file
150
Assets/Scripts/ObjectDetection/ObjectDetectionSample.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using Niantic.Lightship.AR.ObjectDetection;
|
||||
using UnityEngine;
|
||||
|
||||
public class ObjectDetectionSample : MonoBehaviour
|
||||
{
|
||||
private ARObjectDetectionManager _objectDetectionManager;
|
||||
[SerializeField]
|
||||
private DrawRect _drawRect;
|
||||
|
||||
private Camera _mainCamera;
|
||||
private Canvas _canvas;
|
||||
|
||||
private Color[] _colors = new Color[]
|
||||
{
|
||||
Color.red, Color.blue, Color.green, Color.yellow, Color.magenta,
|
||||
Color.cyan, Color.white, Color.black
|
||||
};
|
||||
|
||||
private Vector3 _lastCameraPosition;
|
||||
private float _movementThreshold = 0.005f; // Adjust for sensitivity
|
||||
private float _detectionSlowdownRate = 0.0f; // Detection frame rate for stationary camera
|
||||
private float _detectionNormalRate = 1.0f; // Detection frame rate for moving camera
|
||||
private bool _isCameraMoving = false;
|
||||
private float confidencePercentage = 0.65f;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_mainCamera = Camera.main;
|
||||
if (_mainCamera == null)
|
||||
{
|
||||
Debug.LogError("Main Camera not found.");
|
||||
}
|
||||
|
||||
_objectDetectionManager = FindObjectOfType<ARObjectDetectionManager>();
|
||||
if (_objectDetectionManager == null)
|
||||
{
|
||||
Debug.LogError("ARObjectDetectionManager not found.");
|
||||
}
|
||||
|
||||
_canvas = FindObjectOfType<Canvas>();
|
||||
if (_canvas == null)
|
||||
{
|
||||
Debug.LogError("Canvas not found.");
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (_objectDetectionManager != null)
|
||||
{
|
||||
_objectDetectionManager.enabled = true;
|
||||
_objectDetectionManager.ObjectDetectionsUpdated += ObjectDetectionsUpdated;
|
||||
Debug.Log("Object detection started.");
|
||||
}
|
||||
|
||||
_lastCameraPosition = _mainCamera.transform.position;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
DetectCameraMovement();
|
||||
}
|
||||
|
||||
private void DetectCameraMovement()
|
||||
{
|
||||
if (_mainCamera == null) return;
|
||||
|
||||
Vector3 currentCameraPosition = _mainCamera.transform.position;
|
||||
float movement = Vector3.Distance(currentCameraPosition, _lastCameraPosition);
|
||||
|
||||
Debug.Log($"Camera Movement Detected: {movement}");
|
||||
|
||||
if (movement > _movementThreshold)
|
||||
{
|
||||
if (!_isCameraMoving)
|
||||
{
|
||||
// Resume full frame rate for detection
|
||||
_isCameraMoving = true;
|
||||
SetDetectionFrameRate(_detectionNormalRate);
|
||||
Debug.Log("Camera is moving, increasing detection rate.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_isCameraMoving)
|
||||
{
|
||||
// Slow down detection to save performance
|
||||
_isCameraMoving = false;
|
||||
SetDetectionFrameRate(_detectionSlowdownRate);
|
||||
Debug.Log("Camera is stationary, slowing detection rate.");
|
||||
}
|
||||
}
|
||||
|
||||
_lastCameraPosition = currentCameraPosition;
|
||||
}
|
||||
|
||||
private void SetDetectionFrameRate(float frameRate)
|
||||
{
|
||||
if (_objectDetectionManager != null)
|
||||
{
|
||||
_objectDetectionManager.TargetFrameRate = (uint)frameRate;
|
||||
}
|
||||
}
|
||||
|
||||
private void ObjectDetectionsUpdated(ARObjectDetectionsUpdatedEventArgs args)
|
||||
{
|
||||
if (_mainCamera == null || _objectDetectionManager == null) return;
|
||||
|
||||
var result = args.Results;
|
||||
if (result == null || result.Count == 0)
|
||||
{
|
||||
Debug.Log("No detections found.");
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log($"Detections found: {result.Count}");
|
||||
|
||||
_drawRect.ClearRects();
|
||||
|
||||
for (int i = 0; i < result.Count; i++)
|
||||
{
|
||||
var detection = result[i];
|
||||
if (detection.GetConfidentCategorizations().Count == 0) continue;
|
||||
|
||||
var categoryToDisplay = detection.GetConfidentCategorizations()[0];
|
||||
float confidence = categoryToDisplay.Confidence;
|
||||
|
||||
if (confidence <= confidencePercentage) continue; // Adjust threshold if needed
|
||||
|
||||
string categoryName = categoryToDisplay.CategoryName;
|
||||
|
||||
int h = Mathf.FloorToInt(_canvas.GetComponent<RectTransform>().rect.height);
|
||||
int w = Mathf.FloorToInt(_canvas.GetComponent<RectTransform>().rect.width);
|
||||
var rect = detection.CalculateRect(w, h, Screen.orientation);
|
||||
|
||||
Debug.Log($"Detected: {categoryName} with confidence {confidence * 100:F1}%");
|
||||
|
||||
string label = $"{categoryName} {confidence * 100:F1}%";
|
||||
_drawRect.CreateRect(rect, _colors[i % _colors.Length], label);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (_objectDetectionManager != null)
|
||||
{
|
||||
_objectDetectionManager.ObjectDetectionsUpdated -= ObjectDetectionsUpdated;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ObjectDetection/ObjectDetectionSample.cs.meta
Normal file
11
Assets/Scripts/ObjectDetection/ObjectDetectionSample.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ce4b4913151ce4b2594ebfe2f7be8abe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
43
Assets/Scripts/ObjectDetection/UIRectObject.cs
Normal file
43
Assets/Scripts/ObjectDetection/UIRectObject.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright 2022-2024 Niantic.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(RectTransform), typeof(Image))]
|
||||
public class UIRectObject : MonoBehaviour
|
||||
{
|
||||
private RectTransform _rectangleRectTransform;
|
||||
private Image _rectangleImage;
|
||||
private Text _text;
|
||||
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
_rectangleRectTransform = GetComponent<RectTransform>();
|
||||
_rectangleImage = GetComponent<Image>();
|
||||
_text = GetComponentInChildren<Text>();
|
||||
}
|
||||
|
||||
public void SetRectTransform(Rect rect)
|
||||
{
|
||||
_rectangleRectTransform.anchoredPosition = new Vector2(rect.x, rect.y);
|
||||
_rectangleRectTransform.sizeDelta = new Vector2(rect.width, rect.height);
|
||||
}
|
||||
|
||||
public void SetColor(Color color)
|
||||
{
|
||||
_rectangleImage.color = color;
|
||||
}
|
||||
|
||||
public void SetText(string text)
|
||||
{
|
||||
_text.text = text;
|
||||
}
|
||||
public RectTransform getRectTransform(){
|
||||
return _rectangleRectTransform;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ObjectDetection/UIRectObject.cs.meta
Normal file
11
Assets/Scripts/ObjectDetection/UIRectObject.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 326123b510dc140f48475dfc0d614976
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user