Show pose detection on the Overlay control
authorMarcin Romaniuk <m.romaniuk@samsung.com>
Mon, 31 May 2021 23:19:44 +0000 (01:19 +0200)
committerPiotr Czaja <p.czaja@samsung.com>
Tue, 14 Sep 2021 11:01:34 +0000 (13:01 +0200)
Fitness/Controls/Overlay.cs
Fitness/Views/ScanningView.cs

index bff22c2a30895b47400454ee26a990d523021ee7..adac62c9f5f51159ff4efe88f98111c8e5913849 100644 (file)
@@ -64,8 +64,8 @@ namespace Fitness.Controls
         {
             if (bindable is Overlay overlay && newValue is Landmark[,] landmark)
             {
-                int numberOfPeople = landmark.GetLength(1);
-                int numberOfBodyParts = landmark.GetLength(0);
+                int numberOfPeople = landmark.GetLength(0);
+                int numberOfBodyParts = landmark.GetLength(1);
                 int numberOfPeopleDisplayed = System.Math.Min(numberOfPeople, MaxNumberOfPeople);
 
                 for (int i = 0; i < numberOfPeopleDisplayed; i++)
@@ -77,8 +77,8 @@ namespace Fitness.Controls
                         overlay.AddDrawables(bodyMarker);
                     }
 
-                    var landmarksRow = Enumerable.Range(0, landmark.GetLength(0))
-                        .Select(x => landmark[x, i])
+                    var landmarksRow = Enumerable.Range(0, numberOfBodyParts)
+                        .Select(x => landmark[i, x])
                         .ToArray();
                     overlay.bodyMarkers[i].Update(landmarksRow, overlay.Size);
                 }
index 8c5d30b320cfffb03f492b199a8603457a960894..17375a61a293294bdc3738014da5c38a510123a5 100644 (file)
@@ -1,3 +1,4 @@
+using System.Linq;
 using System.Threading;
 using System.Threading.Tasks;
 using Fitness.Services;
@@ -36,14 +37,68 @@ namespace Fitness.Views
             }
         }
 
+        private static void LogPoseDetection(Tizen.Multimedia.Vision.Landmark[,] landmarks)
+        {
+            int numberOfPeople = landmarks.GetLength(0);
+            int numberOfBodyParts = landmarks.GetLength(1);
+
+            for (int i = 0; i < numberOfPeople; ++i)
+            {
+                var scores = Enumerable.Range(0, numberOfBodyParts)
+                    .Select(j => landmarks[i, j].Score);
+                float averageScore = scores.Average();
+                var trimmedScores = scores.Select(score => score.ToString(".000"));
+
+                Services.Logger.Debug($"scores: {string.Join(", ", trimmedScores)}; avg: {averageScore.ToString(".000")}");
+            }
+        }
+
         private async void DetectPreview(object sender, PreviewEventArgs e)
         {
             if (e.Preview.PlaneType == PlaneType.TriplePlane && Interlocked.Exchange(ref isInferencing, 1) == 0)
             {
-                await Task.Run(() => poseDetector.Detect(e.Preview.Plane as TriplePlane, (uint)e.Preview.Resolution.Height, (uint)e.Preview.Resolution.Width));
-                Interlocked.Exchange(ref isInferencing, 0);
+                var stopwatch = new System.Diagnostics.Stopwatch();
+                stopwatch.Start();
+
+                await Task.Run(async () =>
+                {
+                    try
+                    {
+                        var plane = e.Preview.Plane as TriplePlane;
+                        if (plane == null)
+                        {
+                            throw new System.Exception($"Unsupported plane type: {e.Preview.Plane.GetType()}");
+                        }
+
+                        uint width = (uint)e.Preview.Resolution.Width;
+                        uint height = (uint)e.Preview.Resolution.Height;
+
+                        var landmarks = await poseDetector.Detect(plane, height, width);
 
-                // TODO: draw line
+                        // TODO: Remove when pose detection logs are not required.
+                        LogPoseDetection(landmarks);
+
+                        NUIContext.InvokeOnMainThread(() =>
+                        {
+                            // TODO: Pose detection should be moved to the View Model, but now happens here at the View and updates the View Model.
+                            if (BindingContext is ViewModels.ScanningViewModel viewModel)
+                            {
+                                viewModel.PoseLandmarks = landmarks;
+                            }
+                        });
+                    }
+                    catch (System.Exception exception)
+                    {
+                        Services.Logger.Error(exception.Message);
+                    }
+                    finally
+                    {
+                        stopwatch.Stop();
+                        Services.Logger.Debug($"total time {stopwatch.ElapsedMilliseconds} msec");
+                    }
+                });
+
+                Interlocked.Exchange(ref isInferencing, 0);
             }
         }
     }