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.
18 using static Interop.MediaVision.Surveillance;
20 namespace Tizen.Multimedia
23 /// Provides the ability to recognize person on image sources.
25 /// <seealso cref="PersonRecognitionConfiguration"/>
26 public class PersonRecognizer : SurveillanceEngine
28 private const string KeyCount = "NUMBER_OF_PERSONS";
29 private const string KeyLocations = "PERSONS_LOCATIONS";
30 private const string KeyLabels = "PERSONS_LABELS";
31 private const string KeyConfidences = "PERSONS_CONFIDENCES";
33 private const string PersonRecognizedEventType = "MV_SURVEILLANCE_EVENT_PERSON_RECOGNIZED";
36 /// Initializes a new instance of the <see cref="PersonRecognizer"/> class.
38 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
39 public PersonRecognizer() : base(PersonRecognizedEventType)
44 /// Occurs when a person recognized.
46 /// <remarks>The event handler will be executed on an internal thread.</remarks>
47 /// <seealso cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>
48 public event EventHandler<PersonRecognizedEventArgs> Recognized;
50 internal override void OnEventDetected(IntPtr trigger, IntPtr source, int streamId,
51 IntPtr result, IntPtr _)
55 Recognized?.Invoke(this, CreatePersonRecognizedEventArgs(result));
59 MultimediaLog.Error(MediaVisionLog.Tag, "Failed to invoke Recognized event.", e);
63 private PersonRecognizedEventArgs CreatePersonRecognizedEventArgs(IntPtr result)
67 GetResultValue(result, KeyCount, out count).Validate("Failed to get result count");
69 var recognitionInfo = new PersonRecognitionInfo[count];
73 var rects = new Interop.MediaVision.Rectangle[count];
74 GetResultValue(result, KeyLocations, rects).Validate("Failed to get location");
76 var labels = new int[count];
77 GetResultValue(result, KeyLabels, labels).Validate("Failed to get label");
79 var confidences = new double[count];
80 GetResultValue(result, KeyConfidences, confidences).Validate("Failed to get confidence");
82 for (int i = 0; i < count; i++)
84 recognitionInfo[i] = new PersonRecognitionInfo(rects[i].ToApiStruct(),
85 labels[i], confidences[i]);
89 return new PersonRecognizedEventArgs(recognitionInfo);
93 /// Adds <see cref="SurveillanceSource"/> with the provided <see cref="PersonRecognitionConfiguration"/>.
95 /// <param name="source">The source used for recognition.</param>
96 /// <param name="config">The config for the <paramref name="source"/>.</param>
97 /// <exception cref="ArgumentNullException">
98 /// <paramref name="source"/> is null.\n
100 /// <paramref name="config"/> is null.
102 /// <exception cref="ObjectDisposedException">
103 /// The <see cref="PersonRecognizer"/> has already been disposed of.\n
105 /// <paramref name="config"/> has already been disposed of.
107 /// <exception cref="System.IO.FileNotFoundException">
108 /// <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/> of <paramref name="config"/> does not exists.
110 /// <exception cref="UnauthorizedAccessException">
111 /// No permission to access to the <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>.
113 /// <exception cref="NotSupportedException">The model file is not supported format or file.</exception>
114 /// <see cref="SurveillanceSource.Push(MediaVisionSource)"/>
115 public void AddSource(SurveillanceSource source, PersonRecognitionConfiguration config)
119 throw new ArgumentNullException(nameof(config));
121 InvokeAddSource(source, config);