Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / 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     /// <since_tizen> 3</since_tizen>
27     public class PersonRecognizer : SurveillanceEngine
28     {
29         private const string KeyCount = "NUMBER_OF_PERSONS";
30         private const string KeyLocations = "PERSONS_LOCATIONS";
31         private const string KeyLabels = "PERSONS_LABELS";
32         private const string KeyConfidences = "PERSONS_CONFIDENCES";
33
34         private const string PersonRecognizedEventType = "MV_SURVEILLANCE_EVENT_PERSON_RECOGNIZED";
35
36         /// <summary>
37         /// Initializes a new instance of the <see cref="PersonRecognizer"/> class.
38         /// </summary>
39         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
40         /// <since_tizen> 3</since_tizen>
41         public PersonRecognizer() : base(PersonRecognizedEventType)
42         {
43         }
44
45         /// <summary>
46         /// Occurs when a person recognized.
47         /// </summary>
48         /// <remarks>The event handler will be executed on an internal thread.</remarks>
49         /// <seealso cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>
50         /// <since_tizen> 3</since_tizen>
51         public event EventHandler<PersonRecognizedEventArgs> Recognized;
52
53         internal override void OnEventDetected(IntPtr trigger, IntPtr source, int streamId,
54             IntPtr result, IntPtr _)
55         {
56             try
57             {
58                 Recognized?.Invoke(this, CreatePersonRecognizedEventArgs(result));
59             }
60             catch (Exception e)
61             {
62                 MultimediaLog.Error(MediaVisionLog.Tag, "Failed to invoke Recognized event.", e);
63             }
64         }
65
66         private PersonRecognizedEventArgs CreatePersonRecognizedEventArgs(IntPtr result)
67         {
68             int count;
69
70             GetResultValue(result, KeyCount, out count).Validate("Failed to get result count");
71
72             var recognitionInfo = new PersonRecognitionInfo[count];
73
74             if (count > 0)
75             {
76                 var rects = new global::Interop.MediaVision.Rectangle[count];
77                 GetResultValue(result, KeyLocations, rects).Validate("Failed to get location");
78
79                 var labels = new int[count];
80                 GetResultValue(result, KeyLabels, labels).Validate("Failed to get label");
81
82                 var confidences = new double[count];
83                 GetResultValue(result, KeyConfidences, confidences).Validate("Failed to get confidence");
84
85                 for (int i = 0; i < count; i++)
86                 {
87                     recognitionInfo[i] = new PersonRecognitionInfo(rects[i].ToApiStruct(),
88                         labels[i], confidences[i]);
89                 }
90             }
91
92             return new PersonRecognizedEventArgs(recognitionInfo);
93         }
94
95         /// <summary>
96         /// Adds <see cref="SurveillanceSource"/> with the provided <see cref="PersonRecognitionConfiguration"/>.
97         /// </summary>
98         /// <param name="source">The source used for recognition.</param>
99         /// <param name="config">The config for the <paramref name="source"/>.</param>
100         /// <exception cref="ArgumentNullException">
101         ///     <paramref name="source"/> is null.\n
102         ///     -or-\n
103         ///     <paramref name="config"/> is null.
104         /// </exception>
105         /// <exception cref="ObjectDisposedException">
106         ///     The <see cref="PersonRecognizer"/> has already been disposed of.\n
107         ///     -or-\n
108         ///     <paramref name="config"/> has already been disposed of.
109         /// </exception>
110         /// <exception cref="ArgumentException">
111         /// <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/> of <paramref name="config"/> does not exists.
112         /// </exception>
113         /// <exception cref="UnauthorizedAccessException">
114         /// No permission to access to the <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>.
115         /// </exception>
116         /// <exception cref="NotSupportedException">The model file is not supported format or file.</exception>
117         /// <see cref="SurveillanceSource.Push(MediaVisionSource)"/>
118         /// <since_tizen> 3</since_tizen>
119         public void AddSource(SurveillanceSource source, PersonRecognitionConfiguration config)
120         {
121             if (config == null)
122             {
123                 throw new ArgumentNullException(nameof(config));
124             }
125             InvokeAddSource(source, config);
126         }
127     }
128 }