Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: acquire a lock when drawing DetectionResult annotations #1306

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
Loading