Пример #1
0
        public void SamePhotoTest()
        {
            using (Image testImage = TestUtils.GetImage(TestUtils.TestPhoto1))
            {
                Tuple <ulong, byte[]> fingerPrint1 = FrameIndexer.IndexFrame(testImage);
                Tuple <ulong, byte[]> fingerPrint2 = FrameIndexer.IndexFrame(testImage);

                Assert.AreEqual(fingerPrint1.Item1, fingerPrint2.Item1);
                Assert.IsTrue(Enumerable.SequenceEqual(fingerPrint1.Item2, fingerPrint2.Item2));
            }
        }
Пример #2
0
        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);
                }
        }
Пример #3
0
        private static void ExecuteSearch(string[] args)
        {
            string photoFilePath         = GetPhotoPath(args);
            string databaseMetaTablePath = GetDatabaseMetaTable(args);

            if (string.IsNullOrWhiteSpace(photoFilePath) || string.IsNullOrWhiteSpace(databaseMetaTablePath))
            {
                PrintHelp("Photo path or database metatable path not provided");
                return;
            }

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

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

            using (Image frame = Image.FromFile(photoFilePath))
            {
                ulong providedPhotoHash = FrameIndexer.CalculateFramePerceptionHashOnly(frame);
                VideoFingerPrintDatabaseMetaTableWrapper metaTable = VideoFingerPrintDatabaseMetaTableLoader.Load(databaseMetaTablePath);
                BKTree <FrameMetricWrapper>           bktree       = ModelMetricUtils.CreateBKTree(metaTable);
                IDictionary <FrameMetricWrapper, int> treeResults  = bktree.Query(
                    new PhotoMetricWrapper
                {
                    Photo = new PhotoFingerPrintWrapper
                    {
                        FilePath = photoFilePath,
                        PHash    = providedPhotoHash,
                    },
                },
                    2
                    );

                foreach (KeyValuePair <FrameMetricWrapper, int> kvp in treeResults.OrderBy(e => e.Value))
                {
                    FrameMetricWrapper frameWrapper = kvp.Key;
                    int distance = kvp.Value;
                    Console.WriteLine(string.Format("Distance {0} for {1} at Frame {2}", distance, frameWrapper.Video.FilePath, frameWrapper.Frame.FrameNumber));
                }
            }
        }
Пример #4
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));
                }
            }
        }
Пример #5
0
        private void RunIndexer()
        {
            foreach (Tuple <Image, string> imageTuple in _fileReader.GetConsumingEnumerable())
            {
                using (imageTuple.Item1)
                {
                    Tuple <ulong, byte[]>   fingerPrintHashTuple = FrameIndexer.IndexFrame(imageTuple.Item1);
                    PhotoFingerPrintWrapper fingerPrint          = new PhotoFingerPrintWrapper
                    {
                        FilePath           = Path.GetFullPath(imageTuple.Item2),
                        PHash              = fingerPrintHashTuple.Item1,
                        EdgeGrayScaleThumb = fingerPrintHashTuple.Item2,
                    };

                    _fingerPrints.Add(fingerPrint);
                    Console.WriteLine("Indexed photo {0}", Path.GetFileName(imageTuple.Item2));
                }
            }
        }
        private void RunConsumer()
        {
            try
            {
                foreach (WorkItem item in _buffer.GetConsumingEnumerable())
                {
                    using (WritableLockBitImage frame = item.Frame)
                    {
                        frame.Lock();
                        Tuple <ulong, byte[]> fingerPrintHashTuple = FrameIndexer.IndexFrame(item.Frame.GetImage());
                        _fingerPrints.Add(new FrameFingerPrintWrapper
                        {
                            PHashCode          = fingerPrintHashTuple.Item1,
                            FrameNumber        = item.FrameNumber,
                            EdgeGrayScaleThumb = fingerPrintHashTuple.Item2,
                        });

                        // Reduce the current memory level
                        long currentMemoryLevels = Interlocked.Add(ref _currentMemoryLevel, -frame.MemorySize);

                        // Check capacity
                        if (currentMemoryLevels < _maxCapacity)
                        {
                            // If we have space, set event
                            _capacityBarrier.Set();
                        }
                        else
                        {
                            // Otherwise, reset the event
                            _capacityBarrier.Reset();
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Shutdown();
            }
        }