[MediaVision] Refactoring
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaVision / PersonRecognizer.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using static Interop.MediaVision.Surveillance;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Provides the ability to recognize person on image sources.
24     /// </summary>
25     /// <seealso cref="PersonRecognitionConfiguration"/>
26     public class PersonRecognizer : SurveillanceEngine
27     {
28         private const string KeyCount = "NUMBER_OF_PERSONS";
29         private const string KeyLocations = "PERSONS_LOCATIONS";
30         private const string KeyLabels = "PERSONS_LABELS";
31         private const string KeyConfidences = "PERSONS_CONFIDENCES";
32
33         private const string PersonRecognizedEventType = "MV_SURVEILLANCE_EVENT_PERSON_RECOGNIZED";
34
35         /// <summary>
36         /// Initializes a new instance of the <see cref="PersonRecognizer"/> class.
37         /// </summary>
38         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
39         public PersonRecognizer() : base(PersonRecognizedEventType)
40         {
41         }
42
43         /// <summary>
44         /// Occurs when a person recognized.
45         /// </summary>
46         /// <remarks>The event handler will be executed on an internal thread.</remarks>
47         /// <seealso cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>
48         public event EventHandler<PersonRecognizedEventArgs> Recognized;
49
50         internal override void OnEventDetected(IntPtr trigger, IntPtr source, int streamId,
51             IntPtr result, IntPtr _)
52         {
53             try
54             {
55                 Recognized?.Invoke(this, CreatePersonRecognizedEventArgs(result));
56             }
57             catch (Exception e)
58             {
59                 MultimediaLog.Error(MediaVisionLog.Tag, "Failed to invoke Recognized event.", e);
60             }
61         }
62
63         private PersonRecognizedEventArgs CreatePersonRecognizedEventArgs(IntPtr result)
64         {
65             int count;
66
67             GetResultValue(result, KeyCount, out count).Validate("Failed to get result count");
68
69             var recognitionInfo = new PersonRecognitionInfo[count];
70
71             if (count > 0)
72             {
73                 var rects = new Interop.MediaVision.Rectangle[count];
74                 GetResultValue(result, KeyLocations, rects).Validate("Failed to get location");
75
76                 var labels = new int[count];
77                 GetResultValue(result, KeyLabels, labels).Validate("Failed to get label");
78
79                 var confidences = new double[count];
80                 GetResultValue(result, KeyConfidences, confidences).Validate("Failed to get confidence");
81
82                 for (int i = 0; i < count; i++)
83                 {
84                     recognitionInfo[i] = new PersonRecognitionInfo(rects[i].ToApiStruct(),
85                         labels[i], confidences[i]);
86                 }
87             }
88
89             return new PersonRecognizedEventArgs(recognitionInfo);
90         }
91
92         /// <summary>
93         /// Adds <see cref="SurveillanceSource"/> with the provided <see cref="PersonRecognitionConfiguration"/>.
94         /// </summary>
95         /// <param name="source">The source used for recognition.</param>
96         /// <param name="config">The config for the <paramref name="source"/>.</param>
97         /// <exception cref="ArgumentNullException">
98         ///     <paramref name="source"/> is null.\n
99         ///     - or -\n
100         ///     <paramref name="config"/> is null.
101         /// </exception>
102         /// <exception cref="ObjectDisposedException">
103         ///     The <see cref="PersonRecognizer"/> has already been disposed of.\n
104         ///     - or -\n
105         ///     <paramref name="config"/> has already been disposed of.
106         /// </exception>
107         /// <exception cref="System.IO.FileNotFoundException">
108         /// <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/> of <paramref name="config"/> does not exists.
109         /// </exception>
110         /// <exception cref="UnauthorizedAccessException">
111         /// No permission to access to the <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>.
112         /// </exception>
113         /// <exception cref="NotSupportedException">The model file is not supported format or file.</exception>
114         /// <see cref="SurveillanceSource.Push(MediaVisionSource)"/>
115         public void AddSource(SurveillanceSource source, PersonRecognitionConfiguration config)
116         {
117             if (config == null)
118             {
119                 throw new ArgumentNullException(nameof(config));
120             }
121             InvokeAddSource(source, config);
122         }
123     }
124 }