2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using InteropImage = Interop.MediaVision.Image;
21 namespace Tizen.Multimedia.Vision
24 /// Represents an image object.
26 /// <since_tizen> 3 </since_tizen>
27 public class ImageObject : IDisposable
29 private IntPtr _handle = IntPtr.Zero;
30 private bool _disposed = false;
33 /// Initializes a new instance of the <see cref="ImageObject"/> class.
35 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
36 /// <since_tizen> 3 </since_tizen>
39 InteropImage.Create(out _handle).Validate("Failed to create image object");
43 /// Initializes a new instance of the <see cref="ImageObject"/> class from the specified file.
46 /// ImageObject has been saved by <see cref="Save()"/> can be loaded.
48 /// <param name="path">Path to the image object to load.</param>
49 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
50 /// <exception cref="FileNotFoundException"><paramref name="path"/> is invalid.</exception>
51 /// <exception cref="NotSupportedException">
52 /// The feature is not supported.\n
54 /// <paramref name="path"/> is not supported file.
56 /// <exception cref="UnauthorizedAccessException">No permission to access the specified file.</exception>
57 /// <seealso cref="Save(string)"/>
58 /// <since_tizen> 3 </since_tizen>
59 public ImageObject(string path)
63 throw new ArgumentNullException(nameof(path));
65 InteropImage.Load(path, out _handle).Validate("Failed to load image object from file");
74 /// Gets a value that determines how well an image object can be recognized.
77 /// If recognition rate is too low, try to use another image or change some configuration parameters
78 /// and fill the image object again.
81 /// Recognition rate determines how well an image object can be recognized. This value can be from 0 to 1.
82 /// If the recognition rate is 0 object can not be recognized and the bigger it is the more likely to recognize the object.
84 /// <exception cref="ObjectDisposedException">The <see cref="ImageObject"/> has already been disposed of.</exception>
85 /// <seealso cref="ImageFillConfiguration"/>
86 /// <seealso cref="Fill(MediaVisionSource)"/>
87 /// <seealso cref="Fill(MediaVisionSource, ImageFillConfiguration)"/>
88 /// <seealso cref="Fill(MediaVisionSource, Rectangle)"/>
89 /// <seealso cref="Fill(MediaVisionSource, ImageFillConfiguration, Rectangle)"/>
90 /// <since_tizen> 3 </since_tizen>
91 public double RecognitionRate
95 InteropImage.GetRecognitionRate(Handle, out var rate).Validate("Failed to get recognition rate");
102 /// Gets the label for the image object.
105 /// The label value if the <see cref="ImageObject"/> has label, otherwise null.
107 /// <exception cref="ObjectDisposedException">The <see cref="ImageObject"/> has already been disposed of.</exception>
108 /// <seealso cref="SetLabel(int)"/>
109 /// <since_tizen> 3 </since_tizen>
110 public int? GetLabel()
112 var ret = InteropImage.GetLabel(Handle, out var label);
114 if (ret == MediaVisionError.NoData)
119 ret.Validate("Failed to get label");
124 /// Sets the label for the <see cref="ImageObject"/>.
126 /// <seealso cref="GetLabel"/>
127 /// <since_tizen> 3 </since_tizen>
128 public void SetLabel(int label)
130 InteropImage.SetLabel(Handle, label).Validate("Failed to set label");
134 /// Fills the image object.\n
135 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
137 /// <param name="source">The source image where image object is depicted.</param>
138 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
139 /// <exception cref="ObjectDisposedException">
140 /// The <see cref="ImageObject"/> has already been disposed of.\n
142 /// <paramref name="source"/> has already been disposed of.
144 /// <since_tizen> 3 </since_tizen>
145 public void Fill(MediaVisionSource source)
147 InvokeFill(source, null, null);
151 /// Fills the image object.\n
152 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
154 /// <param name="source">The source image where image object is depicted.</param>
155 /// <param name="config">The configuration used for extract recognition data from source. This value can be null.</param>
156 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
157 /// <exception cref="ObjectDisposedException">
158 /// The <see cref="ImageObject"/> has already been disposed of.\n
160 /// <paramref name="source"/> has already been disposed of.\n
162 /// <paramref name="config"/> has already been disposed of.
164 /// <since_tizen> 3 </since_tizen>
165 public void Fill(MediaVisionSource source, ImageFillConfiguration config)
167 InvokeFill(source, config, null);
171 /// Fills the image object.\n
172 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
174 /// <param name="source">The source image where image object is depicted.</param>
175 /// <param name="rect">Rectangular bound of the image object on the source image.</param>
176 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
177 /// <exception cref="ObjectDisposedException">
178 /// The <see cref="ImageObject"/> has already been disposed of.\n
180 /// <paramref name="source"/> has already been disposed of.\n
182 /// <since_tizen> 3 </since_tizen>
183 public void Fill(MediaVisionSource source, Rectangle rect)
185 InvokeFill(source, null, rect);
189 /// Fills the image object.\n
190 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
192 /// <param name="source">The source image where image object is depicted.</param>
193 /// <param name="config">The configuration used for extract recognition data from source. This value can be null.</param>
194 /// <param name="rect">Rectangular bound of the image object on the source image.</param>
195 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
196 /// <exception cref="ObjectDisposedException">
197 /// The <see cref="ImageObject"/> has already been disposed of.\n
199 /// <paramref name="source"/> has already been disposed of.\n
201 /// <paramref name="config"/> has already been disposed of.
203 /// <since_tizen> 3 </since_tizen>
204 public void Fill(MediaVisionSource source, ImageFillConfiguration config, Rectangle rect)
206 InvokeFill(source, config, rect);
209 private MediaVisionError InvokeFill(IntPtr source, IntPtr config, Rectangle? area)
213 return InteropImage.Fill(Handle, config, source, IntPtr.Zero);
216 var rect = area.Value.ToMarshalable();
218 return InteropImage.Fill(Handle, config, source, ref rect);
221 private void InvokeFill(MediaVisionSource source, ImageFillConfiguration config, Rectangle? area)
225 throw new ArgumentNullException(nameof(source));
228 InvokeFill(source.Handle, EngineConfiguration.GetHandle(config), area).
229 Validate("Failed to fill the image object");
233 /// Saves the image object.
235 /// <param name="path">Path to the file to save the model.</param>
236 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
237 /// <exception cref="UnauthorizedAccessException">No permission to write to the specified path.</exception>
238 /// <exception cref="ObjectDisposedException">The <see cref="FaceRecognitionModel"/> has already been disposed of.</exception>
239 /// <exception cref="DirectoryNotFoundException">The directory for <paramref name="path"/> does not exist.</exception>
240 /// <since_tizen> 3 </since_tizen>
241 public void Save(string path)
245 throw new ArgumentNullException(nameof(path));
248 var ret = InteropImage.Save(path, Handle);
250 if (ret == MediaVisionError.InvalidPath)
252 throw new DirectoryNotFoundException($"The directory for the path({path}) does not exist.");
255 ret.Validate("Failed to save the image object");
259 #region IDisposable-support
262 /// Releases all the resources used by the <see cref="ImageObject"/> object.
264 public void Dispose()
267 GC.SuppressFinalize(this);
271 /// Releases the resources used by the <see cref="ImageObject"/> object.
273 /// <param name="disposing">
274 /// true to release both managed and unmanaged resources; otherwise false to release only unmanaged resources.
276 protected virtual void Dispose(bool disposing)
283 InteropImage.Destroy(_handle);
287 internal IntPtr Handle
293 throw new ObjectDisposedException(nameof(ImageObject));