[Multimedia] Added feature tags and fixed errors in doc-comments of MediaVision.
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / MediaVision / PersonAppearanceDetector.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 detect person appearance changes 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="PersonAppearanceDetectionConfiguration"/>
28     /// <since_tizen> 3 </since_tizen>
29     public class PersonAppearanceDetector : SurveillanceEngine
30     {
31         private const string KeyAppearedNumber = "NUMBER_OF_APPEARED_PERSONS";
32         private const string KeyDisappearedNumber = "NUMBER_OF_DISAPPEARED_PERSONS";
33         private const string KeyTrackedNumber = "NUMBER_OF_TRACKED_PERSONS";
34         private const string KeyAppearedLocations = "APPEARED_PERSONS_LOCATIONS";
35         private const string KeyDisappearedLocations = "DISAPPEARED_PERSONS_LOCATIONS";
36         private const string KeyTrackedLocations = "TRACKED_PERSONS_LOCATIONS";
37
38         private const string PersonAppearanceEventType = "MV_SURVEILLANCE_EVENT_PERSON_APPEARED_DISAPEARED";
39
40         /// <summary>
41         /// Initializes a new instance of the <see cref="PersonAppearanceDetector"/> class.
42         /// </summary>
43         /// <exception cref="NotSupportedException">The required features are not supported.</exception>
44         /// <since_tizen> 3 </since_tizen>
45         public PersonAppearanceDetector() : base(PersonAppearanceEventType)
46         {
47         }
48
49         /// <summary>
50         /// Occurs when the any appearance changes detected.
51         /// </summary>
52         /// <remarks>The event handler will be executed on an internal thread.</remarks>
53         /// <since_tizen> 3 </since_tizen>
54         public event EventHandler<PersonAppearanceDetectedEventArgs> Detected;
55
56         internal override void OnEventDetected(IntPtr trigger, IntPtr source, int streamId,
57             IntPtr result, IntPtr _)
58         {
59             try
60             {
61                 Detected?.Invoke(this, CreatePersonAppearanceChangedEventArgs(result));
62             }
63             catch (Exception e)
64             {
65                 MultimediaLog.Error(MediaVisionLog.Tag, "Failed to invoke Recognized event.", e);
66             }
67         }
68
69
70         private PersonAppearanceDetectedEventArgs CreatePersonAppearanceChangedEventArgs(IntPtr result)
71         {
72             return new PersonAppearanceDetectedEventArgs(
73                 GetResultAreas(result, KeyAppearedNumber, KeyAppearedLocations),
74                 GetResultAreas(result, KeyDisappearedNumber, KeyDisappearedLocations),
75                 GetResultAreas(result, KeyTrackedNumber, KeyTrackedLocations)
76                 );
77         }
78
79         private static Rectangle[] GetResultAreas(IntPtr result, string countKey, string regionsKey)
80         {
81             int count = 0;
82             GetResultValue(result, countKey, out count).Validate("Failed to get result");
83
84             var rects = new global::Interop.MediaVision.Rectangle[count];
85             if (count > 0)
86             {
87                 GetResultValue(result, regionsKey, rects).Validate("Failed to get result");
88             }
89
90             return global::Interop.ToApiStruct(rects);
91         }
92
93         /// <summary>
94         /// Adds <see cref="SurveillanceSource"/>.
95         /// </summary>
96         /// <param name="source">The source used for recognition.</param>
97         /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
98         /// <exception cref="ObjectDisposedException">The <see cref="PersonAppearanceDetector"/> has already been disposed of.</exception>
99         /// <see cref="SurveillanceSource.Push(MediaVisionSource)"/>
100         /// <since_tizen> 3 </since_tizen>
101         public void AddSource(SurveillanceSource source)
102         {
103             AddSource(source, null);
104         }
105
106         /// <summary>
107         /// Adds <see cref="SurveillanceSource"/> with the provided <see cref="PersonAppearanceDetectionConfiguration"/>.
108         /// </summary>
109         /// <param name="source">The source used for recognition.</param>
110         /// <param name="config">The config for the <paramref name="source"/>. This value can be null.</param>
111         /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
112         /// <exception cref="ObjectDisposedException">
113         ///     The <see cref="PersonAppearanceDetector"/> has already been disposed of.<br/>
114         ///     -or-<br/>
115         ///     <paramref name="config"/> has already been disposed of.
116         /// </exception>
117         /// <see cref="SurveillanceSource.Push(MediaVisionSource)"/>
118         /// <since_tizen> 3 </since_tizen>
119         public void AddSource(SurveillanceSource source, PersonAppearanceDetectionConfiguration config)
120         {
121             InvokeAddSource(source, config);
122         }
123     }
124 }