diff --git a/src/OcrInspector/MainWindow.xaml b/src/OcrInspector/MainWindow.xaml
index 731959b..4a27a77 100644
--- a/src/OcrInspector/MainWindow.xaml
+++ b/src/OcrInspector/MainWindow.xaml
@@ -40,10 +40,19 @@
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="100"
- Margin="10"
+ Margin="10,10,0,0"
Click="BtnLoadImage_Click"
Grid.Row="1"/>
+
+
Words) ResolveWords(string imagePath)
{
// Load the image
- var image = new Image(imagePath);
+ using var image = new Image(imagePath);
// Initialize Tesseract OCR
- var ocr = new Tesseract("TrainData/", "eng", OcrEngineMode.TesseractOnly);
-
+ using var ocr = new Tesseract("TrainData/", "eng", OcrEngineMode.Default);
+ ocr.PageSegMode = Emgu.CV.OCR.PageSegMode.SparseText;
// Convert to grayscale for OCR processing (Tesseract requires a grayscale image)
- var grayImage = image.Convert();
+ using var grayImage = image.Convert();
// Perform OCR on the grayscale image and get the recognized words
ocr.SetImage(grayImage);
@@ -107,6 +164,29 @@ private static (ImageSource ImageSource, List Words) ResolveWord
return (image.ToBitmapSource(), words);
}
+ // Converts the provided Bitmap for processing with Tesseract OCR, converts it to grayscale, recognizes the text,
+ // draws bounding boxes around the recognized words, and returns the processed image along with the list of recognized words.
+ private static (ImageSource ImageSource, List Words) ResolveWords(Bitmap bitmap)
+ {
+ // Load the image
+ using var image = bitmap.ToImage();
+ // Initialize Tesseract OCR
+ using var ocr = new Tesseract("TrainData/", "eng", OcrEngineMode.Default);
+ ocr.PageSegMode = Emgu.CV.OCR.PageSegMode.SparseText;
+ // Convert to grayscale for OCR processing (Tesseract requires a grayscale image)
+ using var grayImage = image.Convert();
+
+ // Perform OCR on the grayscale image and get the recognized words
+ ocr.SetImage(grayImage);
+ ocr.Recognize();
+
+ // Get the words from the OCR result
+ var words = ocr.GetWords().ToList();
+
+ // Save or display the image
+ return (grayImage.ToBitmapSource(), words);
+ }
+
// Adds a clickable point to the MainCanvas at the location of the given word.
// The point displays a tooltip with the word text and accuracy.
private void AddClickablePoint(Tesseract.Word word)
@@ -126,11 +206,11 @@ private void AddClickablePoint(Tesseract.Word word)
};
// Create a rectangle to represent the clickable point on the image
- Rectangle rectangle = new()
+ System.Windows.Shapes.Rectangle rectangle = new()
{
- Fill = Brushes.Transparent,
+ Fill = System.Windows.Media.Brushes.Transparent,
Height = word.Region.Height,
- Stroke = Brushes.Blue,
+ Stroke = System.Windows.Media.Brushes.Blue,
StrokeThickness = 1,
Tag = word.Region.Location,
ToolTip = tooltip,
@@ -155,5 +235,49 @@ private void AddClickablePoint(Tesseract.Word word)
// Add the rectangle to the MainCanvas to display the clickable point on the image
MainCanvas.Children.Add(rectangle);
}
+
+ #region ExternalMethods
+ [DllImport("user32.dll")]
+ static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DevMode devMode);
+
+ [StructLayout(LayoutKind.Sequential)]
+ struct DevMode
+ {
+ [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
+ public string dmDeviceName;
+ public short dmSpecVersion;
+ public short dmDriverVersion;
+ public short dmSize;
+ public short dmDriverExtra;
+ public int dmFields;
+ public int dmPositionX;
+ public int dmPositionY;
+ public int dmDisplayOrientation;
+ public int dmDisplayFixedOutput;
+ public short dmColor;
+ public short dmDuplex;
+ public short dmYResolution;
+ public short dmTTOption;
+ public short dmCollate;
+ [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
+ public string dmFormName;
+ public short dmLogPixels;
+ public int dmBitsPerPel;
+ public int dmPelsWidth;
+ public int dmPelsHeight;
+ public int dmDisplayFlags;
+ public int dmDisplayFrequency;
+ public int dmICMMethod;
+ public int dmICMIntent;
+ public int dmMediaType;
+ public int dmDitherType;
+ public int dmReserved1;
+ public int dmReserved2;
+ public int dmPanningWidth;
+ public int dmPanningHeight;
+ }
+ #endregion
}
+
+
}