Skip to content

Commit 3bd6959

Browse files
committed
Image analysis and similarity
1 parent 8367d04 commit 3bd6959

File tree

9 files changed

+123
-9
lines changed

9 files changed

+123
-9
lines changed

SmartImage/Core/ConfigComponents.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ internal static T ReadComponentMapValue<T>(object obj, IDictionary<string, strin
116116

117117

118118
var v = ReadComponentMapValue(cfg, name, setDefaultIfNull, defaultValue);
119-
Debug.WriteLine($"{v} -> {name} {field.Name}");
119+
//Debug.WriteLine($"{v} -> {name} {field.Name}");
120120
return v;
121121
}
122122

@@ -163,7 +163,7 @@ internal static void ResetComponents(object obj)
163163
var dv = attr.DefaultValue;
164164
field.SetValue(obj, dv);
165165

166-
Debug.WriteLine($"Reset {dv} -> {field.Name}");
166+
//Debug.WriteLine($"Reset {dv} -> {field.Name}");
167167
}
168168
}
169169

SmartImage/Core/Interface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private static NConsoleOption[] AllOptions
9494
/// <summary>
9595
/// Console window height
9696
/// </summary>
97-
internal const int ConsoleWindowHeight = 50;
97+
internal const int ConsoleWindowHeight = 56;
9898

9999
/// <summary>
100100
/// Main option

SmartImage/Engines/Other/GoogleImagesEngine.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
using System;
2+
using System.Diagnostics;
13
using System.Drawing;
4+
using System.Linq;
5+
using HtmlAgilityPack;
6+
using SmartImage.Searching;
27

38
namespace SmartImage.Engines.Other
49
{
@@ -11,5 +16,8 @@ public GoogleImagesEngine() : base("http://images.google.com/searchbyimage?image
1116
public override SearchEngineOptions Engine => SearchEngineOptions.GoogleImages;
1217

1318
public override Color Color => Color.CornflowerBlue;
19+
20+
21+
// https://html-agility-pack.net/knowledge-base/2113924/how-can-i-use-html-agility-pack-to-retrieve-all-the-images-from-a-website-
1422
}
1523
}

SmartImage/Engines/Other/IqdbEngine.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ private BasicSearchResult ParseResult(HtmlNodeCollection tr)
3838

3939
if (urlNode.Name != "img") {
4040
var origUrl = urlNode.Attributes["href"].Value;
41-
42-
Debug.WriteLine(origUrl);
43-
41+
4442
// Links must begin with http:// in order to work with "start"
4543
if (origUrl.StartsWith("//")) {
4644
origUrl = "http:" + origUrl;

SmartImage/Engines/Other/YandexEngine.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Drawing;
5+
using System.IO;
46
using System.Linq;
57
using System.Net;
68
using HtmlAgilityPack;
79
using SimpleCore.Net;
810
using SimpleCore.Utilities;
11+
using SmartImage.Core;
912
using SmartImage.Searching;
1013

1114
#pragma warning disable HAA0101, HAA0601, HAA0502, HAA0401
@@ -126,6 +129,7 @@ public override FullSearchResult GetResult(string url)
126129
sr.Description = looksLike;
127130

128131
sr.AddExtendedResults(bestImages);
132+
129133
}
130134
catch (Exception e) {
131135
// ...

SmartImage/Searching/BasicSearchResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public struct BasicSearchResult : ISearchResult
4040
public int? FullResolution => Width * Height;
4141

4242
public BasicSearchResult(string url, int? width, int? height)
43-
: this(url, null, width, height, url, null, null) { }
43+
: this(url, null, width, height, null, null, null) { }
4444

4545
public BasicSearchResult(string url, float? similarity, int? width, int? height,
4646
string? siteName, string? source, string? description)

SmartImage/Searching/FullSearchResult.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public FullSearchResult(SearchEngineOptions src, Color color, string name, strin
3636
Similarity = similarity;
3737
ExtendedInfo = new List<string>();
3838
ExtendedResults = new List<FullSearchResult>();
39+
3940
}
4041

4142
/// <summary>
@@ -48,6 +49,8 @@ public FullSearchResult(SearchEngineOptions src, Color color, string name, strin
4849
/// </summary>
4950
public bool IsPriority => SearchConfig.Config.PriorityEngines.HasFlag(Engine);
5051

52+
public bool IsAnalyzed { get; set; }
53+
5154
/// <summary>
5255
/// Displays <see cref="ExtendedResults" />, if any, in a new menu
5356
/// </summary>
@@ -346,6 +349,7 @@ public static FullSearchResult GetOriginalImageResult(string imageUrl, FileInfo
346349
var result = new FullSearchResult(SearchEngineOptions.None, Color.White, ORIGINAL_IMAGE_NAME, imageUrl)
347350
{
348351
Similarity = 100.0f,
352+
IsAnalyzed = true,
349353
};
350354

351355
var fileFormat = FileSystem.ResolveFileType(imageFile.FullName);

SmartImage/Searching/SearchClient.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
using System.IO;
1818
using System.Linq;
1919
using System.Media;
20+
using System.Net;
2021
using System.Threading;
2122
using System.Threading.Tasks;
23+
using SimpleCore.Net;
2224
using static SimpleCore.Console.CommandLine.NConsoleOption;
2325

2426
// ReSharper disable ConvertIfStatementToReturnStatement
@@ -78,6 +80,7 @@ public sealed class SearchClient
7880
/// </summary>
7981
public NConsoleInterface Interface { get; }
8082

83+
8184
private SearchClient(string img)
8285
{
8386
if (!Images.IsFileValid(img)) {
@@ -114,6 +117,30 @@ private SearchClient(string img)
114117
}
115118

116119

120+
private static async void RunAnalysis(FullSearchResult best)
121+
{
122+
123+
var task = Task.Run(() =>
124+
{
125+
if (!best.IsAnalyzed) {
126+
var d = Images.Similarity(best.Url, SearchConfig.Config.Image);
127+
128+
if (d.HasValue)
129+
{
130+
best.Similarity = ((float)d);
131+
}
132+
}
133+
134+
best.IsAnalyzed = true;
135+
136+
137+
138+
});
139+
140+
await task;
141+
}
142+
143+
117144
/// <summary>
118145
/// Starts search and handles results
119146
/// </summary>
@@ -128,7 +155,7 @@ public async void Start()
128155
var result = finished.Result;
129156

130157
Results.Add(result);
131-
Results.Sort(FullSearchResult.CompareResults);
158+
132159

133160
// If the engine is priority, open its result in the browser
134161
if (result.IsPriority) {
@@ -139,8 +166,23 @@ public async void Start()
139166

140167
Interface.Status = $"Searching: {inProgress}/{len}";
141168

169+
Results.Sort(FullSearchResult.CompareResults);
170+
142171
// Reload console UI
143172
NConsole.Refresh();
173+
174+
175+
/*
176+
*
177+
*/
178+
179+
RunAnalysis(result);
180+
181+
if (result.ExtendedResults.Any()) {
182+
foreach (var resultExtendedResult in result.ExtendedResults) {
183+
RunAnalysis(resultExtendedResult);
184+
}
185+
}
144186
}
145187

146188
/*
@@ -173,6 +215,23 @@ public async void Start()
173215
HandleResultOpen(best);
174216

175217
}
218+
219+
/*
220+
*
221+
*/
222+
223+
Debug.WriteLine($"Analyzing");
224+
225+
226+
while (!Results.All(r=>r.IsAnalyzed)) {
227+
228+
}
229+
230+
Debug.WriteLine($"Analysis complete");
231+
232+
Results.Sort(FullSearchResult.CompareResults);
233+
NConsole.Refresh();
234+
176235
}
177236

178237

SmartImage/Utilities/Images.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Drawing;
45
using System.IO;
56
using System.Linq;
7+
using System.Net;
68
using System.Text;
79
using System.Threading.Tasks;
810
using Novus.Win32;
911
using SimpleCore.Console.CommandLine;
12+
using SimpleCore.Net;
13+
using SmartImage.Core;
1014

1115
namespace SmartImage.Utilities
1216
{
@@ -15,6 +19,43 @@ namespace SmartImage.Utilities
1519
/// </summary>
1620
internal static class Images
1721
{
22+
/*
23+
* ImageHash - too high
24+
* Shipwreck phash - cross correlation
25+
*/
26+
27+
public static double? Similarity(string url, string f)
28+
{
29+
var m = Network.IdentifyType(url);
30+
31+
if (m == null) {
32+
return null;
33+
}
34+
35+
var t = Network.IsImage(m);
36+
37+
Debug.WriteLine($"{url} is image: {t}");
38+
39+
40+
if (t) {
41+
42+
43+
// using var s = Image.FromFile(f);
44+
// using var s2 = Image.FromStream(new WebClient().OpenRead(url));
45+
//
46+
//
47+
//
48+
// return Similarity(s, s2);
49+
}
50+
51+
return null;
52+
}
53+
54+
public static double? Similarity(Image a, Image b)
55+
{
56+
return null;
57+
}
58+
1859
internal static (int Width, int Height) GetDimensions(string img)
1960
{
2061
var bmp = new Bitmap(img);
@@ -42,4 +83,4 @@ internal static bool IsFileValid(string img)
4283
return true;
4384
}
4485
}
45-
}
86+
}

0 commit comments

Comments
 (0)