Ejemplo n.º 1
0
        /// <summary>
        /// Compute similarity score between two <see cref="Person"/>s.
        /// </summary>
        /// <param name="probe">First of the two persons to compare.</param>
        /// <param name="candidate">Second of the two persons to compare.</param>
        /// <returns>Similarity score indicating similarity between the two persons or 0 if there is no match.</returns>
        /// <remarks>
        /// <para>
        /// <see cref="Verify"/> method compares two <see cref="Person"/>s, <see cref="Fingerprint"/> by <see cref="Fingerprint"/>, and returns
        /// floating-point similarity score that indicates degree of similarity between
        /// the two <see cref="Person"/>s. If this score falls below <see cref="Threshold"/>, <see cref="Verify"/> method returns zero.
        /// </para>
        /// <para>
        /// <see cref="Person"/>s passed to this method must have valid <see cref="Fingerprint.Template"/>
        /// for every <see cref="Fingerprint"/>, i.e. they must have passed through <see cref="Extract"/> method.
        /// </para>
        /// </remarks>
        /// <seealso cref="Threshold"/>
        /// <seealso cref="MinMatches"/>
        /// <seealso cref="Identify"/>
        public float Verify(Person probe, Person candidate)
        {
            lock (this)
            {
                probe.CheckForNulls();
                candidate.CheckForNulls();
                BestMatchSkipper collector = new BestMatchSkipper(1, MinMatches - 1);
                Parallel.ForEach(probe.Fingerprints, probeFp =>
                    {
                        var candidateTemplates = (from candidateFp in candidate.Fingerprints
                                                  where IsCompatibleFinger(probeFp.Finger, candidateFp.Finger)
                                                  select candidateFp.Decoded).ToList();

                        ParallelMatcher.PreparedProbe probeIndex = Matcher.Prepare(probeFp.Decoded);
                        float[] scores = Matcher.Match(probeIndex, candidateTemplates);

                        lock (collector)
                            foreach (float score in scores)
                                collector.AddScore(0, score);
                    });

                return ApplyThreshold(collector.GetSkipScore(0));
            }
        }
Ejemplo n.º 2
0
 IEnumerable<Person> GetMatchingCandidates(Person[] candidateArray, BestMatchSkipper.PersonsSkipScore[] results)
 {
     foreach (var match in results)
         if (match.Score >= Threshold)
             yield return candidateArray[match.Person];
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Compares one <see cref="Person"/> against a set of other <see cref="Person"/>s and returns best matches.
        /// </summary>
        /// <param name="probe">Person to look up in the collection.</param>
        /// <param name="candidates">Collection of persons that will be searched.</param>
        /// <returns>All matching <see cref="Person"/> objects in the collection or an empty collection if
        /// there is no match. Results are sorted by score in descending order. If you need only one best match,
        /// call <see cref="Enumerable.FirstOrDefault{T}(IEnumerable{T})"/> method on the returned collection.</returns>
        /// <remarks>
        /// <para>
        /// Compares probe <see cref="Person"/> to all candidate <see cref="Person"/>s and returns the most similar
        /// candidates. Calling <see cref="Identify"/> is conceptually identical to calling <see cref="Verify"/> in a loop
        /// except that <see cref="Identify"/> is significantly faster than loop of <see cref="Verify"/> calls.
        /// If there is no candidate with score at or above <see cref="Threshold"/>, <see cref="Identify"/> returns
        /// empty collection.
        /// </para>
        /// <para>
        /// Most applications need only the best match, which can be obtained by calling
        /// <see cref="Enumerable.FirstOrDefault{T}(IEnumerable{T})"/> method on the returned collection.
        /// Matching score for every returned <see cref="Person"/> can be obtained by calling
        /// <see cref="Verify"/> on probe <see cref="Person"/> and the matching <see cref="Person"/>.
        /// </para>
        /// <para>
        /// <see cref="Person"/>s passed to this method must have valid <see cref="Fingerprint.Template"/>
        /// for every <see cref="Fingerprint"/>, i.e. they must have passed through <see cref="Extract"/> method.
        /// </para>
        /// </remarks>
        /// <seealso cref="Threshold"/>
        /// <seealso cref="MinMatches"/>
        /// <seealso cref="Verify"/>
        public IEnumerable<Person> Identify(Person probe, IEnumerable<Person> candidates)
        {
            probe.CheckForNulls();
            Person[] candidateArray = candidates.ToArray();
            BestMatchSkipper.PersonsSkipScore[] results;
            lock (this)
            {
                BestMatchSkipper collector = new BestMatchSkipper(candidateArray.Length, MinMatches - 1);
                Parallel.ForEach(probe.Fingerprints, probeFp =>
                    {
                        List<int> personsByFingerprint = new List<int>();
                        List<Template> candidateTemplates = FlattenHierarchy(candidateArray, probeFp.Finger, out personsByFingerprint);

                        ParallelMatcher.PreparedProbe probeIndex = Matcher.Prepare(probeFp.Decoded);
                        float[] scores = Matcher.Match(probeIndex, candidateTemplates);

                        lock (collector)
                            for (int i = 0; i < scores.Length; ++i)
                                collector.AddScore(personsByFingerprint[i], scores[i]);
                    });
                results = collector.GetSortedScores();
            }
            return GetMatchingCandidates(candidateArray, results);
        }