Skip to content

Commit

Permalink
Reduce CPU consumption
Browse files Browse the repository at this point in the history
- Add GetHashCode for DirectBitmap
- Cache hashcode -> OCR strings to prevent multiple recognition on same image
  • Loading branch information
Lunat1q committed May 5, 2019
1 parent ed2825b commit 4db3eb1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 49 deletions.
23 changes: 17 additions & 6 deletions ScreenAssistant.ScreenInfoRecognition/DirectBitmap.cs
@@ -1,25 +1,26 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;

namespace TiqSoft.ScreenAssistant.ScreenInfoRecognition
{
public class DirectBitmap : IDisposable
{
public Bitmap Bitmap { get; private set; }
public Int32[] Bits { get; private set; }
public Bitmap Bitmap { get; }
public int[] Bits { get; }
public bool Disposed { get; private set; }
public int Height { get; private set; }
public int Width { get; private set; }
public int Height { get; }
public int Width { get; }

protected GCHandle BitsHandle { get; private set; }
protected GCHandle BitsHandle { get; }

public DirectBitmap(int width, int height)
{
Width = width;
Height = height;
Bits = new Int32[width * height];
Bits = new int[width * height];
BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
}
Expand All @@ -41,12 +42,22 @@ public Color GetPixel(int x, int y)
return result;
}

internal Bitmap ToBitmap()
{
return (Bitmap)Bitmap.Clone();
}

public void Dispose()
{
if (Disposed) return;
Disposed = true;
Bitmap.Dispose();
BitsHandle.Free();
}

public override int GetHashCode()
{
return Bits.Aggregate(Bits.Length, (current, t) => unchecked(current * 31 + t));
}
}
}
99 changes: 56 additions & 43 deletions ScreenAssistant.ScreenInfoRecognition/WeaponTypeScreenRecognizer.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using IronOcr;
Expand All @@ -10,7 +11,8 @@ public static class WeaponTypeScreenRecognizer
{

private static readonly Random Rnd = new Random();


private static readonly Dictionary<int, string> resultCache = new Dictionary<int, string>();

public static bool IsFirstWeaponActive()
{
Expand Down Expand Up @@ -70,47 +72,51 @@ private static void SaveTestImage(this Image image)
image.Save(@"\TestSC\" + fileName, ImageFormat.Tiff);
}

private static Image AdjustImageForRecognition(Image img)
private static DirectBitmap GetAdjustedDirectBitmapOfImage(Image img)
{
Image result;
using (var db = new DirectBitmap(img.Width, img.Height))
var db = new DirectBitmap(img.Width, img.Height);

using (var graphics = Graphics.FromImage(db.Bitmap))
{
using (var graphics = Graphics.FromImage(db.Bitmap))
{
graphics.DrawImage(img, Point.Empty);
}
graphics.DrawImage(img, Point.Empty);
}

for (var i = 0; i < img.Width; i++)
for (var i = 0; i < img.Width; i++)
{
for (var j = 0; j < img.Height; j++)
{
for (var j = 0; j < img.Height; j++)
var currentPixel = db.GetPixel(i, j);
var brightness = currentPixel.GetBrightness();
if (brightness < 0.85f)
{
var currentPixel = db.GetPixel(i, j);
var brightness = currentPixel.GetBrightness();
if (brightness < 0.85f)
{
db.SetPixel(i, j, Color.White);
}
else
{
db.SetPixel(i,j, Color.Black);
}
db.SetPixel(i, j, Color.White);
}
else
{
db.SetPixel(i, j, Color.Black);
}
}

result = (Bitmap) db.Bitmap.Clone();
}

return result;
return db;
}

public static string TestWeapons()
{
var w1Img = GetWeapon1Image();
var w2Img = GetWeapon2Image();
SaveTestImage(w1Img);
SaveTestImage(AdjustImageForRecognition(w1Img));
using (var db = GetAdjustedDirectBitmapOfImage(w1Img))
{
SaveTestImage(db.ToBitmap());
}

SaveTestImage(w2Img);
SaveTestImage(AdjustImageForRecognition(w2Img));
using (var db = GetAdjustedDirectBitmapOfImage(w1Img))
{
SaveTestImage(db.ToBitmap());
}

return WeaponImageToString(w1Img) + WeaponImageToString(w2Img);
}

Expand All @@ -129,23 +135,32 @@ private static string WeaponImageToString(Image img)
var result = "";
try
{
var img2 = AdjustImageForRecognition(img);
var ocr = new AdvancedOcr
using (var db = GetAdjustedDirectBitmapOfImage(img))
{
CleanBackgroundNoise = true,
EnhanceContrast = true,
EnhanceResolution = true,
Language = IronOcr.Languages.English.OcrLanguagePack,
Strategy = AdvancedOcr.OcrStrategy.Advanced,
ColorSpace = AdvancedOcr.OcrColorSpace.GrayScale,
DetectWhiteTextOnDarkBackgrounds = false,
InputImageType = AdvancedOcr.InputTypes.Snippet,
RotateAndStraighten = false,
ReadBarCodes = false,
ColorDepth = 8
};
var res = ocr.Read(img2);
result = res.Text.ToUpper();
if (resultCache.TryGetValue(db.GetHashCode(), out result))
{
return result;
}

var img2 = db.ToBitmap();
var ocr = new AdvancedOcr
{
CleanBackgroundNoise = true,
EnhanceContrast = true,
EnhanceResolution = true,
Language = IronOcr.Languages.English.OcrLanguagePack,
Strategy = AdvancedOcr.OcrStrategy.Advanced,
ColorSpace = AdvancedOcr.OcrColorSpace.GrayScale,
DetectWhiteTextOnDarkBackgrounds = false,
InputImageType = AdvancedOcr.InputTypes.Snippet,
RotateAndStraighten = false,
ReadBarCodes = false,
ColorDepth = 8
};
var res = ocr.Read(img2);
result = res.Text.ToUpper();
resultCache.Add(db.GetHashCode(), result);
}
}
catch
{
Expand All @@ -154,7 +169,5 @@ private static string WeaponImageToString(Image img)

return result;
}


}
}

0 comments on commit 4db3eb1

Please sign in to comment.