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 void ResizedImageShouldHaveAlmostOrExactly100Similarity2Test() { // arrange var hash1 = expectedHashes["Alyson_Hannigan_500x500_0.jpg"]; var hash2 = expectedHashes["Alyson_Hannigan_200x200_0.jpg"]; // act var result = CompareHash.Similarity(hash1, hash2); // assert result.Should().Be(100); }
public void TwoDifferentImagesOfGithubArePrettySimilarTests() { // arrange var hash1 = expectedHashes["github_1.jpg"]; var hash2 = expectedHashes["github_2.jpg"]; // act var result = CompareHash.Similarity(hash1, hash2); // assert result.Should().Be(89.0625); }
public void ComparingExtremelySmallImageShouldDecreaseSimilarityTest() { // arrange var hash1 = expectedHashes["Alyson_Hannigan_4x4_0.jpg"]; var hash2 = expectedHashes["Alyson_Hannigan_500x500_0.jpg"]; // act var result = CompareHash.Similarity(hash1, hash2); // assert result.Should().Be(59.375); }
public void ImageWithFilterShouldHaveAlmostOrExactly100Similarity1Test() { // arrange var hash1 = expectedHashes["Alyson_Hannigan_500x500_0.jpg"]; var hash2 = expectedHashes["Alyson_Hannigan_500x500_1.jpg"]; // act var result = CompareHash.Similarity(hash1, hash2); // assert result.Should().Be(96.875); }
public void CalculateSimilarityDemo() { var hashAlgorithm = new AverageHash(); var imageStream1 = new MemoryStream(); var imageStream2 = new MemoryStream(); #region CalculateSimilarity // calculate the two image hashes ulong hash1 = hashAlgorithm.Hash(imageStream1); ulong hash2 = hashAlgorithm.Hash(imageStream2); double percentageImageSimilarity = CompareHash.Similarity(hash1, hash2); #endregion }
static void Main(string[] args) { LOGGER.Info("Starting..."); List <Tuple <List <HashTriple>, List <Image <Rgb24> > > > bucketList = new List <Tuple <List <HashTriple>, List <Image <Rgb24> > > >(); SayItParser sayItParser = new SayItParser(); ConsoleKeyInfo keyInfo; do { LOGGER.Warn("Process sound file?"); keyInfo = Console.ReadKey(false); LOGGER.Warn("Answer: " + keyInfo.KeyChar); } while ((keyInfo.KeyChar != 'n') && (keyInfo.KeyChar != 'y')); List <string> spectralPercentList; if (keyInfo.KeyChar == 'y') { spectralPercentList = sayItParser.CreateSpectograms(PATH_TO_MP3); } else { spectralPercentList = Directory.GetFiles(".", SPECTRUM_FILE_PREFIX + "???.png").ToList(); spectralPercentList = spectralPercentList.Take(spectralPercentList.Count * RUN_X_PERCENT / 100).ToList(); spectralPercentList.Sort(); } List <Image <Rgb24> > wordSpectrums = sayItParser.ExtractWordSpectrograms(spectralPercentList); List <HashTriple> hashes = sayItParser.CalculateHashes(wordSpectrums); LOGGER.Info("Matching up hashes..."); int hashPercentage = -1; int hashCount = 0; foreach (HashTriple hash in hashes) { hashCount++; if (hashCount * 100 / hashes.Count > hashPercentage) { hashPercentage = hashCount * 100 / hashes.Count; LOGGER.Info("Bucket list {per,3}% done...", hashPercentage); } double bestOneTwoAvg = 0; int bestIndex = -1; foreach (var bucket in bucketList) { List <double> bucketComparison = new List <double>(); foreach (var bucketHash in bucket.Item1) { bucketComparison.Add((CompareHash.Similarity(hash.AvgHash, bucketHash.AvgHash) + CompareHash.Similarity(hash.DiffHash, bucketHash.DiffHash)) / 2); } if (bucketComparison.Average() > bestOneTwoAvg) { bestOneTwoAvg = bucketComparison.Average(); bestIndex = bucketList.IndexOf(bucket); } } if (bestOneTwoAvg > BUCKET_HASH_MATCH_THRESHOLD) { bucketList[bestIndex].Item1.Add(hash); bucketList[bestIndex].Item2.Add(wordSpectrums[hashes.IndexOf(hash)]); } else { bucketList.Add(new Tuple <List <HashTriple>, List <Image <Rgb24> > >(new List <HashTriple>(new HashTriple[] { hash }), new List <Image <Rgb24> >(new Image <Rgb24>[] { wordSpectrums[hashes.IndexOf(hash)] }))); } } LOGGER.Info("Found {} different buckets.", bucketList.Count); LOGGER.Info("Saving buckets..."); Directory.CreateDirectory("buckets"); foreach (var bucket in bucketList) { string bucketName = "bucket" + (bucketList.IndexOf(bucket) + 1); Directory.CreateDirectory("buckets/" + bucketName); foreach (var word in bucket.Item2) { string entryName = "word" + (bucket.Item2.IndexOf(word) + 1) + ".png"; word.SaveAsPng("buckets/" + bucketName + "/" + entryName); } } }
private static void ComputeAndSaveSimilarity(string header, ulong hash1, ulong hash2, StringBuilder bldr) { double percentageImageSimilarity = CompareHash.Similarity(hash1, hash2); bldr.AppendLine($"{header}{percentageImageSimilarity}"); }