I've been messing around with OCR, i have a small method to avoid bottraps which does fine for now, but i want more.
I started out with Tesseract, mostly because its freely available instead of M$'s MODI (depends on M$ Office installation). Tesseract can only read uncompressed tiff. This required some searching cause the default bild.Save("tmp.tif", ImageFormat.Tiff) saves compressed, i stole this method from the internet:
Code:
public static void saveUncompressedTiff(Bitmap bild)
{
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// Get an ImageCodecInfo object that represents the TIFFcodec.
myImageCodecInfo = GetEncoder(ImageFormat.Tiff);
// Create an Encoder object based on the GUID
// for the Compression parameter category.
myEncoder = System.Drawing.Imaging.Encoder.Compression;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a TIFF file with no compression.
myEncoderParameter = new EncoderParameter(myEncoder, (long)System.Drawing.Imaging.EncoderValue.CompressionNone);
myEncoderParameters.Param[0] = myEncoderParameter;
bild.Save("tmp.tif", myImageCodecInfo, myEncoderParameters);
}
public static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
But my experiments gave very poor results, even with the size of the image tripled. after that i started experimenting with MODI. That was as simple as adding a reference to the Microsoft Office Document Imaging 12.0 Type Library. The following code takes a file from disk and passes on what it reads.
Code:
public static bool BottrapFound()
{
// Get the bmp we're gonna read
Bitmap bild = Tools.GetBitmap(MainWindow.BasisX + 486, MainWindow.BasisY + 26, 180, 15);
// Make it twice as big, MODI wont recognize anything otherwise
using (Graphics g = Graphics.FromImage((Image)bild))
g.DrawImage(bild, 0, 0, 360, 30);
// Since MODI seems only be able to read from disk...
// bild.Save("tmp.tif", ImageFormat.Tiff); -> compressed we need uncompressed
saveUncompressedTiff(bild);
System.Threading.Thread.Sleep(500);
// Initialize word(s) we're getting
string strText = "";
bool ReturnValue = false;
// Instantiate the MODI.Document object
MODI.Document md = new MODI.Document();
// The Create method grabs the picture from
// disk snd prepares for OCR.
md.Create(@"tmp.tif");
// Do the OCR.
md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
// Get the first (and only image)
MODI.Image image = (MODI.Image)md.Images[0];
// Get the layout.
MODI.Layout layout = image.Layout;
// Loop through the words.
for (int j = 0; j < layout.Words.Count; j++)
{
// Get this word.
MODI.Word word = (MODI.Word)layout.Words[j];
// Add a blank space to separate words.
if (strText.Length > 0)
{
strText += " ";
}
// Add the word.
strText += word.Text;
}
// Close the MODI.Document object.
md.Close(false);
switch(strText)
{
case "Moon Rabbit":
case "Tamias":
case "Megaloceros":
//MainWindow.AddToProtokoll(strText + " found!");
ReturnValue = true;
break;
default:
break;
}
return ReturnValue;
}
This code doesn't work! I know MODI reads the generated image correct, cause it can read it in M$ Document Imaging application. I need some help here... First of, is there a way to get rid of the writing and reading from disk, which makes the whole process far too slow. We need to do this in memory to get it fast enough. It worked with the tiff, not sure if a double or tripled bmp will be read too. Anyone can help me out here?