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.Vision
23 /// Provides the ability to recognize person on image sources.
25 /// <seealso cref="PersonRecognitionConfiguration"/>
26 /// <since_tizen> 3 </since_tizen>
27 public class PersonRecognizer : SurveillanceEngine
29 private const string KeyCount = "NUMBER_OF_PERSONS";
30 private const string KeyLocations = "PERSONS_LOCATIONS";
31 private const string KeyLabels = "PERSONS_LABELS";
32 private const string KeyConfidences = "PERSONS_CONFIDENCES";
34 private const string PersonRecognizedEventType = "MV_SURVEILLANCE_EVENT_PERSON_RECOGNIZED";
37 /// Initializes a new instance of the <see cref="PersonRecognizer"/> class.
39 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
40 /// <since_tizen> 3 </since_tizen>
41 public PersonRecognizer() : base(PersonRecognizedEventType)
46 /// Occurs when a person recognized.
48 /// <remarks>The event handler will be executed on an internal thread.</remarks>
49 /// <seealso cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>
50 /// <since_tizen> 3 </since_tizen>
51 public event EventHandler<PersonRecognizedEventArgs> Recognized;
53 internal override void OnEventDetected(IntPtr trigger, IntPtr source, int streamId,
54 IntPtr result, IntPtr _)
58 Recognized?.Invoke(this, CreatePersonRecognizedEventArgs(result));
62 MultimediaLog.Error(MediaVisionLog.Tag, "Failed to invoke Recognized event.", e);
66 private PersonRecognizedEventArgs CreatePersonRecognizedEventArgs(IntPtr result)
70 GetResultValue(result, KeyCount, out count).Validate("Failed to get result count");
72 var recognitionInfo = new PersonRecognitionInfo[count];
76 var rects = new global::Interop.MediaVision.Rectangle[count];
77 GetResultValue(result, KeyLocations, rects).Validate("Failed to get location");
79 var labels = new int[count];
80 GetResultValue(result, KeyLabels, labels).Validate("Failed to get label");
82 var confidences = new double[count];
83 GetResultValue(result, KeyConfidences, confidences).Validate("Failed to get confidence");
85 for (int i = 0; i < count; i++)
87 recognitionInfo[i] = new PersonRecognitionInfo(rects[i].ToApiStruct(),
88 labels[i], confidences[i]);
92 return new PersonRecognizedEventArgs(recognitionInfo);
96 /// Adds <see cref="SurveillanceSource"/> with the provided <see cref="PersonRecognitionConfiguration"/>.
98 /// <param name="source">The source used for recognition.</param>
99 /// <param name="config">The config for the <paramref name="source"/>.</param>
100 /// <exception cref="ArgumentNullException">
101 /// <paramref name="source"/> is null.<br/>
103 /// <paramref name="config"/> is null.
105 /// <exception cref="ObjectDisposedException">
106 /// The <see cref="PersonRecognizer"/> has already been disposed of.<br/>
108 /// <paramref name="config"/> has already been disposed of.
110 /// <exception cref="ArgumentException">
111 /// <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/> of <paramref name="config"/> does not exists.
113 /// <exception cref="UnauthorizedAccessException">
114 /// No permission to access to the <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>.
116 /// <exception cref="NotSupportedException">The model file is not supported format or file.</exception>
117 /// <see cref="SurveillanceSource.Push(MediaVisionSource)"/>
118 /// <since_tizen> 3 </since_tizen>
119 public void AddSource(SurveillanceSource source, PersonRecognitionConfiguration config)
123 throw new ArgumentNullException(nameof(config));
125 InvokeAddSource(source, config);