Beispiel #1
0
 public static IEnumerable <(double v, ImgHash)> GetHashes(ImgHash img)
 {
     // ReSharper disable once LoopCanBeConvertedToQuery
     foreach (var f in Directory.EnumerateFiles("../Cheaturu.Compare.Precalc/data"))
     {
         var hash = new ImgHash
         {
             HashData = File.ReadAllText(f).Trim().Split(",").Select(bool.Parse).ToArray(),
             FilePath = f
         };
         yield return(hash.CompareWith(img), hash);
     }
 }
        private async Task<Boyan> ProcessImageBoyan(Message message)
        {
            var biggestPhoto = message.Photo.OrderByDescending(x => x.FileSize).FirstOrDefault();

            var tempFolderPath = Path.Combine(AppContext.BaseDirectory, "Tmp");
            if(Directory.Exists(tempFolderPath) == false)
                Directory.CreateDirectory(tempFolderPath);

            var filePath = Path.Combine(tempFolderPath, $"{Guid.NewGuid()}.jpg");
            using (var stream = File.Create(filePath))
            {
                var file = await Bot.GetInfoAndDownloadFileAsync(biggestPhoto.FileId, stream);
            }

            var hash = new ImgHash();
            hash.GenerateFromPath(filePath);

            Boyan boyan = null;
            foreach (var imageBoyan in Boyans.Items.Where(x => x.ImageHash != null))
            {
                var similarity = hash.CompareWith(imageBoyan.ImageHash);
                if (similarity >= 99)
                {
                    boyan = imageBoyan;
                    break;
                }
            }

            if(boyan == null)
            {
                var newBoyan = new Boyan { ImageHash = hash.HashData, MessageId = message.MessageId.ToString(), ChatId = message.Chat.Id.ToString(), DateCreated = DateTimeOffset.UtcNow };
                Boyans.Items.Add(newBoyan);
            }

            return boyan;
        }