### Starting with Default Configuration Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md Begin your configuration by using the default RapidOcrOptions preset. This provides a balanced starting point for most OCR tasks. ```csharp var options = RapidOcrOptions.Default; // or PythonCompat ``` -------------------------------- ### Quick Start: Initialize and Detect Text Source: https://github.com/bobld/rapidocrnet/blob/master/README.md Loads bundled PP-OCRv5 latin models and performs OCR on an image file. Prints the full recognized text and details for each detected block. ```csharp using RapidOcrNet; using SkiaSharp; using var ocr = new RapidOcr(); ocr.InitModels(); // loads bundled PP-OCRv5 latin models OcrResult result = ocr.Detect("image.png", RapidOcrOptions.Default); Console.WriteLine(result.StrRes); // full recognized text, one line per block foreach (var block in result.TextBlocks) { Console.WriteLine($"{block.Text} (avg score: {block.CharScores!.Average():F2})"); } ``` -------------------------------- ### IBoxPoints Usage Example Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/types.md Demonstrates how to use the IBoxPoints interface to access and draw the corner points of a detected box. ```csharp void DrawBox(IBoxPoints box, SKCanvas canvas) { var pts = box.BoxPoints; // Draw the box... } ``` -------------------------------- ### ITextBox Usage Example Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/types.md Shows how to use the ITextBox interface to annotate a box with its recognized text on a canvas. ```csharp void AnnotateBox(ITextBox box, SKCanvas canvas) { canvas.DrawText(box.Text, box.BoxPoints[0].X, box.BoxPoints[0].Y, ...); } ``` -------------------------------- ### Incremental Configuration Adjustment Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md Make small, incremental adjustments to configuration options to fine-tune OCR performance. This example slightly increases the BoxScoreThresh from its default. ```csharp var options = RapidOcrOptions.Default with { BoxScoreThresh = 0.55f // Small step from 0.5 }; ``` -------------------------------- ### Configuring Image Resizing Strategies in RapidOcrOptions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Demonstrates two distinct image resizing strategies using the 'ImgResize' property. The first example retains legacy behavior by capping the longer side, while the second enables Python-style adaptive short-side resizing by setting ImgResize to 0. ```csharp // Legacy behavior: cap longer side at 1024 var opt1 = RapidOcrOptions.Default; // Python-style: upscale short side to 736 var opt2 = RapidOcrOptions.Default with { ImgResize = 0 }; ``` -------------------------------- ### Adjusting LimitSideLen for Python-style Resize Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Shows how to configure the 'LimitSideLen' property when using Python-style adaptive resizing (ImgResize = 0). This example reduces the target short-side length for faster processing or different detection characteristics. ```csharp var options = RapidOcrOptions.Default with { ImgResize = 0, LimitSideLen = 512 // Smaller upscaling for speed }; ``` -------------------------------- ### Customize RapidOcrOptions with 'with' expressions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/types.md Immutable configuration record for the OCR pipeline. Uses init-only properties; customize via with-expressions. Example shows overriding default padding, text score threshold, and disabling angle detection. ```csharp var opts = RapidOcrOptions.Default with { ReturnWordBox = true, TextScore = 0.7f, DoAngle = false }; ``` -------------------------------- ### Exception Safety During `Detect()` Operation Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md This example shows how to handle exceptions that might occur during the `Detect()` method. The `using` statement ensures that temporary resources are cleaned up before the exception is propagated. ```csharp using var ocr = new RapidOcr(); ocr.InitModels(); try { // If an exception occurs here, temp resources are released var result = ocr.Detect("image.png", RapidOcrOptions.Default); } catch (Exception ex) { // Safe: cleanup already happened Console.WriteLine($"OCR failed: {ex.Message}"); } ``` -------------------------------- ### Draw Bounding Boxes on Image Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/OcrResult.md Demonstrates how to draw bounding boxes around detected text blocks onto the original image using SkiaSharp. Ensure SkiaSharp is installed and the image file exists. ```csharp using var bmp = SKBitmap.Decode("image.png"); var result = ocr.Detect(bmp, RapidOcrOptions.Default); using var canvas = new SKCanvas(bmp); using var paint = new SKPaint { Color = SKColors.Red, IsStroke = true, StrokeWidth = 2 }; foreach (var block in result.TextBlocks) { var pts = block.BoxPoints; canvas.DrawLine(pts[0], pts[1], paint); canvas.DrawLine(pts[1], pts[2], paint); canvas.DrawLine(pts[2], pts[3], paint); canvas.DrawLine(pts[3], pts[0], paint); } using var fs = File.Create("annotated.png"); bmp.Encode(fs, SKEncodedImageFormat.Png, 100); ``` -------------------------------- ### Iterating Through Detected Boxes and Scores Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/OcrResult.md Example demonstrating how to iterate through detected boxes and access their scores and corner points. This snippet is useful for processing the output of the DetectBoxes method. ```csharp var boxes = ocr.DetectBoxes("image.png", RapidOcrOptions.Default); foreach (var box in boxes) { Console.WriteLine($"Detection score: {box.Score:F2}"); for (int i = 0; i < box.BoxPoints.Length; i++) { var pt = box.BoxPoints[i]; Console.WriteLine($" Point {i}: ({pt.X}, {pt.Y})"); } } ``` -------------------------------- ### Get Word-Level Boxes for Layout Extraction Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/INDEX.md Configure RapidOcrOptions to return word-level bounding boxes for detailed layout analysis. Iterate through detected text blocks and their associated words. ```csharp var options = RapidOcrOptions.Default with { ReturnWordBox = true, ReturnSingleCharBox = true }; var result = ocr.Detect("image.png", options); foreach (var block in result.TextBlocks) { Console.WriteLine($ ``` -------------------------------- ### Detecting Text Blocks and Word Boxes Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/OcrResult.md Example of how to configure OCR options to return word boxes and iterate through the results to print detected text lines and their associated word boxes. ```csharp var options = RapidOcrOptions.Default with { ReturnWordBox = true }; var result = ocr.Detect("image.png", options); foreach (var block in result.TextBlocks) { Console.WriteLine($"Line: {block.Text}"); foreach (var word in block.WordResults ?? Array.Empty()) { Console.WriteLine($" Word: '{word.Text}' at ({word.BoxPoints[0].X}, {word.BoxPoints[0].Y}), score: {word.Score:F2}"); } } ``` -------------------------------- ### Access OcrResult properties Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/types.md Complete output from RapidOcr.Detect(): all detected and recognized text lines plus timing. Example shows how to access the number of text blocks, the full recognized text, and the total pipeline timing. ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.Default); Console.WriteLine($"Found {result.TextBlocks.Length} text lines"); Console.WriteLine($"Full text:\n{result.StrRes}"); Console.WriteLine($"Timing: {result.DetectTime:F1}ms total"); ``` -------------------------------- ### Setting MaxSideLen for Input Image Size Control Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Example of setting the 'MaxSideLen' property to control the maximum longer side length of the input image. This helps prevent Out-of-Memory errors with very large images by applying an upper bound before other resizing operations. ```csharp var options = RapidOcrOptions.Default with { MaxSideLen = 1024 }; ``` -------------------------------- ### Use Default Configuration Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md Initialize options using the default preset, which is optimized for photos and screenshots with bundled PP-OCRv5 Latin models. ```csharp var options = RapidOcrOptions.Default; ``` -------------------------------- ### Basic RapidOcr Usage Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Demonstrates the fundamental steps to initialize RapidOcr, detect text in an image, and process the results, including full text and per-line breakdowns. ```csharp using var ocr = new RapidOcr(); oc.InitModels(); var result = ocr.Detect("image.png", RapidOcrOptions.Default); Console.WriteLine("Full text:"); Console.WriteLine(result.StrRes); Console.WriteLine("\nPer-line breakdown:"); foreach (var block in result.TextBlocks) { Console.WriteLine($" {block.Text} (score: {block.BoxScore:F2})"); if (block.CharScores != null) { Console.WriteLine($" Char scores: {string.Join(", ", block.CharScores.Select(s => s.ToString("F2")))}"); } } ``` -------------------------------- ### RapidOcrNet Namespace Import Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/types.md Demonstrates how to import the RapidOcrNet namespace and initialize the OCR engine. ```csharp using RapidOcrNet; var ocr = new RapidOcr(); ocr.InitModels(); var result = ocr.Detect("image.png", RapidOcrOptions.Default); var textBlocks = result.TextBlocks; // TextBlock[] ``` -------------------------------- ### Iterating Through TextBlocks Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/OcrResult.md Example of how to iterate through the TextBlocks in an OcrResult to display recognized text, bounding box scores, angle classification, character scores, and timing. ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.Default); foreach (var block in result.TextBlocks) { Console.WriteLine($ ``` -------------------------------- ### Initializing Models with Default Paths Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md Initialize models using bundled resources with default paths relative to the binary. This is the simplest way to load models if they are correctly bundled. ```csharp // Option 1: Use bundled models with default paths ocr.InitModels(); // Loads from models/v5/ relative to binary ``` -------------------------------- ### Get Default Session Options Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Creates a preconfigured SessionOptions object with default graph optimization and thread settings. This object can be modified before initializing models. ```csharp public static SessionOptions GetDefaultSessionOptions(int numThread = 0) ``` ```csharp using var opts = RapidOcr.GetDefaultSessionOptions(4); // Optionally add GPU provider try { opts.AppendExecutionProvider_CUDA(); } catch { opts.AppendExecutionProvider_CPU(); } ocr.InitModels(opts); ``` -------------------------------- ### Checking Angle Classification Results Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/OcrResult.md Example showing how to check the angle classification results for text blocks. This is useful for identifying and potentially correcting upside-down text. ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.Default); foreach (var block in result.TextBlocks) { if (block.AngleIndex == 1) { Console.WriteLine($"Block was upside-down (confidence: {block.AngleScore:F2})"); } } ``` -------------------------------- ### Use Python Compatibility Configuration Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md Initialize options using the PythonCompat preset to reproduce results from Python rapidocr or test alternative models. ```csharp var options = RapidOcrOptions.PythonCompat; ``` -------------------------------- ### Best Practice: Using `using` for IDisposable Objects Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md Demonstrates the recommended practice of using `using` statements for all `IDisposable` objects, including `RapidOcr` and `SessionOptions`, to ensure proper resource management. ```csharp using var ocr = new RapidOcr(); using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); // ... ``` -------------------------------- ### Initialize RapidOcr with Default Models and Custom SessionOptions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Loads bundled PP-OCRv5 Latin models using a custom SessionOptions object, allowing for configurations like GPU execution. ```csharp using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); sessionOptions.AppendExecutionProvider_CUDA(); ocr.InitModels(sessionOptions); ``` -------------------------------- ### Parse Multi-line String Results Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/OcrResult.md Illustrates how to parse the `StrRes` property, which contains text from all blocks as a single multi-line string. The example splits the string into individual lines for processing. ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.Default); // result.StrRes might be: "Hello world\nSecond line\nThird line\n" // Parse per line var lines = result.StrRes.Split('\n', StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { Console.WriteLine($"Line: {line}"); } ``` -------------------------------- ### Initialize and run basic OCR with RapidOcr Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/INDEX.md Perform basic OCR by initializing the RapidOcr library with bundled models and then detecting text from an image. The full recognized text is output to the console. ```csharp using var ocr = new RapidOcr(); ocr.InitModels(); // Load bundled models var result = ocr.Detect("image.png", RapidOcrOptions.Default); Console.WriteLine(result.StrRes); // Full text ``` -------------------------------- ### Initializing Models with Specific Paths Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md Initialize models by providing absolute paths to the detector, classifier, recognizer ONNX files, and the dictionary text file. Ensure these paths are correct. ```csharp // Option 2: Specify absolute paths ocr.InitModels( detPath: "/full/path/to/det.onnx", clsPath: "/full/path/to/cls.onnx", recPath: "/full/path/to/rec.onnx", keysPath: "/full/path/to/dict.txt" ); ``` -------------------------------- ### Customizing Padding in RapidOcrOptions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Example of modifying the 'Padding' property of RapidOcrOptions to adjust the white border added to the image. Reducing padding can be useful for specific image content or when text is not close to the edges. ```csharp var options = RapidOcrOptions.Default with { Padding = 30 }; // Reduce border ``` -------------------------------- ### Initialize OCR with Custom Model Paths Source: https://github.com/bobld/rapidocrnet/blob/master/README.md Load custom ONNX models and dictionary files for specific languages or improved recognition. Ensure the dictionary file matches the recognizer model. ```csharp using var ocr = new RapidOcr(); ocr.InitModels( detPath: "models/v5/ch_PP-OCRv5_det_server.onnx", clsPath: "models/v5/ch_ppocr_mobile_v2.0_cls_infer.onnx", recPath: "models/v5/korean_PP-OCRv5_rec_mobile.onnx", keysPath: "models/v5/ppocrv5_korean_dict.txt"); // Or, with custom session options: // ocr.InitModels(detPath, clsPath, recPath, keysPath, sessionOptions); ``` -------------------------------- ### RapidOcr with GPU Acceleration (CUDA) Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Configures RapidOcr to use GPU acceleration via CUDA if available, falling back to CPU otherwise. Ensure CUDA toolkit and compatible drivers are installed. ```csharp using var sessionOptions = RapidOcr.GetDefaultSessionOptions(numThread: 0); try { sessionOptions.AppendExecutionProvider_CUDA(); } catch { sessionOptions.AppendExecutionProvider_CPU(); } using var ocr = new RapidOcr(); ocr.InitModels(sessionOptions); var result = ocr.Detect("image.png", RapidOcrOptions.Default); ``` -------------------------------- ### Detect (from file path) Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Loads an image file from disk and runs the complete three-stage pipeline (detection → classification → recognition). ```APIDOC ## Detect (from file path) ### Description Loads an image file from disk and runs the complete three-stage pipeline (detection → classification → recognition). The file is decoded into an SKBitmap and passed to the bitmap overload. ### Method ```csharp public OcrResult Detect(string path, RapidOcrOptions options) ``` ### Parameters #### Path Parameters - **path** (string) - Required - File path to image (PNG, JPEG, etc.). - **options** (RapidOcrOptions) - Required - Configuration for detection, classification, and recognition. ### Returns `OcrResult` containing detected text blocks, character scores, optional word boxes, and timing metrics. ### Throws `FileNotFoundException` — if the image file does not exist. ### Example ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.Default); Console.WriteLine(result.StrRes); // Full recognized text ``` ``` -------------------------------- ### Initialize RapidOcr with Default Models and Thread Count Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Loads bundled PP-OCRv5 Latin models using default paths and configures the number of ONNX Runtime threads. A value of 0 uses the system default. ```csharp using var ocr = new RapidOcr(); ocr.InitModels(numThread: 4); // Use 4 threads ``` -------------------------------- ### Using PythonCompat RapidOcrOptions for OCR Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Illustrates how to use the PythonCompat preset for OCR detection. This preset ensures compatibility with the Python rapidocr reference pipeline, offering specific resizing and padding configurations. ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.PythonCompat); ``` -------------------------------- ### Initialize RapidOcr with Custom Models and Custom SessionOptions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Loads custom ONNX models using specified paths and a custom SessionOptions object for advanced ONNX Runtime configuration. ```csharp using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); sessionOptions.AppendExecutionProvider_CUDA(); ocr.InitModels("det.onnx", "cls.onnx", "rec.onnx", "dict.txt", sessionOptions); ``` -------------------------------- ### InitModels() - Default models with custom SessionOptions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Loads bundled PP-OCRv5 Latin models using a custom SessionOptions object. Use this to enable GPU execution or other runtime settings. ```APIDOC ## InitModels() ### Description Loads bundled PP-OCRv5 Latin models using a custom `SessionOptions` object. Use this to enable GPU execution or other runtime settings. ### Method ```csharp public void InitModels(SessionOptions op) ``` ### Parameters #### Path Parameters - **op** (SessionOptions) - Required - ONNX Runtime session configuration (e.g., with GPU provider). ### Request Example ```csharp using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); sessionOptions.AppendExecutionProvider_CUDA(); ocr.InitModels(sessionOptions); ``` ``` -------------------------------- ### Initialize TextDetector with SessionOptions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/TextDetector.md Loads the detector ONNX model using a specified file path and ONNX Runtime SessionOptions. Throws FileNotFoundException if the model file does not exist. ```csharp public void InitModel(string path, SessionOptions op) ``` -------------------------------- ### Using Default RapidOcrOptions for OCR Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Shows the basic usage of the default OCR options with the Detect method. The Default preset is tuned for bundled PP-OCRv5 Latin ONNX models and includes specific preprocessing steps like padding and angle classification. ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.Default); ``` -------------------------------- ### RapidOcr with Custom Models and Languages Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Initializes RapidOcr using custom model files (detection, classification, recognition) and a custom dictionary for specific languages or improved accuracy. ```csharp using var ocr = new RapidOcr(); ocr.InitModels( detPath: "models/chinese_det.onnx", clsPath: "models/chinese_cls.onnx", recPath: "models/chinese_rec.onnx", keysPath: "models/chinese_dict.txt" ); var result = ocr.Detect("chinese_image.png", RapidOcrOptions.Default); ``` -------------------------------- ### Use GPU Acceleration Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/INDEX.md Initialize the OCR session with CUDA execution provider for GPU acceleration. Fallback to CPU if CUDA is not available. ```csharp using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); try { sessionOptions.AppendExecutionProvider_CUDA(); } catch { sessionOptions.AppendExecutionProvider_CPU(); } using var ocr = new RapidOcr(); ocr.InitModels(sessionOptions); ``` -------------------------------- ### Initialize RapidOcr with Custom Models and Thread Count Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Loads custom ONNX models specified by their paths and configures the number of ONNX Runtime threads. This overload is useful for supporting different languages or model variants. Throws FileNotFoundException if any model or keys file is missing. ```csharp ocr.InitModels( detPath: "models/v5/ch_PP-OCRv5_det_server.onnx", clsPath: "models/v5/ch_ppocr_mobile_v2.0_cls_infer.onnx", recPath: "models/v5/korean_PP-OCRv5_rec_mobile.onnx", keysPath: "models/v5/ppocrv5_korean_dict.txt", numThread: 0 ); ``` -------------------------------- ### Testing Configuration on a Specific Image Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md After adjusting options, test the configuration on a specific image file to evaluate its effectiveness. This step is crucial for iterative tuning. ```csharp var result = ocr.Detect("test.png", options); // Evaluate: does it now catch the missing text without introducing new errors? ``` -------------------------------- ### Proper Resource Management with IDisposable Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md RapidOcr and SessionOptions implement IDisposable and should be disposed to release unmanaged resources and prevent memory leaks. Use 'using' statements for automatic disposal. ```csharp using var ocr = new RapidOcr(); using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); sessionOptions.AppendExecutionProvider_CUDA(); try { ocr.InitModels(sessionOptions); var result = ocr.Detect("image.png", RapidOcrOptions.Default); } finally { // Disposed automatically at end of using blocks } ``` -------------------------------- ### Handle FileNotFoundException During Model Initialization Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md A general approach to handle FileNotFoundException during model initialization, logging the error and exiting. ```csharp try { ocr.InitModels(detPath, clsPath, recPath, keysPath); } catch (FileNotFoundException ex) { Console.Error.WriteLine("Configuration error: Model files not found."); Console.Error.WriteLine($"Details: {ex.Message}"); Environment.Exit(1); } ``` -------------------------------- ### Initialize TextDetector with Thread Count Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/TextDetector.md Loads the detector ONNX model using a specified file path and thread count. The thread count is passed to RapidOcr.GetDefaultSessionOptions(). ```csharp public void InitModel(string path, int numThread) ``` -------------------------------- ### Initialize OCR with GPU Acceleration Source: https://github.com/bobld/rapidocrnet/blob/master/README.md Configure RapidOCR to use a CUDA-enabled GPU via ONNX Runtime for faster processing. Falls back to CPU if CUDA is unavailable. ```csharp using var ocr = new RapidOcr(); using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); // sensible defaults: ORT_ENABLE_EXTENDED try { sessionOptions.AppendExecutionProvider_CUDA(); } // NVIDIA GPU catch { sessionOptions.AppendExecutionProvider_CPU(); } // fallback ocr.InitModels(sessionOptions); ``` -------------------------------- ### Initialize TextRecognizer Model with SessionOptions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/TextRecognizer.md Loads the CRNN ONNX model and character dictionary using provided SessionOptions. Ensure the model and keys files exist. ```csharp public void InitModel(string path, string keysPath, SessionOptions op) ``` -------------------------------- ### Configure Image Resizing for Speed and Compatibility Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md Adjust ImgResize to control detector input size. Set to a specific value (e.g., 512) for speed-focused processing, or 0 for Python-compatible adaptive resizing. ```csharp // Speed-focused: smaller input var fast = RapidOcrOptions.Default with { ImgResize = 512 }; // Python-compatible: adaptive resize var compatible = RapidOcrOptions.Default with { ImgResize = 0 }; ``` -------------------------------- ### InitModels() - Custom models with custom SessionOptions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Loads custom ONNX models using a custom SessionOptions object. Use this to enable GPU execution or other runtime settings. ```APIDOC ## InitModels() ### Description Loads custom ONNX models using a custom `SessionOptions` object. Use this to enable GPU execution or other runtime settings. ### Method ```csharp public void InitModels(string detPath, string clsPath, string recPath, string keysPath, SessionOptions op) ``` ### Parameters #### Path Parameters - **detPath** (string) - Required - Path to detector ONNX model. - **clsPath** (string) - Required - Path to classifier ONNX model. - **recPath** (string) - Required - Path to recognizer ONNX model. - **keysPath** (string) - Required - Path to character dictionary file. - **op** (SessionOptions) - Required - Custom ONNX Runtime configuration. ### Request Example ```csharp using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); sessionOptions.AppendExecutionProvider_CUDA(); ocr.InitModels("det.onnx", "cls.onnx", "rec.onnx", "dict.txt", sessionOptions); ``` ``` -------------------------------- ### InitModels() - Custom models with default thread count Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Load custom ONNX models, typically for different languages (Chinese, Japanese, Korean, etc.) or model variants (server vs. mobile). Throws FileNotFoundException if any model or keys file does not exist. ```APIDOC ## InitModels() ### Description Load custom ONNX models, typically for different languages (Chinese, Japanese, Korean, etc.) or model variants (server vs. mobile). ### Method ```csharp public void InitModels(string detPath, string clsPath, string recPath, string keysPath, int numThread = 0) ``` ### Parameters #### Path Parameters - **detPath** (string) - Required - Path to detector ONNX model (DBNet). - **clsPath** (string) - Required - Path to classifier ONNX model (rotation detection). - **recPath** (string) - Required - Path to recognizer ONNX model (CRNN). - **keysPath** (string) - Required - Path to character dictionary file (*.txt, one character per line). - **numThread** (int) - Optional - Number of threads for ONNX Runtime. 0 uses system default. ### Throws `FileNotFoundException` — if any model file or keys file does not exist. ### Request Example ```csharp ocr.InitModels( detPath: "models/v5/ch_PP-OCRv5_det_server.onnx", clsPath: "models/v5/ch_ppocr_mobile_v2.0_cls_infer.onnx", recPath: "models/v5/korean_PP-OCRv5_rec_mobile.onnx", keysPath: "models/v5/ppocrv5_korean_dict.txt", numThread: 0 ); ``` ``` -------------------------------- ### Python Compatibility Configuration Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md Use the PythonCompat preset for validating against Python rapidocr or testing alternative models. This ensures consistency between different language implementations. ```csharp var options = RapidOcrOptions.PythonCompat; var result = ocr.Detect("image.png", options); ``` -------------------------------- ### InitModels() - Default models with default thread count Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Loads bundled PP-OCRv5 Latin models from the default models/v5/ directory with extended graph optimization. Thread counts apply to both inter- and intra-operation parallelism. ```APIDOC ## InitModels() ### Description Loads bundled PP-OCRv5 Latin models from the default `models/v5/` directory with extended graph optimization. Thread counts apply to both inter- and intra-operation parallelism. ### Method ```csharp public void InitModels(int numThread = 0) ``` ### Parameters #### Path Parameters - **numThread** (int) - Optional - Number of threads for ONNX Runtime. 0 uses system default. ### Request Example ```csharp using var ocr = new RapidOcr(); ocr.InitModels(numThread: 4); // Use 4 threads ``` ``` -------------------------------- ### Performing OCR Detection with Options Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md Execute the OCR detection process by passing the configured RapidOcrOptions object to the Detect method. This is the core runtime configuration step. ```csharp // Everything happens here: var result = ocr.Detect("image.png", myOptions); ``` -------------------------------- ### Use Python-Compatible Options Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/INDEX.md Utilize the PythonCompat option for RapidOcrOptions to ensure compatibility with Python-based OCR pipelines. This sets predefined options suitable for Python integration. ```csharp var options = RapidOcrOptions.PythonCompat; var result = ocr.Detect("image.png", options); // Matches Python rapidocr reference pipeline ``` -------------------------------- ### Configure Polygon Expansion Ratio with UnClipRatio Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md Control the expansion ratio for detected contours to better encompass text width. Higher values create larger boxes, while lower values create tighter ones. ```csharp public float UnClipRatio { get; init; } // Default: 1.6 ``` ```csharp // Expand more to ensure full text width var options = RapidOcrOptions.Default with { UnClipRatio = 1.8f }; ``` -------------------------------- ### RapidOcr Constructor Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Initializes a new instance of the RapidOcr class. Ensure InitModels() is called before Detect() or DetectBoxes(). ```csharp public RapidOcr() ``` -------------------------------- ### Handle FileNotFoundException for Missing Keys File Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md Catch FileNotFoundException when the character dictionary file is missing during initialization. ```csharp try { ocr.InitModels("det.onnx", "cls.onnx", "rec.onnx", "missing_dict.txt"); } catch (FileNotFoundException ex) { Console.WriteLine($"Error: {ex.Message}"); // Output: "Recognizer keys file does not exist: 'missing_dict.txt'." } ``` -------------------------------- ### Load Custom Models for a Different Language Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/INDEX.md Initialize the OCR engine with paths to custom ONNX models and dictionary files for a specific language. This allows for language-specific OCR processing. ```csharp using var ocr = new RapidOcr(); ocr.InitModels( detPath: "models/v5/ch_PP-OCRv5_mobile_det.onnx", clsPath: "models/v5/ch_ppocr_mobile_v2.0_cls_infer.onnx", recPath: "models/v5/chinese_PP-OCRv5_rec_mobile.onnx", keysPath: "models/v5/ppocrv5_chinese_dict.txt" ); var result = ocr.Detect("image.png", RapidOcrOptions.Default); ``` -------------------------------- ### Configure ClsPreserveAspectRatio for OCR Preprocessing Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Set ClsPreserveAspectRatio to true to resize crops preserving aspect ratio, or false to stretch non-uniformly. This affects classifier preprocessing. ```csharp public bool ClsPreserveAspectRatio { get; init; } ``` ```csharp // Use Python-style aspect-preserving preprocessing var options = RapidOcrOptions.Default; // Use legacy stretch preprocessing var options2 = RapidOcrOptions.Default with { ClsPreserveAspectRatio = false }; ``` -------------------------------- ### Customize RapidOcrOptions with 'with' expressions Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/INDEX.md Customize RapidOcrNet options using 'with' expressions for a concise and immutable configuration approach. This is the primary method for tuning OCR behavior. ```csharp var options = RapidOcrOptions.Default with { ReturnWordBox = true, TextScore = 0.7f }; ``` -------------------------------- ### Handle CUDA Provider Not Found Exception Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md This snippet demonstrates how to safely attempt to append the CUDA execution provider and fall back to the CPU if it's not available. This is crucial for ensuring the OCR process can run even without GPU support. ```csharp using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); try { sessionOptions.AppendExecutionProvider_CUDA(); Console.WriteLine("CUDA provider added."); } catch (OnnxRuntimeException ex) { Console.WriteLine("CUDA not available, falling back to CPU."); sessionOptions.AppendExecutionProvider_CPU(); } ocr.InitModels(sessionOptions); ``` -------------------------------- ### DetectBoxes (from file path) Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Runs only the DBNet detector, skipping classification and recognition. Useful for layout analysis. ```APIDOC ## DetectBoxes (from file path) ### Description Run only the DBNet detector, skip classification and recognition. Useful for layout analysis or pre-filtering before deciding how to crop regions. Boxes are returned in source-image pixel coordinates. ### Method ```csharp public IReadOnlyList DetectBoxes(string path, RapidOcrOptions options) ``` ### Parameters #### Path Parameters - **path** (string) - Required - File path to image. - **options** (RapidOcrOptions) - Required - Detection options (TextScore, ReturnWordBox, ClsThresh are ignored). ### Returns `IReadOnlyList` — polygon boxes in source-image coordinates, sorted in reading order (top-to-bottom, left-to-right within lines). ### Throws `FileNotFoundException` — if the image file does not exist. ### Example ```csharp var boxes = ocr.DetectBoxes("image.png", RapidOcrOptions.Default); foreach (var box in boxes) { Console.WriteLine($"Box score: {box.Score:F2}, corners: {box.BoxPoints.Length}"); } ``` ``` -------------------------------- ### Handle FileNotFoundException for Missing Detector Model Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md Catch FileNotFoundException when the detector model file is missing during initialization. ```csharp try { ocr.InitModels("missing_detector.onnx", "classifier.onnx", "recognizer.onnx", "dict.txt"); } catch (FileNotFoundException ex) { Console.WriteLine($"Error: {ex.Message}"); // Output: "Detector model file does not exist: 'missing_detector.onnx'." } ``` -------------------------------- ### Setting MinSideLen for Input Image Size Control Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Demonstrates setting the 'MinSideLen' property to enforce a minimum shorter side length for the input image. This ensures sufficient vertical context for the detector, especially with images that might otherwise be scaled down too much. ```csharp var options = RapidOcrOptions.Default with { MinSideLen = 64 }; ``` -------------------------------- ### Detect Full Pipeline from File Path Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Loads an image from a file path and runs the complete OCR pipeline (detection, classification, recognition). Use this for direct file processing. ```csharp public OcrResult Detect(string path, RapidOcrOptions options) ``` ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.Default); Console.WriteLine(result.StrRes); // Full recognized text ``` -------------------------------- ### Customize Default Options Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md Create a custom options instance by modifying the default preset using with-expressions. This is useful for fine-tuning detection parameters. ```csharp var options = RapidOcrOptions.Default with { ReturnWordBox = true, TextScore = 0.7f, DoAngle = false }; var result = ocr.Detect("image.png", options); ``` -------------------------------- ### Integrate TextClassifier with RapidOcr Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/TextClassifier.md Shows how TextClassifier is instantiated within the RapidOcr class and how its GetAngles method is used during the detection process. It also illustrates the application of confidence thresholds for rotation. ```csharp public sealed class RapidOcr : IDisposable { private readonly TextClassifier _textClassifier = new TextClassifier(); // ... private OcrResult DetectOnce(..., bool doAngle, bool mostAngle, ...) { Angle[] angles = _textClassifier.GetAngles(partImages, doAngle, mostAngle, clsPreserveAspectRatio); // Apply rotations if confident enough... // Pass angles to TextBlock construction... } } ``` ```csharp for (int i = 0; i < partImages.Length; ++i) { if (angles[i].Index == 1 && angles[i].Score >= clsThresh) { // Rotate 180° partImages[i] = OcrUtils.BitmapRotateClockWise180(partImages[i]); } } ``` -------------------------------- ### Detect (from bitmap) Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcr.md Runs the full OCR pipeline on an in-memory bitmap. ```APIDOC ## Detect (from bitmap) ### Description Runs the full pipeline on an in-memory bitmap. Prefer this if you've already decoded the image or are processing from a stream. ### Method ```csharp public OcrResult Detect(SKBitmap originSrc, RapidOcrOptions options) ``` ### Parameters #### Path Parameters - **originSrc** (SKBitmap) - Required - Image already decoded as SkiaSharp bitmap. - **options** (RapidOcrOptions) - Required - Configuration for detection, classification, and recognition. ### Returns `OcrResult` containing detected text blocks, character scores, optional word boxes, and timing metrics. ### Example ```csharp using var bmp = SKBitmap.Decode("image.png"); var result = ocr.Detect(bmp, RapidOcrOptions.Default); foreach (var block in result.TextBlocks) { Console.WriteLine($"{block.Text} (confidence: {block.BoxScore:F2})"); } ``` ``` -------------------------------- ### Optimize Inference Speed with Input Size, Angle Detection, and GPU Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md This snippet shows multiple ways to speed up inference: reducing input size, disabling angle detection, using the GPU, and increasing thread count. Choose the options that best suit your needs. ```csharp // 1. Reduce input size var options = RapidOcrOptions.Default with { ImgResize = 512 }; // 2. Skip angle detection if not needed var options2 = RapidOcrOptions.Default with { DoAngle = false }; // 3. Use GPU using var sessionOptions = RapidOcr.GetDefaultSessionOptions(numThread: 0); try { sessionOptions.AppendExecutionProvider_CUDA(); } catch { sessionOptions.AppendExecutionProvider_CPU(); } ocr.InitModels(sessionOptions); // 4. Increase thread count ocr.InitModels(numThread: 4); // Use 4 threads ``` -------------------------------- ### Set Adaptive Resize Target with LimitSideLen Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/configuration.md When ImgResize is 0, LimitSideLen determines the target short-side length for adaptive resizing. Adjust this value to balance speed and accuracy for smaller input sizes. ```csharp var options = RapidOcrOptions.Default with { ImgResize = 0, // Use adaptive mode LimitSideLen = 512 // Smaller short side = faster }; ``` -------------------------------- ### RapidOcrOptions Configuration Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Configuration record for the OCR pipeline. Controls image preprocessing, detection thresholds, classifier behavior, and output granularity. ```APIDOC ## RapidOcrOptions Configuration record for the OCR pipeline. Controls image preprocessing, detection thresholds, classifier behavior, and output granularity. **Namespace:** `RapidOcrNet` **Source:** `RapidOcrNet/RapidOcrOptions.cs` **Type:** sealed record ## Overview `RapidOcrOptions` is a **record type** (immutable with init-only properties). Create instances using the static presets (`Default`, `PythonCompat`) and customize with `with`-expressions: ```csharp var options = RapidOcrOptions.Default with { ReturnWordBox = true, DoAngle = false, TextScore = 0.7f }; ``` ## Static Presets ### Default ```csharp public static readonly RapidOcrOptions Default ``` Tuned for the bundled **PP-OCRv5 Latin** ONNX models. Features: - 50-pixel white border on all sides (helps text-touching-edge cases) - Legacy max-side cap at 1024 pixels - Vertical letterbox for very wide inputs - Angle classification enabled with 0.9 confidence threshold - Per-character score threshold: 0.5 - Per-box score threshold: 0.5 **Use for:** Most photos and screenshots with the bundled model. **Example:** ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.Default); ``` ### PythonCompat ```csharp public static readonly RapidOcrOptions PythonCompat ``` Matches Python `rapidocr` reference pipeline exactly. Features: - No white border (Padding = 0) - Python-style adaptive short-side resize (LimitSideLen = 736) - Vertical letterbox with 8:1 width-to-height ratio - Same classifier and recognition thresholds - Useful for validation or very wide/tall inputs **Use for:** Reproducing Python results or testing alternative models. **Example:** ```csharp var result = ocr.Detect("image.png", RapidOcrOptions.PythonCompat); ``` ## Properties ### Image Preprocessing #### Padding ```csharp public int Padding { get; init; } ``` **Type:** int **Default:** 50 (Default preset) | 0 (PythonCompat preset) **Range:** ≥ 0 **Description:** Extra white border (in pixels) added on all four sides of the input image before detection. Helps when text runs close to image edges. Set to 0 to disable. **Example:** ```csharp var options = RapidOcrOptions.Default with { Padding = 30 }; // Reduce border ``` #### ImgResize ```csharp public int ImgResize { get; init; } ``` **Type:** int **Default:** 1024 (Default preset) | 0 (PythonCompat preset) **Description:** When **> 0**: Legacy max-side cap. Scales the image so the longer side is ≤ ImgResize, then rounds down to /32. Overrides Python-style adaptive resize. When **= 0**: Use Python-style adaptive short-side resize (see `LimitSideLen`). The short side is upscaled to reach the limit; images already at or above the limit stay at native size. **Use > 0 for:** Bundled PP-OCRv5 model (default behavior) **Use = 0 for:** Python-style behavior or alternative models **Example:** ```csharp // Legacy behavior: cap longer side at 1024 var opt1 = RapidOcrOptions.Default; // Python-style: upscale short side to 736 var opt2 = RapidOcrOptions.Default with { ImgResize = 0 }; ``` #### LimitSideLen ```csharp public int LimitSideLen { get; init; } ``` **Type:** int **Default:** 736 **Used when:** `ImgResize == 0` **Description:** Target short-side length for Python-style adaptive resize. Only active if `ImgResize` is 0. Matches Python's `Det.limit_side_len` (default 736). Images whose short side is already ≥ `LimitSideLen` are left at native size. Output dimensions are rounded to the nearest /32. **Example:** ```csharp var options = RapidOcrOptions.Default with { ImgResize = 0, LimitSideLen = 512 // Smaller upscaling for speed }; ``` #### MaxSideLen ```csharp public int MaxSideLen { get; init; } ``` **Type:** int **Default:** 2000 **Description:** Upper bound on the longer side of the source image (Python `max_side_len`). Applied before other resizing. Helps prevent OOM on huge inputs. **Example:** ```csharp var options = RapidOcrOptions.Default with { MaxSideLen = 1024 }; ``` #### MinSideLen ```csharp public int MinSideLen { get; init; } ``` **Type:** int **Default:** 30 **Description:** Lower bound on the shorter side of the source image (Python `min_side_len`). Applied before other resizing. Ensures the detector has enough vertical context. **Example:** ```csharp var options = RapidOcrOptions.Default with { MinSideLen = 64 }; ``` ``` -------------------------------- ### Handle FileNotFoundException for Nonexistent Image File Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md Catch FileNotFoundException when the input image file does not exist. ```csharp try { var result = ocr.Detect("nonexistent.png", RapidOcrOptions.Default); } catch (FileNotFoundException ex) { Console.WriteLine($"Error: {ex.Message}"); // Output: "Could not find image to process: 'nonexistent.png'." } ``` -------------------------------- ### Catch `FileNotFoundException` for Image Paths Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md Use a try-catch block to handle `FileNotFoundException` when calling `Detect()` with a user-provided image path. This provides a user-friendly message if the image does not exist. ```csharp string imagePath = GetImagePathFromUserInput(); try { var result = ocr.Detect(imagePath, RapidOcrOptions.Default); } catch (FileNotFoundException) { Console.WriteLine($"Image not found: {imagePath}"); } ``` -------------------------------- ### Validate Model Files Before Initialization Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md Check if all required model files exist before calling `InitModels`. This prevents `FileNotFoundException` errors during runtime. ```csharp string[] modelFiles = { detPath, clsPath, recPath, keysPath }; foreach (var file in modelFiles) { if (!File.Exists(file)) { throw new FileNotFoundException($"Missing model file: {file}"); } } ocr.InitModels(detPath, clsPath, recPath, keysPath); ``` -------------------------------- ### Configure UnClipRatio for Box Expansion Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/RapidOcrOptions.md Specifies the polygon expansion ratio applied to detected boxes to better fit the full text width. Higher values expand more. ```csharp public float UnClipRatio { get; init; } ``` ```csharp var options = RapidOcrOptions.Default with { UnClipRatio = 1.8f }; // Larger boxes ``` -------------------------------- ### Ensure Resource Cleanup with `using` Statement Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/errors.md Always use the `using` statement when instantiating `RapidOcr` to ensure that all managed and unmanaged resources are properly disposed of. This prevents memory leaks. ```csharp using var ocr = new RapidOcr(); ocr.InitModels(); try { var result = ocr.Detect("image.png", RapidOcrOptions.Default); } finally { // Cleanup happens automatically } ``` -------------------------------- ### Handle No Detections or Errors Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/INDEX.md Implement error handling for scenarios where no text is detected or file not found exceptions occur. Optionally, retry detection with relaxed thresholds. ```csharp try { var result = ocr.Detect("image.png", RapidOcrOptions.Default); if (result.TextBlocks.Length == 0) { Console.WriteLine("No text detected. Trying with relaxed thresholds..."); var relaxed = RapidOcrOptions.Default with { BoxScoreThresh = 0.3f }; result = ocr.Detect("image.png", relaxed); } Console.WriteLine(result.StrRes); } catch (FileNotFoundException) { Console.WriteLine("Image file not found."); } ``` -------------------------------- ### Instantiate TextDetector within RapidOcr Source: https://github.com/bobld/rapidocrnet/blob/master/_autodocs/api-reference/TextDetector.md Demonstrates the instantiation of TextDetector as a private member within the RapidOcr class, ensuring it's managed per RapidOcr instance. ```csharp public sealed class RapidOcr : IDisposable { private readonly TextDetector _textDetector = new TextDetector(); // ... public IReadOnlyList DetectBoxes(SKBitmap originSrc, RapidOcrOptions options) { using var input = PrepareDetectorInput(originSrc, options); var textBoxes = _textDetector.GetTextBoxes(input.Bitmap, input.Scale, ...); // Translate to source coordinates... return textBoxes; } } ```