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
24 /// Represents an image object.
26 public class ImageObject : IDisposable
28 private IntPtr _handle = IntPtr.Zero;
29 private bool _disposed = false;
32 /// Initializes a new instance of the <see cref="ImageObject"/> class.
34 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
37 InteropImage.Create(out _handle).Validate("Failed to create image object");
41 /// Initializes a new instance of the <see cref="ImageObject"/> class from the specified file.
44 /// ImageObject has been saved by <see cref="Save()"/> can be loaded.
46 /// <param name="path">Path to the image object to load.</param>
47 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
48 /// <exception cref="FileNotFoundException"><paramref name="path"/> is invalid.</exception>
49 /// <exception cref="NotSupportedException">
50 /// The feature is not supported.\n
52 /// <paramref name="path"/> is not supported file.
54 /// <exception cref="UnauthorizedAccessException">No permission to access the specified file.</exception>
55 /// <seealso cref="Save(string)"/>
56 public ImageObject(string path)
60 throw new ArgumentNullException(nameof(path));
62 InteropImage.Load(path, out _handle).Validate("Failed to load image object from file");
71 /// Gets a value that determines how well an image object can be recognized.
74 /// If recognition rate is too low, try to use another image or change some configuration parameters
75 /// and fill the image object again.
78 /// Recognition rate determines how well an image object can be recognized. This value can be from 0 to 1.
79 /// If the recognition rate is 0 object can not be recognized and the bigger it is the more likely to recognize the object.
81 /// <exception cref="ObjectDisposedException">The <see cref="ImageObject"/> has already been disposed of.</exception>
82 /// <seealso cref="ImageFillConfiguration"/>
83 /// <seealso cref="Fill(MediaVisionSource, ImageFillConfiguration, Rectangle?)"/>
84 public double RecognitionRate
89 InteropImage.GetRecognitionRate(Handle, out rate).Validate("Failed to get recognition rate");
96 /// Gets the label for the image object.
99 /// The label value; or null if the <see cref="ImageObject"/> has no label.
101 /// <exception cref="ObjectDisposedException">The <see cref="ImageObject"/> has already been disposed of.</exception>
102 /// <seealso cref="SetLabel(int)"/>
103 public int? GetLabel()
106 var ret = InteropImage.GetLabel(Handle, out label);
108 if (ret == MediaVisionError.NoData)
113 ret.Validate("Failed to get label");
118 /// Sets the label for the <see cref="ImageObject"/>.
120 /// <seealso cref="GetLabel"/>
121 public void SetLabel(int label)
123 InteropImage.SetLabel(Handle, label).Validate("Failed to set label");
127 /// Fills the image object.\n
128 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
130 /// <param name="source">The source image where image object is depicted.</param>
131 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
132 /// <exception cref="ObjectDisposedException">
133 /// The <see cref="ImageObject"/> has already been disposed of.\n
135 /// <paramref name="source"/> has already been disposed of.
137 public void Fill(MediaVisionSource source)
139 InvokeFill(source, null, null);
143 /// Fills the image object.\n
144 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
146 /// <param name="source">The source image where image object is depicted.</param>
147 /// <param name="config">The configuration used for extract recognition data from source. This value can be null.</param>
148 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
149 /// <exception cref="ObjectDisposedException">
150 /// The <see cref="ImageObject"/> has already been disposed of.\n
152 /// <paramref name="source"/> has already been disposed of.\n
154 /// <paramref name="config"/> has already been disposed of.
156 public void Fill(MediaVisionSource source, ImageFillConfiguration config)
158 InvokeFill(source, config, null);
162 /// Fills the image object.\n
163 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
165 /// <param name="source">The source image where image object is depicted.</param>
166 /// <param name="rect">Rectangular bound of the image object on the source image.</param>
167 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
168 /// <exception cref="ObjectDisposedException">
169 /// The <see cref="ImageObject"/> has already been disposed of.\n
171 /// <paramref name="source"/> has already been disposed of.\n
173 public void Fill(MediaVisionSource source, Rectangle rect)
175 InvokeFill(source, null, rect);
179 /// Fills the image object.\n
180 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
182 /// <param name="source">The source image where image object is depicted.</param>
183 /// <param name="config">The configuration used for extract recognition data from source. This value can be null.</param>
184 /// <param name="rect">Rectangular bound of the image object on the source image.</param>
185 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
186 /// <exception cref="ObjectDisposedException">
187 /// The <see cref="ImageObject"/> has already been disposed of.\n
189 /// <paramref name="source"/> has already been disposed of.\n
191 /// <paramref name="config"/> has already been disposed of.
193 public void Fill(MediaVisionSource source, ImageFillConfiguration config, Rectangle rect)
195 InvokeFill(source, config, rect);
198 private MediaVisionError InvokeFill(IntPtr source, IntPtr config, Rectangle? area)
202 return InteropImage.Fill(Handle, config, source, IntPtr.Zero);
205 var rect = area.Value.ToMarshalable();
207 return InteropImage.Fill(Handle, config, source, ref rect);
210 private void InvokeFill(MediaVisionSource source, ImageFillConfiguration config, Rectangle? area)
214 throw new ArgumentNullException(nameof(source));
217 InvokeFill(source.Handle, EngineConfiguration.GetHandle(config), area).
218 Validate("Failed to fill the image object");
222 /// Saves the image object.
224 /// <param name="path">Path to the file to save the model.</param>
225 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
226 /// <exception cref="UnauthorizedAccessException">No permission to write to the specified path.</exception>
227 /// <exception cref="ObjectDisposedException">The <see cref="FaceRecognitionModel"/> has already been disposed of.</exception>
228 /// <exception cref="DirectoryNotFoundException">The directory for <paramref name="path"/> does not exist.</exception>
229 public void Save(string path)
233 throw new ArgumentNullException(nameof(path));
236 var ret = InteropImage.Save(path, Handle);
238 if (ret == MediaVisionError.InvalidPath)
240 throw new DirectoryNotFoundException($"The directory for the path({path}) does not exist.");
243 ret.Validate("Failed to save the image object");
247 #region IDisposable-support
248 public void Dispose()
251 GC.SuppressFinalize(this);
254 protected virtual void Dispose(bool disposing)
261 InteropImage.Destroy(_handle);
265 internal IntPtr Handle
271 throw new ObjectDisposedException(nameof(ImageObject));