Created
June 9, 2019 13:05
-
-
Save Lachee/9ad032bcdfb6d207a88ceb30028f61a8 to your computer and use it in GitHub Desktop.
Recognises Rainbow Six: Siege icons.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ImageMagick; | |
using Newtonsoft.Json.Linq; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace SiegeOpDetector | |
{ | |
class Program | |
{ | |
static HttpClient http = new HttpClient(); | |
const string CacheFolder = "cache/"; | |
const string Source = @"D:\dok_crop.png"; | |
static void Main(string[] args) | |
{ | |
//CacheOperators().Wait(); | |
var bmtask = BestMatch(Source); | |
bmtask.Wait(); | |
Console.WriteLine("Done: " + bmtask.Result); | |
Console.ReadKey(); | |
} | |
/// <summary> | |
/// Finds the file with the best match out of the .shrink.png images. | |
/// </summary> | |
/// <param name="source"></param> | |
/// <returns></returns> | |
static async Task<string> BestMatch(string source, double minMetric = 0.70) | |
{ | |
return await Task.Run(() => | |
{ | |
double bestMatch = minMetric; | |
string bestFile = ""; | |
using (var imgSource = new MagickImage(source)) | |
{ | |
foreach (string file in Directory.EnumerateFiles(CacheFolder, "*.shrink.png")) | |
{ | |
using (var imgShrink = new MagickImage(file)) | |
{ | |
double diff = imgSource.Compare(imgShrink, new ErrorMetric()); | |
if (diff > bestMatch) | |
{ | |
bestMatch = diff; | |
bestFile = file; | |
} | |
} | |
} | |
} | |
return bestFile; | |
}); | |
} | |
/// <summary> | |
/// Downloads the icon for every operator, then shrinks them and stores both images into the file cache. | |
/// </summary> | |
/// <returns></returns> | |
static async Task CacheOperators() | |
{ | |
//Make sure cache exists | |
if (!Directory.Exists(CacheFolder)) | |
Directory.CreateDirectory(CacheFolder); | |
//Get all the operators | |
JObject response = await GetOperators(); | |
foreach (var x in response) | |
{ | |
//Download their image and crop their images. | |
string name = x.Key; | |
string url = x.Value["img"].ToString(); | |
string path = Path.Combine(CacheFolder, name + ".png"); | |
string pathShrink = Path.Combine(CacheFolder, name + ".shrink.png"); | |
Console.WriteLine("Processing " + name); | |
//Download file | |
await DownloadFileAsync(new Uri(url), path); | |
//Shrink the file | |
using (var source = new MagickImage(path)) | |
{ | |
source.Crop(new MagickGeometry(19, 21, 89, 89)); | |
source.Scale(22, 22); | |
source.RePage(); | |
source.Write(pathShrink); | |
} | |
} | |
} | |
/// <summary> | |
/// Downloads a list of operators | |
/// </summary> | |
/// <returns></returns> | |
static async Task<JObject> GetOperators() | |
{ | |
HttpResponseMessage response = await http.GetAsync("https://d.lu.je/siege/operators.php?username=KommadantKlink"); | |
if (response.IsSuccessStatusCode) | |
{ | |
string content = await response.Content.ReadAsStringAsync(); | |
return JObject.Parse(content); | |
} | |
return null; | |
} | |
/// <summary> | |
/// Downloads a file | |
/// </summary> | |
/// <param name="requestUri"></param> | |
/// <param name="filename"></param> | |
/// <returns></returns> | |
public static async Task DownloadFileAsync(Uri requestUri, string filename) | |
{ | |
if (filename == null) | |
throw new ArgumentNullException("filename"); | |
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri)) | |
{ | |
using (Stream contentStream = await (await http.SendAsync(request)).Content.ReadAsStreamAsync(), stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None)) | |
{ | |
await contentStream.CopyToAsync(stream); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment