/*
* 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 static Interop.MediaVision.Surveillance;
namespace Tizen.Multimedia.Vision
{
///
/// Provides the ability to recognize person on image sources.
///
///
/// 3
public class PersonRecognizer : SurveillanceEngine
{
private const string KeyCount = "NUMBER_OF_PERSONS";
private const string KeyLocations = "PERSONS_LOCATIONS";
private const string KeyLabels = "PERSONS_LABELS";
private const string KeyConfidences = "PERSONS_CONFIDENCES";
private const string PersonRecognizedEventType = "MV_SURVEILLANCE_EVENT_PERSON_RECOGNIZED";
///
/// Initializes a new instance of the class.
///
/// The feature is not supported.
/// 3
public PersonRecognizer() : base(PersonRecognizedEventType)
{
}
///
/// Occurs when a person recognized.
///
/// The event handler will be executed on an internal thread.
///
/// 3
public event EventHandler Recognized;
internal override void OnEventDetected(IntPtr trigger, IntPtr source, int streamId,
IntPtr result, IntPtr _)
{
try
{
Recognized?.Invoke(this, CreatePersonRecognizedEventArgs(result));
}
catch (Exception e)
{
MultimediaLog.Error(MediaVisionLog.Tag, "Failed to invoke Recognized event.", e);
}
}
private PersonRecognizedEventArgs CreatePersonRecognizedEventArgs(IntPtr result)
{
int count;
GetResultValue(result, KeyCount, out count).Validate("Failed to get result count");
var recognitionInfo = new PersonRecognitionInfo[count];
if (count > 0)
{
var rects = new global::Interop.MediaVision.Rectangle[count];
GetResultValue(result, KeyLocations, rects).Validate("Failed to get location");
var labels = new int[count];
GetResultValue(result, KeyLabels, labels).Validate("Failed to get label");
var confidences = new double[count];
GetResultValue(result, KeyConfidences, confidences).Validate("Failed to get confidence");
for (int i = 0; i < count; i++)
{
recognitionInfo[i] = new PersonRecognitionInfo(rects[i].ToApiStruct(),
labels[i], confidences[i]);
}
}
return new PersonRecognizedEventArgs(recognitionInfo);
}
///
/// Adds with the provided .
///
/// The source used for recognition.
/// The config for the .
///
/// is null.\n
/// -or-\n
/// is null.
///
///
/// The has already been disposed of.\n
/// -or-\n
/// has already been disposed of.
///
///
/// of does not exists.
///
///
/// No permission to access to the .
///
/// The model file is not supported format or file.
///
/// 3
public void AddSource(SurveillanceSource source, PersonRecognitionConfiguration config)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
InvokeAddSource(source, config);
}
}
}