public void TotallyDifferentPhotosTest()
        {
            using (Image testImage1 = TestUtils.GetImage(TestUtils.TestPhoto1))
                using (Image testImage2 = TestUtils.GetImage(TestUtils.TestPhoto3))
                {
                    Tuple <ulong, byte[]> fingerPrint1 = FrameIndexer.IndexFrame(testImage1);
                    Tuple <ulong, byte[]> fingerPrint2 = FrameIndexer.IndexFrame(testImage2);
                    int distance = DistanceCalculator.CalculateHammingDistance(fingerPrint1.Item1, fingerPrint2.Item1);

                    Assert.AreEqual(10, distance);
                }
        }
Example #2
0
        private static VideoFingerPrintWrapper FindMostLikelyVideo(
            PhotoFingerPrintWrapper photo,
            IDictionary <string, ISet <FrameMetricWrapper> > treeResults,
            IDictionary <string, VideoFingerPrintWrapper> nameToVideoMap
            )
        {
            if (treeResults.Count() == 1)
            {
                return(treeResults.First().Value.First().Video);
            }

            using (WritableLockBitImage photoAsLockBitImage = new WritableLockBitImage(Image.FromFile(photo.FilePath)))
            {
                // Filter on grayscale results first
                List <FrameMetricWrapper> filteredOnGrayScaleResults = (from videoFrameResults in treeResults
                                                                        from frameMetricWrapper in videoFrameResults.Value
                                                                        where DistanceCalculator.CalculateHammingDistance(photo.EdgeGrayScaleThumb, frameMetricWrapper.Frame.EdgeGrayScaleThumb) < 2
                                                                        select frameMetricWrapper).ToList();

                if (filteredOnGrayScaleResults.Count == 1)
                {
                    return(filteredOnGrayScaleResults.First().Video);
                }

                double currentBestSSIM = 0.0;
                VideoFingerPrintWrapper currentBestVideo = null;
                foreach (FrameMetricWrapper FrameMetricWrapper in filteredOnGrayScaleResults)
                {
                    // Calculate SSIM
                    using (WritableLockBitImage videoFrame = GetFrameFromVideo(FrameMetricWrapper.Video, FrameMetricWrapper.Frame.FrameNumber))
                    {
                        double possibleBestSSIM = SSIMCalculator.Compute(photoAsLockBitImage, videoFrame);
                        // SSIM must be at least good enough for us to consider
                        if (possibleBestSSIM > currentBestSSIM)
                        {
                            currentBestSSIM  = possibleBestSSIM;
                            currentBestVideo = FrameMetricWrapper.Video;
                        }
                    }
                }

                return(currentBestVideo);
            }
        }
Example #3
0
        private static void ExecuteSearch(string[] args)
        {
            string photoFile    = GetPhotoPath(args);
            string databaseFile = GetDatabasePath(args);

            if (string.IsNullOrWhiteSpace(photoFile) || string.IsNullOrWhiteSpace(databaseFile))
            {
                PrintHelp("Photo path or database path not provided");
                return;
            }

            if (File.Exists(photoFile) == false)
            {
                PrintHelp("Photo file does not exist");
                return;
            }

            if (File.Exists(databaseFile) == false)
            {
                PrintHelp("Database does not exist");
                return;
            }

            using (Image frame = Image.FromFile(photoFile))
            {
                PhotoFingerPrintDatabaseWrapper database = PhotoFingerPrintDatabaseLoader.Load(databaseFile);
                ulong imageHash = FrameIndexer.CalculateFramePerceptionHashOnly(frame);

                var results = from fingerPrint in database.PhotoFingerPrints.AsParallel()
                              let distance = DistanceCalculator.CalculateHammingDistance(imageHash, fingerPrint.PHash)
                                             where distance < 5
                                             orderby distance
                                             select new
                {
                    Distance = distance,
                    FilePath = fingerPrint.FilePath
                };

                foreach (var result in results)
                {
                    Console.WriteLine(string.Format("{0} - {1}", result.Distance, result.FilePath));
                }
            }
        }
 /// <summary>
 /// Calculate the distance from this frame to another frame
 /// </summary>
 /// <param name="other">The other Frame</param>
 /// <returns>The hamming distance</returns>
 public int CalculateDistance(FrameFingerPrintWrapper other)
 {
     return(DistanceCalculator.CalculateHammingDistance(PHashCode, other.PHashCode));
 }
Example #5
0
 /// <summary>
 /// Calculate the distance from this frame to a picture
 /// </summary>
 /// <param name="other">The picture</param>
 /// <returns>The hamming distance</returns>
 public int CalculateDistance(PhotoFingerPrintWrapper other)
 {
     return(DistanceCalculator.CalculateHammingDistance(PHash, other.PHash));
 }