Exemplo n.º 1
0
        public override async Task OnMessageReceived(IUserMessage message, ITextChannel channel)
        {
            if (IgnoreNSFW && channel.IsNsfw)
            {
                return;
            }
            if (message.Attachments.Count == 0)
            {
                return;
            }
            var attachment = message.Attachments.First();
            var path       = Path.Combine(Path.GetTempPath(), $"{message.Id}_{attachment.Filename}");

            if (File.Exists(path) == false)
            {
                using var wc = new WebClient();
                wc.DownloadFile(attachment.Url, path);
            }
            using var fstream = new FileStream(path, FileMode.Open);
            var algo = new DifferenceHash();
            var hash = algo.Hash(fstream);
            var sim  = CompareHash.Similarity(ImageHash, hash);

            if (sim < 97.5)
            {
                return;
            }
            await message.DeleteAndTrackAsync($"Image {sim:00}% similar to one blocked under {Id}");

            await this.Escalate(message.Author as SocketGuildUser, $"Similarity: {sim:00.0}%");
        }
        public async Task <ImageInfo> Analyse(Stream imageStream)
        {
            // ReSharper disable once UseAwaitUsing
            if (imageStream.Length == 0)
            {
                throw new Exception("Image stream is 0");
            }

            byte[] shaHash;
            using (var sha256Hasher = SHA256.Create())
            {
                shaHash = await sha256Hasher.ComputeHashAsync(imageStream);
            }

            imageStream.Position = 0;
            var(image, format)   = await SixLabors.ImageSharp.Image.LoadWithFormatAsync <Rgba32>(imageStream);

            using (image)
            {
                var width    = image.Width;
                var height   = image.Height;
                var mimeType = format.DefaultMimeType;

                var averageHasher = new AverageHash();
                var averageHash   = averageHasher.Hash(image);

                var perceptualHasher = new PerceptualHash();
                var perceptualHash   = perceptualHasher.Hash(image);
                var differenceHasher = new DifferenceHash();
                var differenceHash   = differenceHasher.Hash(image);

                return(new ImageInfo(shaHash, averageHash, differenceHash, perceptualHash, width, height, mimeType));
            }
        }
Exemplo n.º 3
0
 private static long CalculateActualHash(string fileName)
 {
     using var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
     using var image      = PpmReader.ImageFromPpm(fileStream);
     ImagePipeline.FromImage(image).Webp(new WebpOptions(100, 100, true)).ToBuffer(out var actual);
     return(DifferenceHash.HashLong(actual));
 }
Exemplo n.º 4
0
 public ImageHashFacade(IFileSystem fileSystem)
 {
     this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
     averageHash     = new AverageHash();
     differenceHash  = new DifferenceHash();
     perceptualHash  = new PerceptualHash();
 }
Exemplo n.º 5
0
        private static void Main()
        {
            ImagePipeline
            .FromFile(TestFiles.InputJpg)
            .Png()
            .ToBuffer(out var b);
            var hashLong  = DifferenceHash.HashLong(b);
            var hashShort = DifferenceHash.HashShort(b);

            Console.WriteLine(Reverse(Convert.ToString(hashLong, 2)));
            Console.WriteLine(Reverse(Convert.ToString(hashShort, 2)));
        }
Exemplo n.º 6
0
 private void btnDifferenceHash_Click(object sender, EventArgs e)
 {
     try
     {
         DifferenceHash dHash1 = new DifferenceHash(bmpSlika1);
         DifferenceHash dHash2 = new DifferenceHash(bmpSlika2);
         lblHashPrveSlike.Text  = "Hash prve slike:  " + dHash1.GetHash();
         lblHashDrugeSlike.Text = "Hash druge slike:" + dHash2.GetHash();
         int brojIstihBitova = DifferenceHash.IzracunavanjeSlicnihBitova(dHash1.GetHash(), dHash2.GetHash());
         lblIstiBitovi.Text = "Isti bitovi:" + " " + brojIstihBitova.ToString();
         int brojRazlicitihBitova = DifferenceHash.IzracunavanjeRazlicitihBitova(dHash1.GetHash(), dHash2.GetHash());
         lblRazliciti1.Text = brojRazlicitihBitova.ToString();
         double prosjek = DifferenceHash.IzracunajPostotakSlicnosti(brojIstihBitova, brojRazlicitihBitova, dHash1.GetHash().Length);
         lblRazliciti2.Text = prosjek.ToString() + "%";
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Niste uploadali sliku!", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
Exemplo n.º 7
0
        async Task reactBlockImage(ITextChannel channel, Cacheable <IUserMessage, ulong> cacheMessage, SocketReaction reaction)
        {
            var message = await cacheMessage.GetOrDownloadAsync();

            var user = await channel.GetUserAsync(reaction.UserId);

            await message.RemoveReactionAsync(reaction.Emote, user);

            if (user.GuildPermissions.Administrator == false)
            {
                await user.SendMessageAsync("You do not have permission to block content for this guild");

                return;
            }
            if (message.Attachments.Count == 0)
            {
                await user.SendMessageAsync("To block text content, you must use a topic block via command.");

                return;
            }
            var attachment = message.Attachments.First();
            var path       = Path.Combine(Path.GetTempPath(), $"{message.Id}_{attachment.Filename}");

            if (File.Exists(path) == false)
            {
                using var wc = new WebClient();
                wc.DownloadFile(attachment.Url, path);
            }

            using var fstream = new FileStream(path, FileMode.Open);
            var algo = new DifferenceHash();
            var hash = algo.Hash(fstream);
            var p    = await AddImageBlock(user as SocketGuildUser, null, "Via emoji, none given", null, hash, guildwide : true);

            await message.DeleteAndTrackAsync($"Violated image block {p.Id}");

            await user.SendMessageAsync($"Added indefinite image block for hash {hash}; id of penalty: {p.Id}");
        }
Exemplo n.º 8
0
 public DifferenceHashTest()
 {
     sut = new DifferenceHash();
 }
Exemplo n.º 9
0
 private static long CalculateExpectedHash(string fileName)
 {
     ImagePipeline.FromFile(fileName).Webp(new WebpOptions(100, 100, true)).ToBuffer(out var expected);
     return(DifferenceHash.HashLong(expected));
 }
 public short ComputeShort() => DifferenceHash.HashShort(buffer);
 public long ComputeLong() => DifferenceHash.HashLong(buffer);