/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using InteropImage = Interop.MediaVision.Image;
namespace Tizen.Multimedia.Vision
{
///
/// Represents an image object.
///
/// 3
public class ImageObject : IDisposable
{
private IntPtr _handle = IntPtr.Zero;
private bool _disposed = false;
///
/// Initializes a new instance of the class.
///
/// The feature is not supported.
/// 3
public ImageObject()
{
InteropImage.Create(out _handle).Validate("Failed to create image object");
}
///
/// Initializes a new instance of the class from the specified file.
///
///
/// ImageObject has been saved by can be loaded.
///
/// Path to the image object to load.
/// is null.
/// is invalid.
///
/// The feature is not supported.\n
/// -or-\n
/// is not supported file.
///
/// No permission to access the specified file.
///
/// 3
public ImageObject(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
InteropImage.Load(path, out _handle).Validate("Failed to load image object from file");
}
~ImageObject()
{
Dispose(false);
}
///
/// Gets a value that determines how well an image object can be recognized.
///
///
/// If recognition rate is too low, try to use another image or change some configuration parameters
/// and fill the image object again.
///
///
/// Recognition rate determines how well an image object can be recognized. This value can be from 0 to 1.
/// If the recognition rate is 0 object can not be recognized and the bigger it is the more likely to recognize the object.
///
/// The has already been disposed of.
///
///
///
///
///
/// 3
public double RecognitionRate
{
get
{
InteropImage.GetRecognitionRate(Handle, out var rate).Validate("Failed to get recognition rate");
return rate;
}
}
#region Methods
///
/// Gets the label for the image object.
///
///
/// The label value if the has label, otherwise null.
///
/// The has already been disposed of.
///
/// 3
public int? GetLabel()
{
var ret = InteropImage.GetLabel(Handle, out var label);
if (ret == MediaVisionError.NoData)
{
return null;
}
ret.Validate("Failed to get label");
return label;
}
///
/// Sets the label for the .
///
///
/// 3
public void SetLabel(int label)
{
InteropImage.SetLabel(Handle, label).Validate("Failed to set label");
}
///
/// Fills the image object.\n
/// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
///
/// The source image where image object is depicted.
/// is null.
///
/// The has already been disposed of.\n
/// -or-\n
/// has already been disposed of.
///
/// 3
public void Fill(MediaVisionSource source)
{
InvokeFill(source, null, null);
}
///
/// Fills the image object.\n
/// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
///
/// The source image where image object is depicted.
/// The configuration used for extract recognition data from source. This value can be null.
/// is null.
///
/// The has already been disposed of.\n
/// -or-\n
/// has already been disposed of.\n
/// -or-\n
/// has already been disposed of.
///
/// 3
public void Fill(MediaVisionSource source, ImageFillConfiguration config)
{
InvokeFill(source, config, null);
}
///
/// Fills the image object.\n
/// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
///
/// The source image where image object is depicted.
/// Rectangular bound of the image object on the source image.
/// is null.
///
/// The has already been disposed of.\n
/// -or-\n
/// has already been disposed of.\n
///
/// 3
public void Fill(MediaVisionSource source, Rectangle rect)
{
InvokeFill(source, null, rect);
}
///
/// Fills the image object.\n
/// Extracts data from @a source image which will be needed for recognition of depicted object in @a location.
///
/// The source image where image object is depicted.
/// The configuration used for extract recognition data from source. This value can be null.
/// Rectangular bound of the image object on the source image.
/// is null.
///
/// The has already been disposed of.\n
/// -or-\n
/// has already been disposed of.\n
/// -or-\n
/// has already been disposed of.
///
/// 3
public void Fill(MediaVisionSource source, ImageFillConfiguration config, Rectangle rect)
{
InvokeFill(source, config, rect);
}
private MediaVisionError InvokeFill(IntPtr source, IntPtr config, Rectangle? area)
{
if (area == null)
{
return InteropImage.Fill(Handle, config, source, IntPtr.Zero);
}
var rect = area.Value.ToMarshalable();
return InteropImage.Fill(Handle, config, source, ref rect);
}
private void InvokeFill(MediaVisionSource source, ImageFillConfiguration config, Rectangle? area)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
InvokeFill(source.Handle, EngineConfiguration.GetHandle(config), area).
Validate("Failed to fill the image object");
}
///
/// Saves the image object.
///
/// Path to the file to save the model.
/// is null.
/// No permission to write to the specified path.
/// The has already been disposed of.
/// The directory for does not exist.
/// 3
public void Save(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
var ret = InteropImage.Save(path, Handle);
if (ret == MediaVisionError.InvalidPath)
{
throw new DirectoryNotFoundException($"The directory for the path({path}) does not exist.");
}
ret.Validate("Failed to save the image object");
}
#endregion
#region IDisposable-support
///
/// Releases all the resources used by the object.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases the resources used by the object.
///
///
/// true to release both managed and unmanaged resources; otherwise false to release only unmanaged resources.
///
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
InteropImage.Destroy(_handle);
_disposed = true;
}
internal IntPtr Handle
{
get
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(ImageObject));
}
return _handle;
}
}
#endregion
}
}