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 /// <feature>http://tizen.org/feature/vision.face_recognition</feature>
26 /// <feature>http://tizen.org/feature/vision.image_recognition</feature>
27 /// <seealso cref="PersonRecognitionConfiguration"/>
28 /// <since_tizen> 4 </since_tizen>
29 public class PersonRecognizer : SurveillanceEngine
31 private const string KeyCount = "NUMBER_OF_PERSONS";
32 private const string KeyLocations = "PERSONS_LOCATIONS";
33 private const string KeyLabels = "PERSONS_LABELS";
34 private const string KeyConfidences = "PERSONS_CONFIDENCES";
36 private const string PersonRecognizedEventType = "MV_SURVEILLANCE_EVENT_PERSON_RECOGNIZED";
39 /// Initializes a new instance of the <see cref="PersonRecognizer"/> class.
41 /// <exception cref="NotSupportedException">The required features are not supported.</exception>
42 /// <since_tizen> 4 </since_tizen>
43 public PersonRecognizer() : base(PersonRecognizedEventType)
48 /// Occurs when a person recognized.
50 /// <remarks>The event handler will be executed on an internal thread.</remarks>
51 /// <seealso cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>
52 /// <since_tizen> 4 </since_tizen>
53 public event EventHandler<PersonRecognizedEventArgs> Recognized;
55 internal override void OnEventDetected(IntPtr trigger, IntPtr source, int streamId,
56 IntPtr result, IntPtr _)
60 Recognized?.Invoke(this, CreatePersonRecognizedEventArgs(result));
64 MultimediaLog.Error(MediaVisionLog.Tag, "Failed to invoke Recognized event.", e);
68 private PersonRecognizedEventArgs CreatePersonRecognizedEventArgs(IntPtr result)
72 GetResultValue(result, KeyCount, out count).Validate("Failed to get result count");
74 var recognitionInfo = new PersonRecognitionInfo[count];
78 var rects = new global::Interop.MediaVision.Rectangle[count];
79 GetResultValue(result, KeyLocations, rects).Validate("Failed to get location");
81 var labels = new int[count];
82 GetResultValue(result, KeyLabels, labels).Validate("Failed to get label");
84 var confidences = new double[count];
85 GetResultValue(result, KeyConfidences, confidences).Validate("Failed to get confidence");
87 for (int i = 0; i < count; i++)
89 recognitionInfo[i] = new PersonRecognitionInfo(rects[i].ToApiStruct(),
90 labels[i], confidences[i]);
94 return new PersonRecognizedEventArgs(recognitionInfo);
98 /// Adds <see cref="SurveillanceSource"/> with the provided <see cref="PersonRecognitionConfiguration"/>.
100 /// <param name="source">The source used for recognition.</param>
101 /// <param name="config">The config for the <paramref name="source"/>.</param>
102 /// <exception cref="ArgumentNullException">
103 /// <paramref name="source"/> is null.<br/>
105 /// <paramref name="config"/> is null.
107 /// <exception cref="ObjectDisposedException">
108 /// The <see cref="PersonRecognizer"/> has already been disposed of.<br/>
110 /// <paramref name="config"/> has already been disposed of.
112 /// <exception cref="ArgumentException">
113 /// <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/> of <paramref name="config"/> does not exists.
115 /// <exception cref="UnauthorizedAccessException">
116 /// No permission to access to the <see cref="PersonRecognitionConfiguration.FaceRecognitionModelPath"/>.
118 /// <exception cref="NotSupportedException">The model file is not supported format or file.</exception>
119 /// <see cref="SurveillanceSource.Push(MediaVisionSource)"/>
120 /// <since_tizen> 4 </since_tizen>
121 public void AddSource(SurveillanceSource source, PersonRecognitionConfiguration config)
125 throw new ArgumentNullException(nameof(config));
127 InvokeAddSource(source, config);