Skip to content

Commit

Permalink
Merge pull request #190 from Cupcak3/master
Browse files Browse the repository at this point in the history
Bug Fixes and Optimizations
  • Loading branch information
Andrewthe13th authored Jan 29, 2022
2 parents 8da1f41 + 92d0ca5 commit f163ce8
Show file tree
Hide file tree
Showing 14 changed files with 601 additions and 297 deletions.
4 changes: 3 additions & 1 deletion InventoryKamera/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="InventoryKamera.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="GenshinGuide.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
Expand Down Expand Up @@ -44,6 +43,9 @@
<setting name="MinimumArtifactRarity" serializeAs="String">
<value>4</value>
</setting>
<setting name="UpgradeNeeded" serializeAs="String">
<value>True</value>
</setting>
</InventoryKamera.Properties.Settings>
</userSettings>
</configuration>
113 changes: 81 additions & 32 deletions InventoryKamera/ArtifactScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,51 @@ public static void ScanArtifacts(int count = 0)
private static int ScanArtifactCount()
{
//Find artifact count
int left = (int)(1028 / 1280.0 * Navigation.GetWidth());
int top = (int)(20 / 720.0 * Navigation.GetHeight());
int right = (int)(1208 / 1280.0 * Navigation.GetWidth());
int bottom = (int)(45 / 720.0 * Navigation.GetHeight());
var region = new Rectangle(
x: (int)(1030 / 1280.0 * Navigation.GetWidth()),
y: (int)(20 / 720.0 * Navigation.GetHeight()),
width: (int)( 175 / 1280.0 * Navigation.GetWidth() ),
height: (int)( 25 / 720.0 * Navigation.GetHeight() ));

using (Bitmap bm = Navigation.CaptureRegion(new RECT(left, top, right, bottom)))
using (Bitmap countBitmap = Navigation.CaptureRegion(region))
{
UserInterface.SetNavigation_Image(bm);
UserInterface.SetNavigation_Image(countBitmap);

Bitmap nBM = Scraper.ConvertToGrayscale(bm);
Scraper.SetContrast(60.0, ref nBM);
Scraper.SetInvert(ref nBM);
Bitmap n = Scraper.ConvertToGrayscale(countBitmap);
Scraper.SetContrast(60.0, ref n);
Scraper.SetInvert(ref n);

string text = Scraper.AnalyzeText(nBM).Trim();
nBM.Dispose();
text = Regex.Replace(text, @"[^\d/]", string.Empty);
string text = Scraper.AnalyzeText(n).Trim();
n.Dispose();

// Remove any non-numeric and '/' characters
text = Regex.Replace(text, @"[^0-9/]", string.Empty);

if (string.IsNullOrWhiteSpace(text))
{
countBitmap.Save($"./logging/artifacts/ArtifactCount.png");
Navigation.CaptureWindow().Save($"./logging/artifacts/ArtifactWindow_{Navigation.GetWidth()}x{Navigation.GetHeight()}.png");
throw new FormatException("Unable to locate artifact count.");
}

int count;
// Check for dash

// Check for slash
if (Regex.IsMatch(text, "/"))
{
count = Int32.Parse(text.Split('/')[0]);
count = int.Parse(text.Split('/')[0]);
Debug.WriteLine($"Parsed {count} for artifact count");
}
else
else if (Regex.Matches(text, "1500").Count == 1) // Remove the inventory limit from number
{
// divide by the number on the right if both numbers fused
count = Int32.Parse(text) / 1500;
text = text.Replace("1500", string.Empty);
count = int.Parse(text);
Debug.WriteLine($"Parsed {count} for artifact count");
}

// Check if larger than 1500
while (count > 1500)
else // Extreme worst case
{
count /= 10;
count = 1500;
Debug.WriteLine("Defaulted to 1500 for artifact count");
}

return count;
Expand All @@ -129,7 +141,7 @@ private static int ScanArtifactCount()

private static (List<Rectangle> rectangles, int cols, int rows) GetPageOfItems()
{
// Size of an item card is the same in 16:10 to 16:9. Also accounts for character icon and resolution size.
// Size of an item card is the same in 16:10 and 16:9. Also accounts for character icon and resolution size.
var card = new RECT(
Left: 0,
Top: 0,
Expand All @@ -146,10 +158,11 @@ private static (List<Rectangle> rectangles, int cols, int rows) GetPageOfItems()
MaxWidth = card.Width + 10,
})
{

// Screenshot of inventory
Bitmap screenshot = Navigation.CaptureWindow();
Bitmap output = new Bitmap(screenshot); // Copy used to overlay onto in testing

// Copy used to overlay onto in testing
Bitmap output = new Bitmap(screenshot);

// Image pre-processing
ContrastCorrection contrast = new ContrastCorrection(85);
Expand Down Expand Up @@ -295,9 +308,14 @@ private static (List<Rectangle> rectangles, int cols, int rows) GetPageOfItems()
Debug.WriteLine($"{rectangles.Count} rectangles");

// Generated rectangles
//new RectanglesMarker(rectangles, Color.Green).ApplyInPlace(output);
new RectanglesMarker(rectangles, Color.Green).ApplyInPlace(output);
//Navigation.DisplayBitmap(output, "Rectangles");

if (colCoords.Count < 7 || rowCoords.Count < 5)
{
output.Save($"./logging/artifacts/ArtifactInventory_{colCoords.Count}x{rowCoords.Count}.png");
}

screenshot.Dispose();
output.Dispose();
return (rectangles, colCoords.Count, rowCoords.Count);
Expand Down Expand Up @@ -404,7 +422,7 @@ public static void QueueScan(int id)

try
{
int rarity = GetRarity(card.GetPixel(5, 5));
int rarity = GetRarity(card);
if (0 < rarity && rarity < Properties.Settings.Default.MinimumArtifactRarity)
{
artifactImages.ForEach(i => i.Dispose());
Expand Down Expand Up @@ -444,8 +462,7 @@ public static async Task<Artifact> CatalogueFromBitmapsAsync(List<Bitmap> bm, in
int a_gearSlot = 0; int a_mainStat = 1; int a_level = 2; int a_subStats = 3; int a_equippedCharacter = 4; int a_lock = 5; int a_card = 6;

// Get Rarity
Color rarityColor = bm[a_card].GetPixel(5, 5);
rarity = GetRarity(rarityColor);
rarity = GetRarity(bm[a_card]);

// Check for equipped color
Color equippedColor = Color.FromArgb(255, 255, 231, 187);
Expand Down Expand Up @@ -481,8 +498,13 @@ public static async Task<Artifact> CatalogueFromBitmapsAsync(List<Bitmap> bm, in
return new Artifact(setName, rarity, level, gearSlot, mainStat, subStats.ToArray(), subStats.Count, equippedCharacter, id, _lock);
}

private static int GetRarity(Color rarityColor)
private static int GetRarity(Bitmap bitmap)
{
int x = (int)(10/1280.0 * Navigation.GetWidth());
int y = (int)(10/720.0 * Navigation.GetHeight());

Color rarityColor = bitmap.GetPixel(x,y);

Color fiveStar = Color.FromArgb(255, 188, 105, 50);
Color fourStar = Color.FromArgb(255, 161, 86, 224);
Color threeStar = Color.FromArgb(255, 81, 127, 203);
Expand All @@ -497,6 +519,33 @@ private static int GetRarity(Color rarityColor)
else return 0; // throw new ArgumentException("Unable to determine artifact rarity");
}

public static bool IsEnhancementMaterial(Bitmap card)
{
RECT reference = Navigation.GetAspectRatio() == new Size(16, 9) ?
new RECT(new Rectangle(862, 80, 327, 560)) : (RECT)new Rectangle(862, 80, 328, 640);
Bitmap nameBitmap = card.Clone(new RECT(
Left: 0,
Top: 0,
Right: card.Width,
Bottom: (int)( 38.0 / reference.Height * card.Height )), card.PixelFormat);
string material = ScanEnhancementMaterialName(nameBitmap);
return !string.IsNullOrWhiteSpace(material) && Scraper.enhancementMaterials.Contains(material.ToLower());
}

private static string ScanEnhancementMaterialName(Bitmap bm)
{
Scraper.SetGamma(0.2, 0.2, 0.2, ref bm);
Bitmap n = Scraper.ConvertToGrayscale(bm);
Scraper.SetInvert(ref n);

// Analyze
string name = Regex.Replace(Scraper.AnalyzeText(n).ToLower(), @"[\W]", string.Empty);
name = Scraper.FindClosestMaterialName(name, 3);
n.Dispose();

return name;
}

#region Task Methods

private static string ScanArtifactGearSlot(Bitmap bm)
Expand Down Expand Up @@ -601,7 +650,7 @@ private static List<SubStat> ScanArtifactSubStats(Bitmap artifactImage, ref stri
SubStat substat = new SubStat();
Regex re = new Regex(@"([\w]+\W*)(\d+.*\d+)");
var result = re.Match(line);
var stat = Regex.Replace(result.Groups[1].Value, @"[^\w]", string.Empty);
var stat = Regex.Replace(result.Groups[1].Value, @"[^\w]", string.Empty);
var value = result.Groups[2].Value;

string name = line.Contains("%") ? stat + "%" : stat;
Expand All @@ -618,7 +667,7 @@ private static List<SubStat> ScanArtifactSubStats(Bitmap artifactImage, ref stri
}

// Need to retain the decimal place for percent boosts
if (substat.stat.Contains("_")) substat.value /= 10;
if (substat.stat.Contains("_")) substat.value /= 10;

substats[j] = substat;
return null;
Expand Down Expand Up @@ -669,7 +718,7 @@ private static string ScanArtifactEquippedCharacter(Bitmap bm)
{
equippedCharacter = Regex.Replace(equippedCharacter.Split(':')[1], @"[\W]", string.Empty);
equippedCharacter = Scraper.FindClosestCharacterName(equippedCharacter);

return equippedCharacter;
}
}
Expand Down
Loading

0 comments on commit f163ce8

Please sign in to comment.