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