151 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			151 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| 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}";
 | |
|             _drawRect.CreateRect(rect, _colors[i % _colors.Length], label);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void OnDestroy()
 | |
|     {
 | |
|         if (_objectDetectionManager != null)
 | |
|         {
 | |
|             _objectDetectionManager.ObjectDetectionsUpdated -= ObjectDetectionsUpdated;
 | |
|         }
 | |
|     }
 | |
| }
 | 
