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 /// <param name="area">Location of the image object on the source image, or NULL if the object is shown in full</param>
149 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
150 /// <exception cref="ObjectDisposedException">
151 /// The <see cref="ImageObject"/> has already been disposed of.\n
153 /// <paramref name="source"/> has already been disposed of.\n
155 /// <paramref name="config"/> has already been disposed of.
157 public void Fill(MediaVisionSource source, ImageFillConfiguration config)
159 InvokeFill(source, config, null);
163 /// Fills the image object.\n
164 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
166 /// <param name="source">The source image where image object is depicted.</param>
167 /// <param name="rect">Rectangular bound of the image object on the source image.</param>
168 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
169 /// <exception cref="ObjectDisposedException">
170 /// The <see cref="ImageObject"/> has already been disposed of.\n
172 /// <paramref name="source"/> has already been disposed of.\n
174 public void Fill(MediaVisionSource source, Rectangle rect)
176 InvokeFill(source, null, rect);
180 /// Fills the image object.\n
181 /// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
183 /// <param name="source">The source image where image object is depicted.</param>
184 /// <param name="config">The configuration used for extract recognition data from source. This value can be null.</param>
185 /// <param name="rect">Rectangular bound of the image object on the source image.</param>
186 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
187 /// <exception cref="ObjectDisposedException">
188 /// The <see cref="ImageObject"/> has already been disposed of.\n
190 /// <paramref name="source"/> has already been disposed of.\n
192 /// <paramref name="config"/> has already been disposed of.
194 public void Fill(MediaVisionSource source, ImageFillConfiguration config, Rectangle rect)
196 InvokeFill(source, config, rect);
199 private MediaVisionError InvokeFill(IntPtr source, IntPtr config, Rectangle? area)
203 return InteropImage.Fill(Handle, config, source, IntPtr.Zero);
206 var rect = area.Value.ToMarshalable();
208 return InteropImage.Fill(Handle, config, source, ref rect);
211 private void InvokeFill(MediaVisionSource source, ImageFillConfiguration config, Rectangle? area)
215 throw new ArgumentNullException(nameof(source));
218 InvokeFill(source.Handle, EngineConfiguration.GetHandle(config), area).
219 Validate("Failed to fill the image object");
223 /// Saves the image object.
225 /// <param name="path">Path to the file to save the model.</param>
226 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
227 /// <exception cref="UnauthorizedAccessException">No permission to write to the specified path.</exception>
228 /// <exception cref="ObjectDisposedException">The <see cref="FaceRecognitionModel"/> has already been disposed of.</exception>
229 /// <exception cref="DirectoryNotFoundException">The directory for <paramref name="path"/> does not exist.</exception>
230 public void Save(string path)
234 throw new ArgumentNullException(nameof(path));
237 var ret = InteropImage.Save(path, Handle);
239 if (ret == MediaVisionError.InvalidPath)
241 throw new DirectoryNotFoundException($"The directory for the path({path}) does not exist.");
244 ret.Validate("Failed to save the image object");
248 #region IDisposable-support
249 public void Dispose()
252 GC.SuppressFinalize(this);
255 protected virtual void Dispose(bool disposing)
262 InteropImage.Destroy(_handle);
266 internal IntPtr Handle
272 throw new ObjectDisposedException(nameof(ImageObject));