Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / MediaVision / FaceDetectionConfiguration.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
19 namespace Tizen.Multimedia
20 {
21     /// <summary>
22     /// Represents a configuration of <see cref="FaceDetector"/> instances.
23     /// </summary>
24     /// <since_tizen> 3</since_tizen>
25     public class FaceDetectionConfiguration : EngineConfiguration
26     {
27         private const string KeyModelFilePath = "MV_FACE_DETECTION_MODEL_FILE_PATH";
28         private const string KeyRoiX = "MV_FACE_DETECTION_ROI_X";
29         private const string KeyRoiY = "MV_FACE_DETECTION_ROI_Y";
30         private const string KeyRoiWidth = "MV_FACE_DETECTION_ROI_WIDTH";
31         private const string KeyRoiHeight = "MV_FACE_DETECTION_ROI_HEIGHT";
32         private const string KeyMinWidth = "MV_FACE_DETECTION_MIN_SIZE_WIDTH";
33         private const string KeyMinHeight = "MV_FACE_DETECTION_MIN_SIZE_HEIGHT";
34
35         /// <summary>
36         /// Initializes a new instance of the <see cref="FaceDetectionConfiguration"/> class.
37         /// </summary>
38         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
39         /// <since_tizen> 3</since_tizen>
40         public FaceDetectionConfiguration() : base("face_recognition")
41         {
42         }
43
44         /// <summary>
45         /// Gets or sets the face detection haarcascade xml file for face detection.
46         /// </summary>
47         /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
48         /// <since_tizen> 3</since_tizen>
49         public string ModelFilePath
50         {
51             get
52             {
53                 return GetString(KeyModelFilePath);
54             }
55             set
56             {
57                 if (value == null)
58                 {
59                     throw new ArgumentNullException(nameof(ModelFilePath), "ModeFilePath can't be null.");
60                 }
61                 Set(KeyModelFilePath, value);
62             }
63         }
64
65
66         /// <summary>
67         /// Gets or sets minimum height of face which will be detected.
68         /// </summary>
69         /// <remarks>
70         /// Default value is null (all detected faces will be applied), can be changed to specify the minimum face height.
71         /// </remarks>
72         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
73         /// <since_tizen> 3</since_tizen>
74         public int? MinHeight
75         {
76             get
77             {
78                 int value = GetInt(KeyMinHeight);
79                 if (value == -1) return null;
80                 return value;
81             }
82             set
83             {
84                 if (value.HasValue && value < 0)
85                 {
86                     throw new ArgumentOutOfRangeException(nameof(MinHeight), value,
87                         $"{nameof(MinHeight)} can't be less than zero.");
88                 }
89
90                 Set(KeyMinHeight, value ?? -1);
91             }
92         }
93
94         /// <summary>
95         /// Gets or sets minimum width of face which will be detected.
96         /// </summary>
97         /// <remarks>
98         /// Default value is null (all detected faces will be applied), can be changed to specify the minimum face width.
99         /// </remarks>
100         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
101         /// <since_tizen> 3</since_tizen>
102         public int? MinWidth
103         {
104             get
105             {
106                 int value = GetInt(KeyMinWidth);
107                 if (value == -1) return null;
108                 return value;
109             }
110             set
111             {
112                 if (value.HasValue && value < 0)
113                 {
114                     throw new ArgumentOutOfRangeException(nameof(MinWidth), value,
115                         $"{nameof(MinWidth)} can't be less than zero.");
116                 }
117
118                 Set(KeyMinWidth, value ?? -1);
119             }
120         }
121
122         private static readonly Rectangle DefaultRoi = new Rectangle(-1, -1, -1, -1);
123
124         private Rectangle? _roi;
125
126         /// <summary>
127         /// Gets or sets the roi of the face detection.
128         /// </summary>
129         /// <remarks>
130         /// Default value is null (the roi will be a full image) can be changed to specify the roi for face detection.
131         /// </remarks>
132         /// <exception cref="ArgumentOutOfRangeException">
133         ///     The width of <paramref name="value"/> is less than or equal to zero.\n
134         ///     -or-\n
135         ///     The height of <paramref name="value"/> is less than or equal to zero.\n
136         ///     -or-\n
137         ///     The x position of <paramref name="value"/> is less than zero.\n
138         ///     -or-\n
139         ///     The y position of <paramref name="value"/> is less than zero.\n
140         /// </exception>
141         /// <since_tizen> 3</since_tizen>
142         public Rectangle? Roi
143         {
144             get
145             {
146                 return _roi;
147             }
148             set
149             {
150                 if (value != null)
151                 {
152                     ValidateRoi(value.Value);
153                 }
154
155                 SetRoi(value ?? DefaultRoi);
156
157                 _roi = value;
158             }
159         }
160
161         private static void ValidateRoi(Rectangle roi)
162         {
163             if (roi.Width <= 0)
164             {
165                 throw new ArgumentOutOfRangeException("Roi.Width", roi.Width,
166                     "The width of roi can't be less than or equal to zero.");
167             }
168
169             if (roi.Height <= 0)
170             {
171                 throw new ArgumentOutOfRangeException("Roi.Height", roi.Height,
172                     "The height of roi can't be less than or equal to zero.");
173             }
174
175             if (roi.X < 0)
176             {
177                 throw new ArgumentOutOfRangeException("Roi.X", roi.X,
178                     "The x position of roi can't be less than zero.");
179             }
180
181             if (roi.Y < 0)
182             {
183                 throw new ArgumentOutOfRangeException("Roi.Y", roi.Y,
184                     "The y position of roi can't be less than zero.");
185             }
186         }
187
188         private void SetRoi(Rectangle roi)
189         {
190             Set(KeyRoiX, roi.X);
191             Set(KeyRoiY, roi.Y);
192             Set(KeyRoiWidth, roi.Width);
193             Set(KeyRoiHeight, roi.Height);
194         }
195     }
196 }