The IXrAiObjectDetector
interface defines the contract for AI models that detect and locate objects within images. This interface returns bounding box information for detected objects.
public interface IXrAiObjectDetector
Analyzes an image and detects objects, returning their locations and classifications.
public Task<XrAiResult<XrAiBoundingBox[]>> Execute(Texture2D texture, Dictionary<string, string> options = null)
Parameters:
texture
(Texture2D): The input Unity texture containing the image to analyzeoptions
(Dictionary<string, string>, optional): Model-specific options and parametersReturns:
Task<XrAiResult<XrAiBoundingBox[]>>
: A task that resolves to a result containing an array of detected object bounding boxes// Load the model
IXrAiObjectDetector objectDetector = XrAiFactory.LoadObjectDetector("Yolo", new Dictionary<string, string>
{
{ "confidence", "0.5" },
{ "threshold", "0.4" }
}, assets);
// Execute object detection
var result = await objectDetector.Execute(inputTexture, new Dictionary<string, string>
{
{ "maxDetections", "10" }
});
// Handle the result
if (result.IsSuccess)
{
XrAiBoundingBox[] detections = result.Data;
foreach (var detection in detections)
{
Debug.Log($"Detected {detection.ClassName} at ({detection.CenterX}, {detection.CenterY}) " +
$"with size {detection.Width}x{detection.Height}");
}
// Draw bounding boxes on UI
XrAiObjectDetectorHelper.DrawBoxes(parentTransform, detections, scale, dimensions);
}
else
{
Debug.LogError($"Object detection failed: {result.ErrorMessage}");
}
Cloud-based object detection service.
Required Options:
apiKey
: Your Google Cloud API keyOptional Parameters:
url
: Custom API endpoint URLLocal inference using Sentis and YOLO models.
Required:
assets
: XrAiAssets component with model referencesOptional Parameters:
Cloud-based object detection with custom models.
Required Options:
apiKey
: Your Roboflow API keyOptional Parameters:
url
: API endpoint URLLocal inference using Roboflow-trained models.
Required Options:
apiKey
: Authentication keyOptional Parameters:
url
: Local service endpointThe XrAiBoundingBox
structure contains:
CenterX
: X coordinate of the bounding box centerCenterY
: Y coordinate of the bounding box centerWidth
: Width of the bounding boxHeight
: Height of the bounding boxClassName
: The detected object class/category nameCoordinates are typically normalized (0.0 to 1.0) relative to the image dimensions.
Use the XrAiObjectDetectorHelper
class to visualize detection results:
// Clear previous boxes
XrAiObjectDetectorHelper.ClearBoxes(parentTransform);
// Draw new detection boxes
XrAiObjectDetectorHelper.DrawBoxes(
parentTransform,
detections,
new Vector2(imageWidth, imageHeight), // scale
new Vector2(displayWidth, displayHeight) // dimensions
);
Task
XrAiResult<XrAiBoundingBox[]>
for consistent error handlingconfidence
: Minimum confidence threshold for detections (0.0 to 1.0)threshold
: Non-maximum suppression thresholdmaxDetections
: Maximum number of objects to detectclasses
: Specific object classes to detect (provider-dependent)