Skip to content

Commit

Permalink
- OCR optimization
Browse files Browse the repository at this point in the history
- Setting to set default DPI
- Performance improvements
  • Loading branch information
MrFlapstaart committed Aug 15, 2022
1 parent ff84754 commit 1ee614d
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 71 deletions.
17 changes: 17 additions & 0 deletions ImageProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ internal static Bitmap CaptureScreenshot(Rectangle bounds)

return bitmap;
}

internal static Bitmap CropImage(Image original, Rectangle bounds)
{
Bitmap bitmap = new Bitmap(bounds.Width + 200, bounds.Height + 200);

using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(original,
10, 10,
new Rectangle(bounds.Left, bounds.Top, bounds.Width, bounds.Height),
GraphicsUnit.Pixel
);
}

return bitmap;
}

internal static Color GetColorFromCurrentPixel()
{
Bitmap bitmap = new Bitmap(10, 10);
Expand Down
48 changes: 44 additions & 4 deletions MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 24 additions & 14 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ namespace GameOCRTTS
{
public partial class MainForm : Form
{
private KeyboardHook _Hook = new KeyboardHook();
private Color _Brightest = Color.White;
private int _FadeDistance = 15;
private KeyboardHook _Hook = new KeyboardHook();
private static readonly string GithubUsername = "MrFlapstaart";
private LiveUpdate _LiveUpdater = new LiveUpdate(GithubUsername);
private OCR _OCR = new OCR();

public MainForm()
{
Expand All @@ -25,9 +24,12 @@ public MainForm()

InitializeComponent();

colorPanel.BackColor = _Brightest;
distanceBar.Value = _FadeDistance;
distanceLabel.Text = _FadeDistance.ToString();
colorPanel.BackColor = _OCR.Brightest;
distanceBar.Value = _OCR.FadeDistance;
distanceLabel.Text = _OCR.FadeDistance.ToString();

defaultdpiBar.Value = _OCR.DefaultScaleDPI;
defaultdpiLabel.Text = _OCR.DefaultScaleDPI.ToString();

voiceCombo.Items.AddRange(TTS.GetVoices().ToArray());
voiceCombo.SelectedIndex = 0;
Expand All @@ -41,7 +43,7 @@ private void Hook_KeyPressed(object sender, KeyPressedEventArgs e)
if (e.Modifier == SpecialKeys.Control)
{
Color color = ImageProc.GetColorFromCurrentPixel();
_Brightest = color;
_OCR.Brightest = color;
colorPanel.BackColor = color;
SFXPlayer.PlayOK();
return;
Expand Down Expand Up @@ -81,9 +83,10 @@ private void ocrButton_Click(object sender, EventArgs e)

private void ProcessImage(Bitmap bitmap, bool forcefullscale)
{
OCRResult result = OCR.HandleOCR(bitmap, _Brightest, _FadeDistance, forcefullscale);
Image resultimage = result.ProcessedImage;
OCRResult result = _OCR.HandleOCR(bitmap, forcefullscale);
Image resultimage = result.ProcessedImage;
ocrBox.Text = result.ResultText;
Logger.AddLog("Original text: " + result.OriginalText);

if (processedImage.Image != null)
processedImage.Image.Dispose();
Expand Down Expand Up @@ -132,19 +135,20 @@ private void garbageButton_Click(object sender, EventArgs e)

private void selectColorButton_Click(object sender, EventArgs e)
{
colorSelect.Color = _Brightest;
colorSelect.Color = _OCR.Brightest;
if (colorSelect.ShowDialog() != DialogResult.OK)
return;

_Brightest = colorSelect.Color;
colorPanel.BackColor = _Brightest;
_OCR.Brightest = colorSelect.Color;
colorPanel.BackColor = _OCR.Brightest;
}

private void distanceBar_Scroll(object sender, EventArgs e)
{
_FadeDistance = distanceBar.Value;
distanceLabel.Text = _FadeDistance.ToString();
_OCR.FadeDistance = distanceBar.Value;
distanceLabel.Text = _OCR.FadeDistance.ToString();
}

// Context menu links.
private void contextMenuHelp_Click(object sender, EventArgs e)
{
Expand Down Expand Up @@ -207,6 +211,12 @@ private void contextMenuAbout_Click(object sender, EventArgs e)
{
MessageBox.Show($"{_LiveUpdater.Product} version {_LiveUpdater.CurrentVersion} by @MrFlapstaart and @wrt54g", "About", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void defaultdpiBar_Scroll(object sender, EventArgs e)
{
_OCR.DefaultScaleDPI = defaultdpiBar.Value;
defaultdpiLabel.Text = defaultdpiBar.Value.ToString();
}
// End of context menu links.
}
}
Loading

0 comments on commit 1ee614d

Please sign in to comment.