[MediaVision] Add new APIs for ROI tracker (#4523)
authorHaesu Gwon <haesu.gwon@samsung.com>
Thu, 8 Sep 2022 01:26:40 +0000 (10:26 +0900)
committerGitHub <noreply@github.com>
Thu, 8 Sep 2022 01:26:40 +0000 (10:26 +0900)
* [MediaVision] Add new APIs for ROI tracker

src/Tizen.Multimedia.Vision/Interop/Interop.MediaVision.RoiTracker.cs [new file with mode: 0644]
src/Tizen.Multimedia.Vision/MediaVision/RoiTracker.cs [new file with mode: 0644]
src/Tizen.Multimedia.Vision/MediaVision/RoiTrackingConfiguration.cs [new file with mode: 0755]
src/Tizen.Multimedia.Vision/MediaVision/VisionFeatures.cs

diff --git a/src/Tizen.Multimedia.Vision/Interop/Interop.MediaVision.RoiTracker.cs b/src/Tizen.Multimedia.Vision/Interop/Interop.MediaVision.RoiTracker.cs
new file mode 100644 (file)
index 0000000..4dce28f
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Runtime.InteropServices;
+using Tizen.Multimedia.Vision;
+
+/// <summary>
+/// Interop APIs.
+/// </summary>
+internal static partial class Interop
+{
+    /// <summary>
+    /// Interop for Media Vision APIs.
+    /// </summary>
+    internal static partial class MediaVision
+    {
+        /// <summary>
+        /// Interop for ROI tracker APIs.
+        /// </summary>
+        internal static partial class RoiTracker
+        {
+            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+            internal delegate void RoiTrackedCallback(IntPtr source, Rectangle roi, IntPtr userData = default);
+
+            [DllImport(Libraries.MediaVisionRoiTracker, EntryPoint = "mv_roi_tracker_create")]
+            internal static extern MediaVisionError Create(out IntPtr handle);
+
+            [DllImport(Libraries.MediaVisionRoiTracker, EntryPoint = "mv_roi_tracker_destroy")]
+            internal static extern MediaVisionError Destroy(IntPtr handle);
+
+            [DllImport(Libraries.MediaVisionRoiTracker, EntryPoint = "mv_roi_tracker_configure")]
+            internal static extern MediaVisionError Configure(IntPtr handle, IntPtr engineConfig);
+
+            [DllImport(Libraries.MediaVisionRoiTracker, EntryPoint = "mv_roi_tracker_prepare")]
+            internal static extern MediaVisionError Prepare(IntPtr handle, int x, int y, int width, int height);
+
+            [DllImport(Libraries.MediaVisionRoiTracker, EntryPoint = "mv_roi_tracker_perform")]
+            internal static extern MediaVisionError TrackRoi(IntPtr handle, IntPtr source, RoiTrackedCallback callback,
+                IntPtr userData = default);
+        }
+    }
+}
diff --git a/src/Tizen.Multimedia.Vision/MediaVision/RoiTracker.cs b/src/Tizen.Multimedia.Vision/MediaVision/RoiTracker.cs
new file mode 100644 (file)
index 0000000..0491fec
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Threading.Tasks;
+using InteropRoi = Interop.MediaVision.RoiTracker;
+
+namespace Tizen.Multimedia.Vision
+{
+    /// <summary>
+    /// Provides the ability to track ROI(Region Of Interest) area on image source.
+    /// </summary>
+    /// <since_tizen> 10 </since_tizen>
+    public static class RoiTracker
+    {
+        /// <summary>Tracks ROI(Region Of Interest) on the source image.</summary>
+        /// <remarks><see cref="RoiTrackingConfiguration.Roi"/> should be set first correctly.</remarks>
+        /// <feature>http://tizen.org/feature/vision.roi_tracking</feature>
+        /// <param name="source">The source of the media user wants to track.</param>
+        /// <param name="config">The engine's configuration that will be used for tracking.</param>
+        /// <returns>A task that represents the asynchronous tracking operation.</returns>
+        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="config"/> is null.</exception>
+        /// <exception cref="ArgumentException"><paramref name="config.Roi"/> is not set correctly.</exception>
+        /// <exception cref="InvalidOperationException">Internal error.</exception>
+        /// <seealso cref="RoiTrackingConfiguration"/>
+        /// <since_tizen> 10 </since_tizen>
+        public static async Task<Rectangle> TrackAsync(MediaVisionSource source, RoiTrackingConfiguration config)
+        {
+            if (source == null)
+            {
+                throw new ArgumentNullException(nameof(source));
+            }
+            if (config == null)
+            {
+                throw new ArgumentNullException(nameof(config));
+            }
+            if (config._roi == null)
+            {
+                throw new ArgumentException(nameof(config.Roi));
+            }
+
+            if (!config.IsApplied)
+            {
+                // Configure() and Prepare() can be called every time but it's useless, because Native Vision FW can accept it once.
+                InteropRoi.Configure(config.GetRoiConfigHandle(), config.Handle).
+                    Validate("Failed to configure roi tracking");
+
+                var initialRoi = config.Roi;
+                InteropRoi.Prepare(config.GetRoiConfigHandle(), initialRoi.X, initialRoi.Y, initialRoi.Width, initialRoi.Height).
+                    Validate("Failed to prepare roi tracking");
+
+                config.IsApplied = true;
+            }
+
+            var tcs = new TaskCompletionSource<Rectangle>();
+
+            InteropRoi.RoiTrackedCallback callback = (handle, roi_, _) => tcs.TrySetResult(roi_.ToApiStruct());
+
+            using (var cb = ObjectKeeper.Get(callback))
+            {
+                InteropRoi.TrackRoi(config.GetRoiConfigHandle(), source.Handle, cb.Target).
+                    Validate("Failed to track roi");
+
+                return await tcs.Task;
+            }
+        }
+    }
+}
diff --git a/src/Tizen.Multimedia.Vision/MediaVision/RoiTrackingConfiguration.cs b/src/Tizen.Multimedia.Vision/MediaVision/RoiTrackingConfiguration.cs
new file mode 100755 (executable)
index 0000000..793b07b
--- /dev/null
@@ -0,0 +1,169 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using InteropRoi = Interop.MediaVision.RoiTracker;
+
+namespace Tizen.Multimedia.Vision
+{
+    /// <summary>
+    /// Represents a configuration of <see cref="RoiTracker"/> instances.
+    /// </summary>
+    /// <feature>http://tizen.org/feature/vision.roi_tracking</feature>
+    /// <exception cref="NotSupportedException">The required features are not supported.</exception>
+    /// <since_tizen> 10 </since_tizen>
+    public class RoiTrackingConfiguration : EngineConfiguration
+    {
+        private IntPtr _handle;
+        private const string KeyTrackerType = "MV_ROI_TRACKER_TYPE";
+
+        internal bool IsApplied { get; set; }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="RoiTrackingConfiguration"/> class.
+        /// </summary>
+        /// <exception cref="NotSupportedException">The feature is not supported.</exception>
+        /// <since_tizen> 10 </since_tizen>
+        public RoiTrackingConfiguration() : base("roi_tracking")
+        {
+            InteropRoi.Create(out _handle).Validate("Failed to create roi tracking configuration");
+        }
+
+        internal IntPtr GetRoiConfigHandle()
+        {
+            return _handle;
+        }
+
+        /// <summary>
+        /// Gets or sets the type of ROI tracker.<br/>
+        /// Default value is <see cref="RoiTrackerType.Balance"/>.
+        /// </summary>
+        /// <exception cref="ArgumentException"><paramref name="value"/> is not valid.</exception>
+        /// <since_tizen> 10 </since_tizen>
+        public RoiTrackerType TrackerType
+        {
+            get
+            {
+                return (RoiTrackerType)GetInt(KeyTrackerType);
+            }
+            set
+            {
+                ValidationUtil.ValidateEnum(typeof(RoiTrackerType), value, nameof(value));
+
+                Set(KeyTrackerType, (int)value);
+            }
+        }
+
+        internal Rectangle? _roi;
+
+        /// <summary>
+        /// Gets or sets the ROI(Region Of Interest) of <see cref="RoiTracker"/>.
+        /// </summary>
+        /// <remarks>
+        /// The default value is Rectangle(0, 0, 0, 0) but it's meaningless.<br/>
+        /// The user should set this to correct value for <see cref="RoiTracker.TrackAsync"/>.
+        /// </remarks>
+        /// <exception cref="ArgumentOutOfRangeException">
+        ///     The width of <paramref name="value"/> is less than or equal to zero.<br/>
+        ///     -or-<br/>
+        ///     The height of <paramref name="value"/> is less than or equal to zero.<br/>
+        ///     -or-<br/>
+        ///     The x position of <paramref name="value"/> is less than zero.<br/>
+        ///     -or-<br/>
+        ///     The y position of <paramref name="value"/> is less than zero.
+        /// </exception>
+        /// <since_tizen> 10 </since_tizen>
+        public Rectangle Roi
+        {
+            get
+            {
+                return _roi.HasValue ? _roi.Value : new Rectangle(0, 0, 0, 0);
+            }
+            set
+            {
+                ValidateRoi(value);
+                _roi = value;
+            }
+        }
+
+        internal static void ValidateRoi(Rectangle roi)
+        {
+            if (roi.Width <= 0)
+            {
+                throw new ArgumentOutOfRangeException("roi.Width", roi.Width,
+                    "The width of roi can't be less than or equal to zero.");
+            }
+
+            if (roi.Height <= 0)
+            {
+                throw new ArgumentOutOfRangeException("roi.Height", roi.Height,
+                    "The height of roi can't be less than or equal to zero.");
+            }
+
+            if (roi.X < 0)
+            {
+                throw new ArgumentOutOfRangeException("roi.X", roi.X,
+                    "The x position of roi can't be less than zero.");
+            }
+
+            if (roi.Y < 0)
+            {
+                throw new ArgumentOutOfRangeException("roi.Y", roi.Y,
+                    "The y position of roi can't be less than zero.");
+            }
+        }
+
+        /// <summary>
+        /// Releases the resources used by the <see cref="RoiTrackingConfiguration"/> object.
+        /// </summary>
+        /// <param name="disposing">
+        /// true to release both managed and unmanaged resources, otherwise false to release only unmanaged resources.
+        /// </param>
+        /// <since_tizen> 10 </since_tizen>
+        protected override void Dispose(bool disposing)
+        {
+            base.Dispose(disposing);
+
+            if (_handle != IntPtr.Zero)
+            {
+                InteropRoi.Destroy(_handle).Validate("Failed to destroy roi tracking configuration");
+                _handle = IntPtr.Zero;
+            }
+        }
+    }
+
+    /// <summary>
+    /// Specifies the type of ROI tracker.
+    /// </summary>
+    /// <since_tizen> 10 </since_tizen>
+    public enum RoiTrackerType
+    {
+        /// <summary>
+        /// ROI tracker focuses on accuracy
+        /// </summary>
+        Accuracy = 1,
+
+        /// <summary>
+        /// ROI tracker focuses on both accuracy and speed
+        /// </summary>
+        Balance,
+
+        /// <summary>
+        /// ROI tracker focuses on speed
+        /// </summary>
+        Speed
+    }
+}
index c626bf8..8780c7e 100755 (executable)
@@ -18,6 +18,7 @@ namespace Tizen.Multimedia
 {
     internal static class VisionFeatures
     {
+        internal const string RoiTracking = "http://tizen.org/feature/vision.roi_tracking";
         internal const string InferenceFace = "http://tizen.org/feature/vision.inference.face";
         internal const string InferenceImage = "http://tizen.org/feature/vision.inference.image";
         internal const string FaceRecognition = "http://tizen.org/feature/vision.face_recognition";