Skip to content

Commit

Permalink
[REG-1936] Cleanup withinRect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nAmKcAz committed Dec 16, 2024
1 parent 1165802 commit 4976594
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ await SendWebRequest(

public async Task PostCriteriaObjectDetection(CVObjectDetectionRequest request,
Action<Action> abortRegistrationHook,
Action<List<CVObjectDetectionResult>> onSuccess,
Action<List<CVImageResult>> onSuccess,
Action onFailure)
{
if (RGServiceManager.GetInstance().LoadAuth(out var authToken))
Expand Down Expand Up @@ -113,7 +113,7 @@ await SendWebRequest(
abortRegistrationHook: abortRegistrationHook.Invoke,
onSuccess: (s) =>
{
var response = JsonConvert.DeserializeObject<CVObjectDetectionResultList>(s, JsonUtils.JsonSerializerSettings);
var response = JsonConvert.DeserializeObject<CVImageResultList>(s, JsonUtils.JsonSerializerSettings);
onSuccess.Invoke(response.results);
},
onFailure: (f) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,23 +351,7 @@ public static List<string> Matched(int segmentNumber, List<KeyFrameCriteria> 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)}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, ConcurrentDictionary<int, List<CVObjectDetectionResult>>> _queryResultTracker = new();
private static readonly Dictionary<int, ConcurrentDictionary<int, List<CVImageResult>>> _queryResultTracker = new();

private static readonly Dictionary<int, List<string>> _priorResultsTracker = new();

Expand Down Expand Up @@ -86,7 +86,7 @@ public static List<string> Matched(int segmentNumber, List<KeyFrameCriteria> cri
{
RGDebug.LogVerbose($"CVObjectDetectionEvaluator - Matched - botSegment: {segmentNumber} - BEGIN");

ConcurrentDictionary<int, List<CVObjectDetectionResult>> objectDetectionResults = null;
ConcurrentDictionary<int, List<CVImageResult>> objectDetectionResults = null;
List<string> priorResults = null;
bool requestInProgress = false;

Expand Down Expand Up @@ -158,7 +158,7 @@ public static List<string> Matched(int segmentNumber, List<KeyFrameCriteria> cri
private static List<string> EvaluateResult(
int segmentNumber,
List<KeyFrameCriteria> criteriaList,
ConcurrentDictionary<int, List<CVObjectDetectionResult>> objectDetectionResults,
ConcurrentDictionary<int, List<CVImageResult>> objectDetectionResults,
List<string> resultList)
{
int criteriaListCount = criteriaList.Count;
Expand Down Expand Up @@ -220,35 +220,32 @@ private static List<string> EvaluateResult(
/// <summary>
/// Checks if any of the CV object detection results match within the specified rectangle.
/// </summary>
/// <param name="cvImageResultList">List of CV object detection results to check.</param>
/// <param name="cvImageResultList">List of CV detection results to check.</param>
/// <param name="withinRect">The rectangle constraint to check against.</param>
/// <returns>True if a match is found within the specified rectangle, otherwise false.</returns>
/// <remarks>
/// 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.
/// </remarks>
private static bool DidMatchInsideWithinRect(List<CVObjectDetectionResult> cvImageResultList, CVWithinRect withinRect)
public static bool DidMatchInsideWithinRect(List<CVImageResult> 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;
Expand Down Expand Up @@ -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.
/// </remarks>
private static void OnSuccess(int segmentNumber, int index, List<CVObjectDetectionResult> list)
private static void OnSuccess(int segmentNumber, int index, List<CVImageResult> list)
{
RGDebug.LogVerbose($"CVObjectDetectionEvaluator - Matched - botSegment: {segmentNumber}, index: {index} - Request - onSuccess callback");
lock (_requestTracker)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private void OnFailure(int segmentNumber)
/// </summary>
/// <param name="segmentNumber">The segment number of the bot action, used for logging and debugging purposes.</param>
/// <param name="list">A list of CVObjectDetectionResult objects returned from the CV evaluation.</param>
private void OnSuccess(int segmentNumber, List<CVObjectDetectionResult> list)
private void OnSuccess(int segmentNumber, List<CVImageResult> list)
{
RGDebug.LogDebug($"CVObjectDetectionMouseActionData - RequestCVObjectDetectionEvaluation - botSegment: {segmentNumber} - Request - onSuccess callback");
lock (_syncLock)
Expand Down

0 comments on commit 4976594

Please sign in to comment.