From 4976594a81c70a5f204579537bec7faa3075e141 Mon Sep 17 00:00:00 2001 From: RG-nAmKcAz Date: Mon, 16 Dec 2024 13:20:19 -0500 Subject: [PATCH] [REG-1936] Cleanup withinRect logic --- .../BotSegments/AIServiceManager.cs | 4 +- .../BotSegments/CVImageCriteriaEvaluator.cs | 18 +-------- .../BotSegments/CVObjectDetectionEvaluator.cs | 39 +++++++++---------- .../AIService/CVObjectDetectionResult.cs | 38 ------------------ .../AIService/CVObjectDetectionResult.cs.meta | 3 -- .../AIService/CVObjectDetectionResultList.cs | 9 ----- .../CVObjectDetectionResultList.cs.meta | 3 -- .../CVObjectDetectionMouseActionData.cs | 2 +- 8 files changed, 22 insertions(+), 94 deletions(-) delete mode 100644 src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResult.cs delete mode 100644 src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResult.cs.meta delete mode 100644 src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResultList.cs delete mode 100644 src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResultList.cs.meta diff --git a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/AIServiceManager.cs b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/AIServiceManager.cs index d219ed10f..084cb424d 100644 --- a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/AIServiceManager.cs +++ b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/AIServiceManager.cs @@ -78,7 +78,7 @@ await SendWebRequest( public async Task PostCriteriaObjectDetection(CVObjectDetectionRequest request, Action abortRegistrationHook, - Action> onSuccess, + Action> onSuccess, Action onFailure) { if (RGServiceManager.GetInstance().LoadAuth(out var authToken)) @@ -113,7 +113,7 @@ await SendWebRequest( abortRegistrationHook: abortRegistrationHook.Invoke, onSuccess: (s) => { - var response = JsonConvert.DeserializeObject(s, JsonUtils.JsonSerializerSettings); + var response = JsonConvert.DeserializeObject(s, JsonUtils.JsonSerializerSettings); onSuccess.Invoke(response.results); }, onFailure: (f) => diff --git a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/CVImageCriteriaEvaluator.cs b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/CVImageCriteriaEvaluator.cs index ca4e2725c..debab6c55 100644 --- a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/CVImageCriteriaEvaluator.cs +++ b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/CVImageCriteriaEvaluator.cs @@ -351,23 +351,7 @@ public static List Matched(int segmentNumber, List cri // we had the result for this criteria if (withinRect != null) { - foreach (var cvImageResult in cvImageResultList) - { - // ensure result rect is inside - var relativeScaling = new Vector2(withinRect.screenSize.x / (float)cvImageResult.resolution.x, withinRect.screenSize.y / (float)cvImageResult.resolution.y); - - // check the bottom left and top right to see if it intersects our rect - var bottomLeft = new Vector2Int(Mathf.CeilToInt(cvImageResult.rect.x * relativeScaling.x), Mathf.CeilToInt(cvImageResult.rect.y * relativeScaling.y)); - var topRight = new Vector2Int(bottomLeft.x + Mathf.FloorToInt(cvImageResult.rect.width * relativeScaling.x), bottomLeft.y + Mathf.FloorToInt(cvImageResult.rect.height * relativeScaling.y)); - - // we currently test overlap, should we test fully inside instead ?? - if (withinRect.rect.Contains(bottomLeft) || withinRect.rect.Contains(topRight)) - { - found = true; - break; // we found one, we can stop - } - } - + found = CVObjectDetectionEvaluator.DidMatchInsideWithinRect(cvImageResultList, withinRect); if (!found) { resultList.Add($"CV Image result for criteria at index: {i} was not found withinRect: {RectIntJsonConverter.ToJsonString(withinRect.rect)}"); diff --git a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/CVObjectDetectionEvaluator.cs b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/CVObjectDetectionEvaluator.cs index fae34e996..2d1ea34d8 100644 --- a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/CVObjectDetectionEvaluator.cs +++ b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/CVObjectDetectionEvaluator.cs @@ -25,7 +25,7 @@ public static class CVObjectDetectionEvaluator // if an entry is NULL, request is in progress for that segment // if an entry has a value, then it is completed for that segment.. it should be cleared out on the next matched call if the result didn't match so it can run again - private static readonly Dictionary>> _queryResultTracker = new(); + private static readonly Dictionary>> _queryResultTracker = new(); private static readonly Dictionary> _priorResultsTracker = new(); @@ -86,7 +86,7 @@ public static List Matched(int segmentNumber, List cri { RGDebug.LogVerbose($"CVObjectDetectionEvaluator - Matched - botSegment: {segmentNumber} - BEGIN"); - ConcurrentDictionary> objectDetectionResults = null; + ConcurrentDictionary> objectDetectionResults = null; List priorResults = null; bool requestInProgress = false; @@ -158,7 +158,7 @@ public static List Matched(int segmentNumber, List cri private static List EvaluateResult( int segmentNumber, List criteriaList, - ConcurrentDictionary> objectDetectionResults, + ConcurrentDictionary> objectDetectionResults, List resultList) { int criteriaListCount = criteriaList.Count; @@ -220,35 +220,32 @@ private static List EvaluateResult( /// /// Checks if any of the CV object detection results match within the specified rectangle. /// - /// List of CV object detection results to check. + /// List of CV detection results to check. /// The rectangle constraint to check against. /// True if a match is found within the specified rectangle, otherwise false. /// /// This method scales the detection results to match the withinRect's screen size, - /// then checks if either the bottom-left or top-right corner of the scaled result - /// is contained within the specified rectangle. It stops checking after finding + /// then checks if the shape overlaps the withinRect in any way. It stops checking after finding /// the first match. /// - private static bool DidMatchInsideWithinRect(List cvImageResultList, CVWithinRect withinRect) + public static bool DidMatchInsideWithinRect(List cvImageResultList, CVWithinRect withinRect) { bool found = false; foreach (var cvImageResult in cvImageResultList) { - // ensure result rect is inside - var relativeScaling = new Vector2(withinRect.screenSize.x / (float)cvImageResult.resolution.x, - withinRect.screenSize.y / (float)cvImageResult.resolution.y); - - // check the bottom left and top right to see if it intersects our rect - var bottomLeft = new Vector2Int(Mathf.CeilToInt(cvImageResult.rect.x * relativeScaling.x), - Mathf.CeilToInt(cvImageResult.rect.y * relativeScaling.y)); - var topRight = new Vector2Int(bottomLeft.x + Mathf.FloorToInt(cvImageResult.rect.width * relativeScaling.x), - bottomLeft.y + Mathf.FloorToInt(cvImageResult.rect.height * relativeScaling.y)); - - // we currently test overlap, should we test fully inside instead ?? - if (withinRect.rect.Contains(bottomLeft) || withinRect.rect.Contains(topRight)) + var relativeScaling = new Vector2(withinRect.screenSize.x / (float)cvImageResult.resolution.x, withinRect.screenSize.y / (float)cvImageResult.resolution.y); + + var minX = Mathf.CeilToInt(cvImageResult.rect.x * relativeScaling.x); + var maxX = Mathf.FloorToInt((cvImageResult.rect.x + cvImageResult.rect.width) * relativeScaling.x); + + var minY = Mathf.CeilToInt(cvImageResult.rect.y * relativeScaling.y); + var maxY = Mathf.FloorToInt((cvImageResult.rect.y + cvImageResult.rect.height) * relativeScaling.y); + + // we test that the shapes overlap, as long as the result in some way overlaps the withinRect, it passes + if (withinRect.rect.xMin <= maxX && withinRect.rect.xMax >= minX && withinRect.rect.yMin <= maxY && withinRect.rect.yMax >= minY ) { found = true; - break; // we found one, we can stop. + break; // we found one, we can stop } } return found; @@ -393,7 +390,7 @@ private static void AbortRegistrationHook(int segmentNumber, int index, Action a /// It stores the results, cleans up the request tracker, and removes completed requests. /// The method ensures thread-safety by using a lock on the _requestTracker. /// - private static void OnSuccess(int segmentNumber, int index, List list) + private static void OnSuccess(int segmentNumber, int index, List list) { RGDebug.LogVerbose($"CVObjectDetectionEvaluator - Matched - botSegment: {segmentNumber}, index: {index} - Request - onSuccess callback"); lock (_requestTracker) diff --git a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResult.cs b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResult.cs deleted file mode 100644 index 2dfe82c7f..000000000 --- a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResult.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Text; -using RegressionGames.StateRecorder.JsonConverters; -using UnityEngine; - -namespace RegressionGames.StateRecorder.BotSegments.Models.AIService -{ - public class CVObjectDetectionResult - { - /// - /// The resolution of the image in which the object was detected. - /// - public Vector2Int resolution; - - /// - /// The bounding box of the detected object within the image. - /// - public RectInt rect; - - /// - /// The segment index associated with this detection result. - /// - public int index; - - public override string ToString() - { - StringBuilder sb = new(1000); - sb.Append("{\"resolution\":"); - Vector2IntJsonConverter.WriteToStringBuilder(sb, resolution); - sb.Append(",\"rect\":"); - RectIntJsonConverter.WriteToStringBuilder(sb, rect); - sb.Append(",\"index\":"); - IntJsonConverter.WriteToStringBuilder(sb, index); - sb.Append("}"); - - return sb.ToString(); - } - } -} diff --git a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResult.cs.meta b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResult.cs.meta deleted file mode 100644 index ddd4892da..000000000 --- a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResult.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 686d7246789c47bf9affcd0ecb120700 -timeCreated: 1724105951 \ No newline at end of file diff --git a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResultList.cs b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResultList.cs deleted file mode 100644 index bba7e797f..000000000 --- a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResultList.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; - -namespace RegressionGames.StateRecorder.BotSegments.Models.AIService -{ - public class CVObjectDetectionResultList - { - public List results; - } -} diff --git a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResultList.cs.meta b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResultList.cs.meta deleted file mode 100644 index 01a419478..000000000 --- a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/AIService/CVObjectDetectionResultList.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 4e2f355e61bd465e84b147bf6aa290d8 -timeCreated: 1724106526 \ No newline at end of file diff --git a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/BotActions/CVObjectDetectionMouseActionData.cs b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/BotActions/CVObjectDetectionMouseActionData.cs index 2fb070d1b..b091e94d6 100644 --- a/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/BotActions/CVObjectDetectionMouseActionData.cs +++ b/src/gg.regression.unity.bots/Runtime/Scripts/StateRecorder/BotSegments/Models/BotActions/CVObjectDetectionMouseActionData.cs @@ -211,7 +211,7 @@ private void OnFailure(int segmentNumber) /// /// The segment number of the bot action, used for logging and debugging purposes. /// A list of CVObjectDetectionResult objects returned from the CV evaluation. - private void OnSuccess(int segmentNumber, List list) + private void OnSuccess(int segmentNumber, List list) { RGDebug.LogDebug($"CVObjectDetectionMouseActionData - RequestCVObjectDetectionEvaluation - botSegment: {segmentNumber} - Request - onSuccess callback"); lock (_syncLock)