Skip to content

Commit

Permalink
closes #3; Fix wrong outputs coming from asynchronous operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kabuto_W committed Oct 11, 2021
1 parent f6901c7 commit 379ef24
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions SimilarImages/SimilarImages/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ private void bgw_Calculate_DoWork(object sender, DoWorkEventArgs e)
Stopwatch watch = new Stopwatch();
watch.Start();

tuples = null;
tuples = ImageHash.GetSimilarity(folderPathes, out int count,
precision, interpolationMode, hashEnum, threshold);
lb_Count.Invoke((Action)(() => { lb_Count.Text = count.ToString(); }));
Expand Down
13 changes: 7 additions & 6 deletions SimilarImages/SimilarImages/ImageHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static List<Tuple<string, string, double>> GetSimilarity(
{
throw new DirectoryNotFoundException("Directory not found.");
}
Debug.WriteLine($"Hash Algorithm: {hashEnum}\nPrecision: {precision}\n" +
$"Interpolation Mode: {interpolationMode}\nThreshold: {threshold}%");
Debug.WriteLine($"HashAlgorithm: {hashEnum} Precision: {precision}\n" +
$"InterpolationMode: {interpolationMode} Threshold: {threshold}%");

watch.Restart();

Expand Down Expand Up @@ -62,7 +62,7 @@ public static List<Tuple<string, string, double>> GetSimilarity(
watch.Restart();

// Compare hashes
var tuples = new List<Tuple<string, string, double>>();
var tuples = new ConcurrentBag<Tuple<string, string, double>>();
Parallel.For(0, imageHashPairs.Length, i =>
{
for (int j = imageHashPairs.Length - 1; j > i; j--)
Expand All @@ -81,9 +81,10 @@ public static List<Tuple<string, string, double>> GetSimilarity(
watch.Stop();
long compareTime = watch.ElapsedMilliseconds;
Debug.WriteLine($"GetHash: {hashTime}ms; CompareHash: {compareTime}ms");
Debug.WriteLine("TuplesCount:" + tuples.Count);

// Sort by similarity
return tuples.OrderByDescending(u => u.Item3).ToList(); // TODO: seldom u=null.
return tuples.OrderByDescending(u => u.Item3).ToList();
}

public enum HashEnum
Expand All @@ -100,7 +101,7 @@ private static KeyValuePair<string, string>[] GetImageHashes(string folderPath,
var imageNames = from file in di.GetFiles()
where imageExtensions.Contains(file.Extension)
select file.FullName;
Debug.WriteLine($"Directory: {folderPath}\nImage count: {imageNames.Count()}");
Debug.WriteLine($"Directory: {folderPath}; ImageCount: {imageNames.Count()}");
if (imageNames.Count() < 2) { return null; }

// Get hash algorithm
Expand Down Expand Up @@ -137,7 +138,7 @@ where imageExtensions.Contains(file.Extension)
});

watch.Stop();
Debug.WriteLine($"Valid count: {imageHashPairs.Count} elapsed time: {watch.ElapsedMilliseconds}ms");
Debug.WriteLine($"ValidCount: {imageHashPairs.Count}; ElapsedTime: {watch.ElapsedMilliseconds}ms");
return imageHashPairs.ToArray();
}

Expand Down
4 changes: 2 additions & 2 deletions SimilarImages/SimilarImages/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.3.1.0")]
[assembly: AssemblyFileVersion("1.3.1.0")]
Binary file modified _Output/SimilarImages.exe
Binary file not shown.
Binary file modified _Output/zh-CN/SimilarImages.resources.dll
Binary file not shown.

0 comments on commit 379ef24

Please sign in to comment.