[MediaVision] Add new APIs for deep learning face recognition (#4510)
authorHaesu Gwon <haesu.gwon@samsung.com>
Wed, 7 Sep 2022 07:11:59 +0000 (16:11 +0900)
committerGitHub <noreply@github.com>
Wed, 7 Sep 2022 07:11:59 +0000 (16:11 +0900)
* [MediaVision] Add new APIs for inference face recognition

15 files changed:
src/Tizen.Multimedia.Vision/Interop/Interop.Libraries.cs
src/Tizen.Multimedia.Vision/Interop/Interop.MediaVision.Face.cs
src/Tizen.Multimedia.Vision/MediaVision/DeepLearningFaceRecognitionResult.cs [new file with mode: 0755]
src/Tizen.Multimedia.Vision/MediaVision/DeepLearningFaceRecognizer.cs [new file with mode: 0755]
src/Tizen.Multimedia.Vision/MediaVision/FaceDetectionResult.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/FaceDetector.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/FacialLandmarkDetector.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/ImageClassificationResult.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/ImageClassifier.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/InferenceModelConfiguration.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/InferenceType.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/ObjectDetectionResult.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/ObjectDetector.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/PoseLandmarkDetector.cs [changed mode: 0644->0755]
src/Tizen.Multimedia.Vision/MediaVision/VisionFeatures.cs [changed mode: 0644->0755]

index 5e94dcc..3a22309 100644 (file)
@@ -25,5 +25,7 @@ internal static partial class Interop
         public const string MediaVisionSurveillance = "libmv_surveillance.so";
         public const string MediaVisionBarcodeDetector = "libmv_barcode_detector.so";
         public const string MediaVisionBarcodeGenerator = "libmv_barcode_generator.so";
+        public const string MediaVisionRoiTracker = "libmv_roi_tracker.so";
+        public const string MediaVisionFaceRecognition = "libmv_face_recognition.so"; // It's based on machine learning
     }
 }
index 7c3374a..dd013a1 100755 (executable)
@@ -149,5 +149,32 @@ internal static partial class Interop
             [DllImport(Libraries.MediaVisionFace, EntryPoint = "mv_face_tracking_model_load")]
             internal static extern MediaVisionError Load(string fileName, out IntPtr handle);
         }
+
+        /// <summary>
+        /// Interop for FaceRecognition APIs.
+        /// </summary>
+        internal static partial class FaceRecognition
+        {
+            [DllImport(Libraries.MediaVisionFaceRecognition, EntryPoint = "mv_face_recognition_create")]
+            internal static extern MediaVisionError Create(out IntPtr handle);
+
+            [DllImport(Libraries.MediaVisionFaceRecognition, EntryPoint = "mv_face_recognition_destroy")]
+            internal static extern MediaVisionError Destroy(IntPtr handle);
+
+            [DllImport(Libraries.MediaVisionFaceRecognition, EntryPoint = "mv_face_recognition_prepare")]
+            internal static extern MediaVisionError Prepare(IntPtr handle);
+
+            [DllImport(Libraries.MediaVisionFaceRecognition, EntryPoint = "mv_face_recognition_register")]
+            internal static extern MediaVisionError Register(IntPtr handle, IntPtr source, string label);
+
+            [DllImport(Libraries.MediaVisionFaceRecognition, EntryPoint = "mv_face_recognition_unregister")]
+            internal static extern MediaVisionError Unregister(IntPtr handle, string label);
+
+            [DllImport(Libraries.MediaVisionFaceRecognition, EntryPoint = "mv_face_recognition_inference")]
+            internal static extern MediaVisionError Inference(IntPtr handle, IntPtr source);
+
+            [DllImport(Libraries.MediaVisionFaceRecognition, EntryPoint = "mv_face_recognition_get_label")]
+            internal static extern MediaVisionError GetLabel(IntPtr handle, out string label);
+        }
     }
 }
diff --git a/src/Tizen.Multimedia.Vision/MediaVision/DeepLearningFaceRecognitionResult.cs b/src/Tizen.Multimedia.Vision/MediaVision/DeepLearningFaceRecognitionResult.cs
new file mode 100755 (executable)
index 0000000..cd7e5ba
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+
+namespace Tizen.Multimedia.Vision
+{
+    /// <summary>
+    /// Represents the result of <see cref="DeepLearningFaceRecognizer"/> operations.
+    /// </summary>
+    /// <since_tizen> 10 </since_tizen>
+    public class DeepLearningFaceRecognitionResult
+    {
+        internal DeepLearningFaceRecognitionResult(string label)
+        {
+            Label = label;
+        }
+
+        /// <summary>
+        /// Gets the label of the recognized face.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public string Label { get; }
+
+        /// <summary>
+        /// Returns a string that represents the current object.
+        /// </summary>
+        /// <returns>A string that represents the current object.</returns>
+        /// <since_tizen> 10 </since_tizen>
+        public override string ToString() => $"Label={Label}";
+    }
+}
diff --git a/src/Tizen.Multimedia.Vision/MediaVision/DeepLearningFaceRecognizer.cs b/src/Tizen.Multimedia.Vision/MediaVision/DeepLearningFaceRecognizer.cs
new file mode 100755 (executable)
index 0000000..82b5b1a
--- /dev/null
@@ -0,0 +1,177 @@
+/*
+ * 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 InteropFace = Interop.MediaVision.FaceRecognition;
+
+namespace Tizen.Multimedia.Vision
+{
+    /// <summary>
+    /// Provides the ability to recognize face based on previously registered face data.
+    /// </summary>
+    /// <since_tizen> 10 </since_tizen>
+    public class DeepLearningFaceRecognizer : IDisposable
+    {
+        private IntPtr _handle;
+        private bool _disposed;
+
+        /// <summary>Initializes a new instance of the <see cref="DeepLearningFaceRecognizer"/> class.</summary>
+        /// <remarks>
+        /// This class is different from <see cref="FaceRecognizer"/> in aspect of using internal machine learning algorithm.
+        /// </remarks>
+        /// <exception cref="NotSupportedException">The required features are not supported.</exception>
+        /// <feature>http://tizen.org/feature/vision.face_recognition</feature>
+        /// <since_tizen> 10 </since_tizen>
+        public DeepLearningFaceRecognizer()
+        {
+            ValidationUtil.ValidateFeatureSupported(VisionFeatures.FaceRecognition);
+
+            InteropFace.Create(out _handle).Validate("Failed to create face recognizer");
+
+            try
+            {
+                InteropFace.Prepare(_handle).Validate("Failed to prepare face recognizer");
+            }
+            catch (Exception e)
+            {
+                Log.Error(MediaVisionLog.Tag, e.ToString());
+                InteropFace.Destroy(_handle);
+                throw e;
+            }
+        }
+
+        /// <summary>
+        /// Finalizes an instance of the DeepLearningFaceRecognizer class.
+        /// </summary>
+        ~DeepLearningFaceRecognizer()
+        {
+            Dispose(false);
+        }
+
+        /// <summary>
+        /// Registers face data to internal database.
+        /// </summary>
+        /// <param name="source">The face data to register.</param>
+        /// <param name="label">The name of face data.</param>
+        /// <exception cref="ObjectDisposedException">The DeepLearningFaceRecognizer already has been disposed.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="label"/> is null.</exception>
+        /// <exception cref="InvalidOperationException">In case of any invalid operations.</exception>
+        /// <since_tizen> 10 </since_tizen>
+        public void RegisterFace(MediaVisionSource source, string label)
+        {
+            ValidateNotDisposed();
+
+            if (source == null)
+            {
+                throw new ArgumentNullException(nameof(source));
+            }
+            if (label == null)
+            {
+                throw new ArgumentNullException(nameof(label));
+            }
+
+            InteropFace.Register(_handle, source.Handle, label).Validate("Failed to register face data");
+        }
+
+        /// <summary>
+        /// Unregisters face data from internal database.
+        /// </summary>
+        /// <param name="label">The name of face data.</param>
+        /// <exception cref="ObjectDisposedException">The DeepLearningFaceRecognizer already has been disposed.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="label"/> is null.</exception>
+        /// <exception cref="InvalidOperationException">In case of any invalid operations.</exception>
+        /// <since_tizen> 10 </since_tizen>
+        public void UnregisterFace(string label)
+        {
+            ValidateNotDisposed();
+
+            if (label == null)
+            {
+                throw new ArgumentNullException(nameof(label));
+            }
+
+            InteropFace.Unregister(_handle, label).Validate("Failed to register face data");
+        }
+
+        /// <summary>
+        /// Recognizes a face in by finding the closest match among the registered faces and returns the label of the found face.
+        /// </summary>
+        /// <param name="source">The face data to recognize.</param>
+        /// <returns>A label of recognized face.</returns>
+        /// <exception cref="ObjectDisposedException">The DeepLearningFaceRecognizer already has been disposed.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
+        /// <exception cref="InvalidOperationException">In case of any invalid operations.</exception>
+        /// <since_tizen> 10 </since_tizen>
+        public DeepLearningFaceRecognitionResult Recognize(MediaVisionSource source)
+        {
+            ValidateNotDisposed();
+
+            if (source == null)
+            {
+                throw new ArgumentNullException(nameof(source));
+            }
+
+            InteropFace.Inference(_handle, source.Handle).Validate("Failed to recognize face");
+
+            InteropFace.GetLabel(_handle, out string label).Validate("Failed to get label");
+
+            return new DeepLearningFaceRecognitionResult(label);
+        }
+
+        /// <summary>
+        /// Releases the unmanaged resources used by the DeepLearningFaceRecognizer.
+        /// </summary>
+        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
+        /// <since_tizen> 10 </since_tizen>
+        protected virtual void Dispose(bool disposing)
+        {
+            if (!_disposed)
+            {
+                if (disposing)
+                {
+                    // to be used if there are any other disposable objects
+                }
+
+                if (_handle != IntPtr.Zero)
+                {
+                    InteropFace.Destroy(_handle);
+                    _handle = IntPtr.Zero;
+                }
+
+                _disposed = true;
+            }
+        }
+
+        /// <summary>
+        /// Releases all resources used by the DeepLearningFaceRecognizer.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        internal void ValidateNotDisposed()
+        {
+            if (_disposed)
+            {
+                Log.Error(MediaVisionLog.Tag, "DeepLearningFaceRecognizer handle is disposed.");
+                throw new ObjectDisposedException(nameof(DeepLearningFaceRecognizer));
+            }
+        }
+    }
+}
\ No newline at end of file
old mode 100644 (file)
new mode 100755 (executable)
index 31e25cd..c626bf8
@@ -20,5 +20,6 @@ namespace Tizen.Multimedia
     {
         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";
     }
 }