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