Skip to content

Commit

Permalink
fix: acquire a lock when drawing DetectionResult annotations (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler authored Jan 12, 2025
1 parent d80f33a commit c8f61ce
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ internal static void Copy(in NativeDetection source, ref Detection destination)
destination = new Detection(categories, boundingBox, keypoints);
}

public void CloneTo(ref Detection destination)
{
if (categories == null)
{
destination = default;
return;
}

var dstCategories = destination.categories ?? new List<Category>(categories.Count);
dstCategories.Clear();
dstCategories.AddRange(categories);

var dstKeypoints = destination.keypoints;
if (keypoints != null)
{
dstKeypoints ??= new List<NormalizedKeypoint>(keypoints.Count);
dstKeypoints.Clear();
dstKeypoints.AddRange(keypoints);
}

destination = new Detection(dstCategories, boundingBox, dstKeypoints);
}

public override string ToString()
=> $"{{ \"categories\": {Util.Format(categories)}, \"boundingBox\": {boundingBox}, \"keypoints\": {Util.Format(keypoints)} }}";
}
Expand Down Expand Up @@ -173,6 +196,20 @@ internal static void Copy(NativeDetectionResult source, ref DetectionResult dest
destination = new DetectionResult(detections);
}

public void CloneTo(ref DetectionResult destination)
{
if (detections == null)
{
destination = default;
return;
}

var dstDetections = destination.detections ?? new List<Detection>(detections.Count);
dstDetections.CopyFrom(detections);

destination = new DetectionResult(dstDetections);
}

public override string ToString() => $"{{ \"detections\": {Util.Format(detections)} }}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ public static void CopyFrom(this List<Classifications> target, List<Classificati
}
}

public static void CopyFrom(this List<Detection> target, List<Detection> source)
{
target.ResizeTo(source.Count);

var i = 0;
foreach (var x in source)
{
var v = target[i];
x.CloneTo(ref v);
target[i++] = v;
}
}

public static void CopyFrom(this List<Landmarks> target, List<Landmarks> source)
{
target.ResizeTo(source.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class DetectionResultAnnotationController : AnnotationController<Detectio
{
[SerializeField, Range(0, 1)] private float _threshold = 0.0f;

private readonly object _currentTargetLock = new object();
private mptcc.DetectionResult _currentTarget;

public void DrawNow(mptcc.DetectionResult target)
Expand All @@ -22,12 +23,24 @@ public void DrawNow(mptcc.DetectionResult target)
SyncNow();
}

public void DrawLater(mptcc.DetectionResult target) => UpdateCurrentTarget(target, ref _currentTarget);
public void DrawLater(mptcc.DetectionResult target) => UpdateCurrentTarget(target);

protected void UpdateCurrentTarget(mptcc.DetectionResult newTarget)
{
lock (_currentTargetLock)
{
newTarget.CloneTo(ref _currentTarget);
isStale = true;
}
}

protected override void SyncNow()
{
isStale = false;
annotation.Draw(_currentTarget, imageSize, _threshold);
lock (_currentTargetLock)
{
isStale = false;
annotation.Draw(_currentTarget, imageSize, _threshold);
}
}
}
}

0 comments on commit c8f61ce

Please sign in to comment.