public async Task <(IEnumerable <Violation> Violations, IEnumerable <Human> Humans)> Detect(Mat image) { return(await Task.Run(delegate { lock (detectorLockLink) { var humans = new List <Human>(); var violations = new List <Violation>(); try { var items = detector.Detect(image); foreach (var(Confidence, CenterX, CenterY, Width, Height) in items) { var human = new Human(CenterX - (Width / 2), CenterY - (Height / 2), Width, Height, image.Width, image.Height); var pers = session.GridProjection.Perspective(human.BottomCenter); if (pers.HasValue) { human.PerspectivePoint = pers.Value; humans.Add(human); } } foreach (var i in humans) { foreach (var j in humans) { if (i == j) { continue; } if (violations.Any(v => (i.BottomCenter == v.Line.A && j.BottomCenter == v.Line.B) || (i.BottomCenter == v.Line.B && j.BottomCenter == v.Line.A) || (j.BottomCenter == v.Line.A && i.BottomCenter == v.Line.B) || (j.BottomCenter == v.Line.B && i.BottomCenter == v.Line.A))) { continue; } if (session.GridProjection.IsProjectionReady) { double dis = GeometryHelpers.GetDistance(i.PerspectivePoint, j.PerspectivePoint); if (dis < ViolationThreshold) { i.IsViolation = true; j.IsViolation = true; violations.Add(new Violation(new RelativeLine(i.BottomCenter, j.BottomCenter), dis)); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return (violations, humans); } }));