private void GetSupportedBackend() { var supportedBackend = new List <InferenceBackendType>(); InteropInference.SupportedBackendCallback cb = (backend, isSupported, _) => { if (isSupported && backend != null) { switch (backend) { case _backendTypeOpenCV: supportedBackend.Add(InferenceBackendType.OpenCV); break; case _backendTypeTFLite: supportedBackend.Add(InferenceBackendType.TFLite); break; } } return(true); }; InteropInference.ForeachSupportedBackend(_inferenceHandle, cb, IntPtr.Zero). Validate("Failed to get supported backend"); _supportedBackend = supportedBackend; }
/// <summary> /// Detects objects and gets its locations on the source image using inference engine set in <paramref name="config"/>.<br/> /// Each time when DetectAsync is called, a set of the detected objects at the media source are received asynchronously. /// </summary> /// <feature>http://tizen.org/feature/vision.inference</feature> /// <feature>http://tizen.org/feature/vision.inference.image</feature> /// <param name="source">The source of the media where faces will be detected.</param> /// <param name="config">The engine's configuration that will be used for detecting.</param> /// <returns> /// A task that represents the asynchronous detect operation.<br/> /// If there's no detected object, empty collection will be returned. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="config"/> is null.</exception> /// <exception cref="InvalidOperationException">Internal error.</exception> /// <exception cref="NotSupportedException">The feature is not supported.</exception> /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception> /// <seealso cref="InferenceModelConfiguration"/> /// <since_tizen> 6 </since_tizen> public static async Task <IEnumerable <ObjectDetectionResult> > DetectAsync(MediaVisionSource source, InferenceModelConfiguration config) { // `vision.inference` feature is already checked, when config is created. ValidationUtil.ValidateFeatureSupported(VisionFeatures.InferenceImage); if (source == null) { throw new ArgumentNullException(nameof(source)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } var tcs = new TaskCompletionSource <IEnumerable <ObjectDetectionResult> >(); using (var cb = ObjectKeeper.Get(GetCallback(tcs))) { InteropInference.DetectObject(source.Handle, config.GetHandle(), cb.Target). Validate("Failed to detect object."); return(await tcs.Task); } }
/// <summary> /// Releases the resources used by the <see cref="InferenceModelConfiguration"/> object. /// </summary> /// <param name="disposing"> /// true to release both managed and unmanaged resources, otherwise false to release only unmanaged resources. /// </param> /// <since_tizen> 6 </since_tizen> protected override void Dispose(bool disposing) { base.Dispose(disposing); if (_inferenceHandle != IntPtr.Zero) { InteropInference.Destroy(_inferenceHandle).Validate("Failed to destroy inference configuration"); _inferenceHandle = IntPtr.Zero; } }
/// <summary> /// Loads inference model data and its related attributes. /// </summary> /// <remarks> /// Before calling this method, user should set all properties which is required by each inference model.<br/> /// The properties set after calling this method will not be affected in the result. /// </remarks> /// <privilege>http://tizen.org/privilege/mediastorage</privilege> /// <privilege>http://tizen.org/privilege/externalstorage</privilege> /// <feature>http://tizen.org/feature/vision.inference.face</feature> /// <feature>http://tizen.org/feature/vision.inference.image</feature> /// <exception cref="FileNotFoundException"> /// <see cref="ConfigurationFilePath"/>, <see cref="WeightFilePath"/> or <see cref="CategoryFilePath"/> have invalid path. /// </exception> /// <exception cref="FileFormatException">Invalid data type is used in inference model data.</exception> /// <exception cref="InvalidDataException"> /// Inference model data contains unsupported operations in current backend version. /// -or-<br/> /// Invalid data type is used in inference model data.<br/> /// </exception> /// <exception cref="InvalidOperationException">Internal operation error.</exception> /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception> /// <since_tizen> 6 </since_tizen> public void LoadInferenceModel() { InteropInference.Configure(_inferenceHandle, GetHandle(this)). Validate("Failed to configure inference model."); var ret = InteropInference.Load(_inferenceHandle); if (ret == MediaVisionError.InvalidData) { throw new InvalidDataException("Inference model data contains unsupported operations in current backend version."); } else if (ret == MediaVisionError.NotSupportedFormat) { throw new FileFormatException("Invalid data type is used in inference model data."); } ret.Validate("Failed to load inference model."); }
/// <summary> /// Classifies image objects on the source image using inference engine set in <paramref name="config"/>.<br/> /// Each time when DetectAsync is called, a set of the detected faces at the media source are received asynchronously. /// </summary> /// <feature>http://tizen.org/feature/vision.inference</feature> /// <feature>http://tizen.org/feature/vision.inference.image</feature> /// <param name="source">The source of the media where faces will be detected.</param> /// <param name="config">The engine's configuration that will be used for classifying.</param> /// <returns> /// A task that represents the asynchronous classify operation.<br/> /// If there's no classified image object, empty collection will be returned. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="config"/> is null.</exception> /// <exception cref="InvalidOperationException">Internal error.</exception> /// <exception cref="NotSupportedException">The feature is not supported.</exception> /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception> /// <seealso cref="InferenceModelConfiguration"/> /// <since_tizen> 6 </since_tizen> public static async Task <IEnumerable <ImageClassificationResult> > ClassifyAsync(MediaVisionSource source, InferenceModelConfiguration config) { // `vision.inference` feature is already checked, when config is created. ValidationUtil.ValidateFeatureSupported(VisionFeatures.InferenceImage); if (source == null) { throw new ArgumentNullException(nameof(source)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } var tcs = new TaskCompletionSource <IEnumerable <ImageClassificationResult> >(); using (var cb = ObjectKeeper.Get(GetCallback(tcs))) { IntPtr roiUnmanaged = IntPtr.Zero; try { if (config.Roi.HasValue) { var roi = config.Roi.Value.ToMarshalable(); roiUnmanaged = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(global::Interop.MediaVision.Rectangle))); Marshal.WriteIntPtr(roiUnmanaged, IntPtr.Zero); Marshal.StructureToPtr(roi, roiUnmanaged, false); } InteropInference.ClassifyImage(source.Handle, config.GetHandle(), roiUnmanaged, cb.Target). Validate("Failed to classify image."); } finally { if (roiUnmanaged != IntPtr.Zero) { Marshal.FreeHGlobal(roiUnmanaged); } } return(await tcs.Task); } }
/// <summary> /// Initializes a new instance of the <see cref="InferenceModelConfiguration"/> class. /// </summary> /// <feature>http://tizen.org/feature/vision.inference.face</feature> /// <feature>http://tizen.org/feature/vision.inference.image</feature> /// <exception cref="NotSupportedException">The feature is not supported.</exception> /// <since_tizen> 6 </since_tizen> public InferenceModelConfiguration() : base("inference") { InteropInference.Create(out _inferenceHandle).Validate("Failed to create inference configuration"); }