### Install NuGet Packages Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddle2onnx.md Use these commands in the Package Manager Console to install the library and the required Windows runtime. ```powershell Install-Package Sdcb.Paddle2Onnx Install-Package Sdcb.Paddle2Onnx.runtime.win64 ``` -------------------------------- ### Install NuGet Packages for Non-Windows Platforms Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Platform-specific runtime packages for Linux and macOS. ```bash # For linux-x64 Install-Package Sdcb.PaddleInference.runtime.linux-x64.mkl # For linux-arm64 Install-Package Sdcb.PaddleInference.runtime.linux-arm64 # For macOS-x64 Install-Package Sdcb.PaddleInference.runtime.osx-x64 # For macOS-arm64(m1/m2/m3/m4 based) Install-Package Sdcb.PaddleInference.runtime.osx-arm64 ``` -------------------------------- ### Install NuGet Packages Source: https://github.com/sdcb/paddlesharp/blob/master/docs/rotation-detection.md Required NuGet packages for running the rotation detector on Windows. ```text Sdcb.PaddleInference.runtime.win64.mkl Sdcb.RotationDetector OpenCvSharp4.runtime.win ``` -------------------------------- ### Perform OCR with Online Models Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Example of downloading and initializing an online model for OCR. ```csharp FullOcrModel model = await OnlineFullModels.EnglishV3.DownloadAsync(); byte[] sampleImageData; string sampleImageUrl = @"https://www.tp-link.com.cn/content/images2017/gallery/4288_1920.jpg"; using (HttpClient http = new HttpClient()) { Console.WriteLine("Download sample image from: " + sampleImageUrl); sampleImageData = await http.GetByteArrayAsync(sampleImageUrl); } using (PaddleOcrAll all = new PaddleOcrAll(model, PaddleDevice.Mkldnn()) { AllowRotateDetection = true, /* 允许识别有角度的文字 */ Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */ }) { // Load local file by following code: // using (Mat src2 = Cv2.ImRead(@"C:\test.jpg")) using (Mat src = Cv2.ImDecode(sampleImageData, ImreadModes.Color)) { PaddleOcrResult result = all.Run(src); Console.WriteLine("Detected all texts: \n" + result.Text); foreach (PaddleOcrResultRegion region in result.Regions) { Console.WriteLine($"Text: {region.Text}, Score: {region.Score}, RectCenter: {region.Rect.Center}, RectSize: {region.Rect.Size}, Angle: {region.Rect.Angle}"); } } } ``` -------------------------------- ### Perform OCR with Local Models Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Example of initializing a local model and running detection on an image. ```csharp FullOcrModel model = LocalFullModels.ChineseV3; byte[] sampleImageData; string sampleImageUrl = @"https://www.tp-link.com.cn/content/images2017/gallery/4288_1920.jpg"; using (HttpClient http = new HttpClient()) { Console.WriteLine("Download sample image from: " + sampleImageUrl); sampleImageData = await http.GetByteArrayAsync(sampleImageUrl); } using (PaddleOcrAll all = new PaddleOcrAll(model, PaddleDevice.Mkldnn()) { AllowRotateDetection = true, /* 允许识别有角度的文字 */ Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */ }) { // Load local file by following code: // using (Mat src2 = Cv2.ImRead(@"C:\test.jpg")) using (Mat src = Cv2.ImDecode(sampleImageData, ImreadModes.Color)) { PaddleOcrResult result = all.Run(src); Console.WriteLine("Detected all texts: \n" + result.Text); foreach (PaddleOcrResultRegion region in result.Regions) { Console.WriteLine($"Text: {region.Text}, Score: {region.Score}, RectCenter: {region.Rect.Center}, RectSize: {region.Rect.Size}, Angle: {region.Rect.Angle}"); } } } ``` -------------------------------- ### Install NuGet Packages for Windows Online Models Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Required packages for running PaddleOCR with online models on Windows. ```text Sdcb.PaddleInference Sdcb.PaddleOCR Sdcb.PaddleOCR.Models.Online Sdcb.PaddleInference.runtime.win64.mkl OpenCvSharp4.runtime.win ``` -------------------------------- ### Initialize and Run PaddleDetector for Object Detection Source: https://context7.com/sdcb/paddlesharp/llms.txt Initializes the PaddleDetector with a model directory and configuration file for object detection. Demonstrates processing an image, filtering results by confidence, and visualizing detections. Includes an example for real-time detection from a webcam. ```csharp using Sdcb.PaddleInference; using Sdcb.PaddleDetection; using OpenCvSharp; string modelDir = "path/to/picodet_model"; string configPath = Path.Combine(modelDir, "infer_cfg.yml"); // Initialize detector with model directory and config using PaddleDetector detector = new PaddleDetector( modelDir, configPath, PaddleDevice.Mkldnn() ); // Process image using Mat src = Cv2.ImRead("scene.jpg"); DetectionResult[] results = detector.Run(src); // Filter and display results foreach (DetectionResult result in results.Where(r => r.Confidence > 0.5f)) { Console.WriteLine($"Class: {result.LabelName} (ID: {result.LabelId})"); Console.WriteLine($"Confidence: {result.Confidence:F2}"); Console.WriteLine($"Bounding Box: {result.Rect}"); } // Visualize detections on image using Mat visualized = PaddleDetector.Visualize( src, results.Where(r => r.Confidence > 0.5f), detector.Config.LabelList.Length ); Cv2.ImWrite("detections.jpg", visualized); // Real-time detection from webcam using VideoCapture capture = new VideoCapture(0); while (true) { using Mat frame = capture.RetrieveMat(); DetectionResult[] frameResults = detector.Run(frame); using Mat display = PaddleDetector.Visualize( frame, frameResults.Where(r => r.Confidence > 0.5f), detector.Config.LabelList.Length ); Cv2.ImShow("Detection", display); if (Cv2.WaitKey(1) == 27) break; // ESC to exit } ``` -------------------------------- ### Install NuGet Packages for Windows Local Models Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Required packages for running PaddleOCR with local models on Windows. ```text Sdcb.PaddleInference Sdcb.PaddleOCR Sdcb.PaddleOCR.Models.Local Sdcb.PaddleInference.runtime.win64.mkl OpenCvSharp4.runtime.win ``` -------------------------------- ### Import Required Namespaces Source: https://github.com/sdcb/paddlesharp/blob/master/src/Sdcb.PaddleSeg/paddleseg.md Imports necessary namespaces for PaddleSeg and image processing. Ensure Sdcb.PaddleSeg, Sdcb.PaddleInference, and OpenCvSharp4 NuGet packages are installed. ```csharp using OpenCvSharp; using Sdcb.PaddleInference; using Sdcb.PaddleSeg; ``` -------------------------------- ### C# Object Detection with PaddleDetector Source: https://github.com/sdcb/paddlesharp/blob/master/docs/detection.md This C# code snippet demonstrates how to perform real-time object detection using the PaddleDetector class. It initializes the detector with a model directory and configuration, captures frames from a webcam, runs detection, visualizes the results, and displays the output. Ensure you have the necessary models and NuGet packages installed. ```csharp string modelDir = DetectionLocalModel.PicoDets.L_416_coco.Directory; // your model directory here using (PaddleDetector detector = new PaddleDetector(modelDir, Path.Combine(modelDir, "infer_cfg.yml"), PaddleDevice.Mkldnn())) using (VideoCapture vc = new VideoCapture()) { vc.Open(0); while (true) { using (Mat mat = vc.RetrieveMat()) { DetectionResult[] results = detector.Run(mat); using (Mat dest = PaddleDetector.Visualize(mat, results.Where(x => x.Confidence > 0.5f), detector.Config.LabelList.Length)) { Cv2.ImShow("test", dest); } } Cv2.WaitKey(1); } } ``` -------------------------------- ### Register QueuedPaddleOcrAll in ASP.NET Core Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Register a singleton instance of QueuedPaddleOcrAll in your ASP.NET Core service builder. Configure the device (GPU or Mkldnn) based on application settings. This setup is for background processing. ```csharp builder.Services.AddSingleton(s => { Action device = builder.Configuration["PaddleDevice"] == "GPU" ? PaddleDevice.Gpu() : PaddleDevice.Mkldnn(); return new QueuedPaddleOcrAll(() => new PaddleOcrAll(LocalFullModels.ChineseV3, device) { Enable180Classification = true, AllowRotateDetection = true, }, consumerCount: 1); }); ``` -------------------------------- ### Use QueuedPaddleOcrAll in ASP.NET Core Controller Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Inject and use the registered QueuedPaddleOcrAll singleton in your ASP.NET Core controller for OCR processing. This example demonstrates reading an image from an IFormFile, processing it, and returning the OCR result with elapsed time. ```csharp public class OcrController : Controller { private readonly QueuedPaddleOcrAll _ocr; public OcrController(QueuedPaddleOcrAll ocr) { _ocr = ocr; } [Route("ocr")] public async Task Ocr(IFormFile file) { using MemoryStream ms = new(); using Stream stream = file.OpenReadStream(); stream.CopyTo(ms); using Mat src = Cv2.ImDecode(ms.ToArray(), ImreadModes.Color); double scale = 1; using Mat scaled = src.Resize(default, scale, scale); Stopwatch sw = Stopwatch.StartNew(); string textResult = (await _ocr.Run(scaled)).Text; sw.Stop(); return new OcrResponse(textResult, sw.ElapsedMilliseconds); } } ``` -------------------------------- ### Enable GPU Support in PaddleSharp Source: https://github.com/sdcb/paddlesharp/blob/master/README.md To enable GPU support, specify PaddleDevice.Gpu() in your paddle device configuration. Ensure you have installed the correct runtime package and necessary NVIDIA drivers and libraries. ```csharp PaddleDevice.Gpu() ``` -------------------------------- ### Perform Text Detection on an Image Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Use PaddleOcrDetector for text detection. Ensure necessary packages are installed and provide image data. The output is visualized and saved. ```csharp // Install following packages: // Sdcb.PaddleInference // Sdcb.PaddleOCR // Sdcb.PaddleOCR.Models.Local // Sdcb.PaddleInference.runtime.win64.mkl // OpenCvSharp4.runtime.win byte[] sampleImageData; string sampleImageUrl = @"https://www.tp-link.com.cn/content/images2017/gallery/4288_1920.jpg"; using (HttpClient http = new HttpClient()) { Console.WriteLine("Download sample image from: " + sampleImageUrl); sampleImageData = await http.GetByteArrayAsync(sampleImageUrl); } using (PaddleOcrDetector detector = new PaddleOcrDetector(LocalDetectionModel.ChineseV3, PaddleDevice.Mkldnn())) using (Mat src = Cv2.ImDecode(sampleImageData, ImreadModes.Color)) { RotatedRect[] rects = detector.Run(src); using (Mat visualized = PaddleOcrDetector.Visualize(src, rects, Scalar.Red, thickness: 2)) { string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "output.jpg"); Console.WriteLine("OutputFile: " + outputFile); visualized.ImWrite(outputFile); } } ``` -------------------------------- ### Configure Compute Backends with PaddleDevice Source: https://context7.com/sdcb/paddlesharp/llms.txt Configure inference backends including CPU, GPU, and ONNX Runtime. Allows for mixed-device configurations for different models. ```csharp using Sdcb.PaddleInference; using Sdcb.PaddleOCR; using Sdcb.PaddleOCR.Models.Local; // MKL-DNN CPU backend (recommended for most users) using PaddleOcrAll ocrMkldnn = new PaddleOcrAll( LocalFullModels.ChineseV3, PaddleDevice.Mkldnn(cacheCapacity: 10, cpuMathThreadCount: 4) ); // OpenBLAS CPU backend (lower memory usage) using PaddleOcrAll ocrBlas = new PaddleOcrAll( LocalFullModels.ChineseV3, PaddleDevice.Blas(cpuMathThreadCount: 4) ); // ONNX Runtime backend using PaddleOcrAll ocrOnnx = new PaddleOcrAll( LocalFullModels.ChineseV3, PaddleDevice.Onnx(enableOnnxOptimization: true) ); // GPU with CUDA using PaddleOcrAll ocrGpu = new PaddleOcrAll( LocalFullModels.ChineseV3, PaddleDevice.Gpu(initialMemoryMB: 500, deviceId: 0) ); // GPU with TensorRT acceleration using PaddleOcrAll ocrTensorRt = new PaddleOcrAll( LocalFullModels.ChineseV3, PaddleDevice.Gpu().And(PaddleDevice.TensorRt("det-shape.txt")), // Detection PaddleDevice.Gpu().And(PaddleDevice.TensorRt("cls-shape.txt")), // Classification PaddleDevice.Gpu().And(PaddleDevice.TensorRt("rec-shape.txt")) // Recognition ); // Different devices for different models using PaddleOcrAll ocrMixed = new PaddleOcrAll( LocalFullModels.ChineseV3, detectorDevice: PaddleDevice.Gpu(), classifierDevice: PaddleDevice.Mkldnn(), recognizerDevice: PaddleDevice.Gpu() ); // Platform-specific default Action defaultDevice = PaddleDevice.PlatformDefault; // Windows/Linux: MKLDNN, macOS x64: ONNX, macOS ARM64: BLAS ``` -------------------------------- ### Access Pre-trained OCR Models in C# Source: https://context7.com/sdcb/paddlesharp/llms.txt Demonstrates how to access various pre-trained OCR models for different languages using the LocalFullModels class. These models are bundled as local NuGet packages. ```csharp using Sdcb.PaddleOCR; using Sdcb.PaddleOCR.Models.Local; // Chinese OCR (Simplified) - V3, V4, V5 versions available FullOcrModel chineseV5 = LocalFullModels.ChineseV5; FullOcrModel chineseV4 = LocalFullModels.ChineseV4; FullOcrModel chineseV3 = LocalFullModels.ChineseV3; // Traditional Chinese FullOcrModel traditionalChinese = LocalFullModels.TraditionalChineseV3; // English FullOcrModel englishV4 = LocalFullModels.EnglishV4; FullOcrModel englishV3 = LocalFullModels.EnglishV3; // Asian languages FullOcrModel japanese = LocalFullModels.JapanV4; FullOcrModel korean = LocalFullModels.KoreanV4; // South Asian languages FullOcrModel devanagari = LocalFullModels.DevanagariV4; // Hindi, Sanskrit FullOcrModel tamil = LocalFullModels.TamilV4; FullOcrModel telugu = LocalFullModels.TeluguV4; FullOcrModel kannada = LocalFullModels.KannadaV4; // Middle Eastern and European FullOcrModel arabic = LocalFullModels.ArabicV4; FullOcrModel cyrillic = LocalFullModels.CyrillicV3; // Russian, Ukrainian FullOcrModel latin = LocalFullModels.LatinV3; // French, German, etc. // List all available models foreach (FullOcrModel model in LocalFullModels.All) { Console.WriteLine($"Detection: {model.DetectionModel.GetType().Name}"); Console.WriteLine($"Recognition: {model.RecognizationModel.GetType().Name}"); } // Use with PaddleOcrAll using PaddleOcrAll ocr = new PaddleOcrAll(LocalFullModels.JapanV4, PaddleDevice.Mkldnn()); ``` -------------------------------- ### Initialize and Run PaddleOcrRecognizer Source: https://context7.com/sdcb/paddlesharp/llms.txt Initializes the PaddleOcrRecognizer with a Chinese V3 model and MKLDNN device for text recognition. Demonstrates single image and batch recognition, including accessing character-level details. ```csharp using Sdcb.PaddleInference; using Sdcb.PaddleOCR; using Sdcb.PaddleOCR.Models.Local; using OpenCvSharp; // Initialize recognizer with Chinese V3 recognition model using PaddleOcrRecognizer recognizer = new PaddleOcrRecognizer( LocalRecognizationModel.ChineseV3, PaddleDevice.Mkldnn() ); // Single image recognition using Mat textImage = Cv2.ImRead("cropped_text.jpg"); PaddleOcrRecognizerResult singleResult = recognizer.Run(textImage); Console.WriteLine($"Text: {singleResult.Text}, Score: {singleResult.Score:F2}"); // Batch recognition for better throughput Mat[] textImages = new Mat[] { Cv2.ImRead("text1.jpg"), Cv2.ImRead("text2.jpg"), Cv2.ImRead("text3.jpg") }; PaddleOcrRecognizerResult[] batchResults = recognizer.Run(textImages, batchSize: 8); foreach (var result in batchResults) { Console.WriteLine($"Text: {result.Text}, Score: {result.Score:F2}"); // Access individual character details foreach (var ch in result.Chars) { Console.WriteLine($" Char: {ch.Character}, Score: {ch.Score:F2}"); } } // Cleanup foreach (Mat mat in textImages) mat.Dispose(); ``` -------------------------------- ### Initialize and Run PaddleOcrTableRecognizer Source: https://context7.com/sdcb/paddlesharp/llms.txt Initializes the PaddleOcrTableRecognizer for table structure recognition. Loads an image, detects the table structure, and then uses PaddleOcrAll to perform OCR on the image to rebuild an HTML table. ```csharp using Sdcb.PaddleInference; using Sdcb.PaddleOCR; using Sdcb.PaddleOCR.Models.Local; using OpenCvSharp; // Initialize table recognizer using PaddleOcrTableRecognizer tableRec = new PaddleOcrTableRecognizer( LocalTableRecognitionModel.ChineseMobileV2_SLANET ); // Load table image using Mat src = Cv2.ImRead("table.jpg"); // Detect table structure TableDetectionResult tableResult = tableRec.Run(src); Console.WriteLine($"Table detection score: {tableResult.Score:F2}"); Console.WriteLine($"Cell count: {tableResult.Boxes.Count}"); // Perform OCR on the same image for text content using PaddleOcrAll ocr = new PaddleOcrAll(LocalFullModels.ChineseV3); o cr.Detector.UnclipRatio = 1.2f; // Adjust for table detection PaddleOcrResult ocrResult = ocr.Run(src); // Rebuild complete HTML table with OCR text string htmlTable = tableResult.RebuildTable(ocrResult); Console.WriteLine(htmlTable); // Output example: // // // // //
NameAgeCity
John25New York
Jane30Los Angeles
``` -------------------------------- ### Specify Deployment Backend Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddle2onnx.md Set the deployment backend, such as TensorRT, during the conversion check. ```csharp bool can = Paddle2OnnxConverter.CanConvert(modelFile, paramsFile, deployBackend: "tensorrt"); ``` -------------------------------- ### Configure PaddleOCR with TensorRT for Multiple Models Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Initialize PaddleOCR using TensorRT for detection, classification, and recognition models. Specify separate shape info files for each model component. TensorRT requires a shape-info text file specific to each model. ```csharp using PaddleOcrAll all = new(model, PaddleDevice.Gpu().And(PaddleDevice.TensorRt("det.txt")), PaddleDevice.Gpu().And(PaddleDevice.TensorRt("cls.txt")), PaddleDevice.Gpu().And(PaddleDevice.TensorRt("rec.txt"))) { Enable180Classification = true, AllowRotateDetection = true, }; ``` -------------------------------- ### Create PaddleSeg Predictor Source: https://github.com/sdcb/paddlesharp/blob/master/src/Sdcb.PaddleSeg/paddleseg.md Initializes a PaddleSeg predictor with the loaded model and specifies the device for inference, such as MKLDNN acceleration. ```csharp PaddleSegPredictor paddleSegPredictor = new PaddleSegPredictor(segModel, PaddleDevice.Mkldnn()); ``` -------------------------------- ### Execute Full OCR Pipeline with PaddleOcrAll Source: https://context7.com/sdcb/paddlesharp/llms.txt Initializes a complete OCR pipeline using local models and processes an image to extract text and region metadata. ```csharp using Sdcb.PaddleInference; using Sdcb.PaddleOCR; using Sdcb.PaddleOCR.Models.Local; using OpenCvSharp; // Initialize with local Chinese V3 model using MKLDNN backend FullOcrModel model = LocalFullModels.ChineseV3; using PaddleOcrAll ocr = new PaddleOcrAll(model, PaddleDevice.Mkldnn()) { AllowRotateDetection = true, // Enable detection of rotated text Enable180Classification = false // Disable 180-degree text classification }; // Load and process image using Mat src = Cv2.ImRead("document.jpg"); PaddleOcrResult result = ocr.Run(src); // Output full extracted text Console.WriteLine($"Full Text:\n{result.Text}"); // Process individual regions with position information foreach (PaddleOcrResultRegion region in result.Regions) { Console.WriteLine($"Text: {region.Text}"); Console.WriteLine($"Score: {region.Score:F2}"); Console.WriteLine($"Position: {region.Rect.Center}, Size: {region.Rect.Size}"); Console.WriteLine($"Angle: {region.Rect.Angle}"); } ``` -------------------------------- ### Perform Table Recognition and Rebuild Table Source: https://github.com/sdcb/paddlesharp/blob/master/docs/ocr.md Utilize PaddleOcrTableRecognizer for table recognition and then rebuild the table structure using OCR results. Requires specific packages and local models. ```csharp // Install following packages: // Sdcb.PaddleInference // Sdcb.PaddleOCR // Sdcb.PaddleOCR.Models.Local // Sdcb.PaddleInference.runtime.win64.mkl // OpenCvSharp4.runtime.win using PaddleOcrTableRecognizer tableRec = new(LocalTableRecognitionModel.ChineseMobileV2_SLANET); using Mat src = Cv2.ImRead(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "table.jpg")); // Table detection TableDetectionResult tableResult = tableRec.Run(src); // Normal OCR using PaddleOcrAll all = new(LocalFullModels.ChineseV3); all.Detector.UnclipRatio = 1.2f; PaddleOcrResult ocrResult = all.Run(src); // Rebuild table string html = tableResult.RebuildTable(ocrResult); ``` -------------------------------- ### 自定义词库配置 Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddlenlp-lac.md 通过构造函数传入自定义词典,以调整分词和标注结果。 ```csharp string input = "我爱北京天安门"; using ChineseSegmenter segmenter = new(new () { CustomDictionary = new() { { "北京天安门", WordTag.LocationName }, } }); WordAndTag[] result = segmenter.Tagging(input); string labels = string.Join(",", result.Select(x => x.Label)); string words = string.Join(",", result.Select(x => x.Word)); string tags = string.Join(",", result.Select(x => x.Tag)); Console.WriteLine(words); // 我,爱,北京天安门 Console.WriteLine(labels); // r,v,LOC Console.WriteLine(tags); // Pronoun,Verb,LocationName ``` -------------------------------- ### Disable Mkldnn for AVX-Unsupported CPUs Source: https://github.com/sdcb/paddlesharp/blob/master/README.md If your CPU does not support AVX instructions, use the x64-noavx-openblas DLLs and disable Mkldnn by specifying PaddleDevice.Openblas(). ```csharp PaddleDevice.Openblas() ``` -------------------------------- ### Check Model Compatibility Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddle2onnx.md Verify if a PaddlePaddle model can be converted to ONNX using file paths or byte arrays. ```csharp string modelFile = "model.pdmodel"; string paramsFile = "model.pdiparams"; bool can = Paddle2OnnxConverter.CanConvert(modelFile, paramsFile); ``` -------------------------------- ### 中文词性标注 Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddlenlp-lac.md 获取分词结果及其对应的词性标签和描述。 ```csharp string input = "我爱北京天安门"; using ChineseSegmenter segmenter = new(); WordAndTag[] result = segmenter.Tagging(input); string labels = string.Join(",", result.Select(x => x.Label)); string words = string.Join(",", result.Select(x => x.Word)); string tags = string.Join(",", result.Select(x => x.Tag)); Console.WriteLine(words); // 我,爱,北京,天安门 Console.WriteLine(labels); // r,v,LOC,LOC Console.WriteLine(tags); // Pronoun,Verb,LocationName,LocationName ``` -------------------------------- ### Describe ONNX Model Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddle2onnx.md Retrieve metadata from an existing ONNX model byte array. ```csharp OnnxModelInfo info = Paddle2OnnxConverter.DescribeOnnxModel(onnxModel); Console.WriteLine("Input shapes:"); Console.WriteLine(string.Join("\n", info.Inputs.Select(x => x.ToString()))); Console.WriteLine(); Console.WriteLine("Output shapes:"); Console.WriteLine(string.Join("\n", info.Outputs.Select(x => x.ToString()))); ``` -------------------------------- ### ASP.NET Core Service Registration for OCR Source: https://context7.com/sdcb/paddlesharp/llms.txt Registers `QueuedPaddleOcrAll` as a singleton service in ASP.NET Core's dependency injection container. This wrapper manages concurrent OCR requests efficiently for web applications. ```csharp // Program.cs - Service registration using Sdcb.PaddleInference; using Sdcb.PaddleOCR; using Sdcb.PaddleOCR.Models.Local; var builder = WebApplication.CreateBuilder(args); // Register QueuedPaddleOcrAll as singleton builder.Services.AddSingleton(sp => { Action device = builder.Configuration["PaddleDevice"] == "GPU" ? PaddleDevice.Gpu() : PaddleDevice.Mkldnn(); return new QueuedPaddleOcrAll( () => new PaddleOcrAll(LocalFullModels.ChineseV3, device) { Enable180Classification = true, AllowRotateDetection = true }, consumerCount: 1 // Number of concurrent OCR workers ); }); // Controller usage [ApiController] [Route("api/[controller]")] public class OcrController : ControllerBase { private readonly QueuedPaddleOcrAll _ocr; public OcrController(QueuedPaddleOcrAll ocr) { _ocr = ocr; } [HttpPost("recognize")] public async Task Recognize(IFormFile file) { using MemoryStream ms = new(); await file.CopyToAsync(ms); using Mat src = Cv2.ImDecode(ms.ToArray(), ImreadModes.Color); Stopwatch sw = Stopwatch.StartNew(); PaddleOcrResult result = await _ocr.Run(src); sw.Stop(); return Ok(new { Text = result.Text, Regions = result.Regions.Select(r => new { r.Text, r.Score, Center = new { r.Rect.Center.X, r.Rect.Center.Y }, Size = new { r.Rect.Size.Width, r.Rect.Size.Height }, r.Rect.Angle }), ProcessingTimeMs = sw.ElapsedMilliseconds }); } } ``` -------------------------------- ### Load Image and Perform Segmentation Source: https://github.com/sdcb/paddlesharp/blob/master/src/Sdcb.PaddleSeg/paddleseg.md Loads an input image using OpenCvSharp and performs segmentation prediction using the PaddleSeg predictor. Ensure the input image path is valid. ```csharp // Load input image Mat img = new Mat(@"E:\gitproject\PaddleSeg\datasets\optic_disc_seg\JPEGImages\H0002.jpg"); // Perform prediction var result = paddleSegPredictor.Run(img); ``` -------------------------------- ### Describe PaddlePaddle Model Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddle2onnx.md Retrieve metadata such as input and output shapes from a PaddlePaddle model file. ```csharp byte[] modelBuffer = File.ReadAllBytes("model.pdmodel"); PaddleModelInfo info = Paddle2OnnxConverter.DescribePaddleModel(modelBuffer); Console.WriteLine($"Input shapes: {string.Join(", ", info.InputShapes)}"); Console.WriteLine($"Output shapes: {string.Join(", ", info.OutputShapes)}"); ``` -------------------------------- ### 基础中文分词 Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddlenlp-lac.md 使用 ChineseSegmenter 对输入文本进行基础分词处理。 ```csharp string input = "我是中国人,我爱我的祖国。"; using ChineseSegmenter segmenter = new(); string[] result = segmenter.Segment(input); Console.WriteLine(string.Join(",", result)); // 我,是,中国,人,,,我,爱,我的祖国,。 ``` -------------------------------- ### Load PaddleSeg Model Source: https://github.com/sdcb/paddlesharp/blob/master/src/Sdcb.PaddleSeg/paddleseg.md Loads a segmentation model from a specified directory. The path must point to a valid PaddleSeg exported model directory. ```csharp SegModel segModel = SegModel.FromDirectory(@"E:\gitproject\PaddleSeg\output\infer_model"); ``` -------------------------------- ### Convert Model to ONNX Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddle2onnx.md Perform the conversion of a PaddlePaddle model to an ONNX byte array. ```csharp byte[] modelBuffer = File.ReadAllBytes("model.pdmodel"); byte[] paramsBuffer = File.ReadAllBytes("model.pdiparams"); byte[] onnxModel = Paddle2OnnxConverter.ConvertToOnnx(modelBuffer, paramsBuffer); ``` -------------------------------- ### Detect Text Regions with PaddleOcrDetector Source: https://context7.com/sdcb/paddlesharp/llms.txt Configures and runs a text detector to identify and visualize bounding boxes for text areas in an image. ```csharp using Sdcb.PaddleInference; using Sdcb.PaddleOCR; using Sdcb.PaddleOCR.Models.Local; using OpenCvSharp; // Initialize detector with Chinese V3 detection model using PaddleOcrDetector detector = new PaddleOcrDetector( LocalDetectionModel.ChineseV3, PaddleDevice.Mkldnn() ); // Configure detection parameters detector.MaxSize = 960; // Max image dimension for detection detector.BoxScoreThreahold = 0.7f; // Minimum confidence threshold detector.BoxThreshold = 0.3f; // Binarization threshold detector.MinSize = 3; // Minimum box size detector.UnclipRatio = 1.5f; // Box expansion ratio using Mat src = Cv2.ImRead("document.jpg"); RotatedRect[] textRegions = detector.Run(src); Console.WriteLine($"Found {textRegions.Length} text regions"); // Visualize detected regions using Mat visualized = PaddleOcrDetector.Visualize(src, textRegions, Scalar.Red, thickness: 2); Cv2.ImWrite("detected_regions.jpg", visualized); foreach (RotatedRect rect in textRegions) { Console.WriteLine($"Center: {rect.Center}, Size: {rect.Size}, Angle: {rect.Angle}"); } ``` -------------------------------- ### Detect and Correct Image Rotation with PaddleRotationDetector Source: https://context7.com/sdcb/paddlesharp/llms.txt Use PaddleRotationDetector to identify text orientation in images and optionally restore them. Requires an initialized detector and an input image. ```csharp using Sdcb.PaddleInference; using Sdcb.RotationDetector; using OpenCvSharp; // Initialize with embedded default model using PaddleRotationDetector detector = new PaddleRotationDetector( RotationDetectionModel.EmbeddedDefault ); using Mat src = Cv2.ImRead("rotated_document.jpg"); // Detect rotation with default threshold RotationResult result = detector.Run(src, rotateThreshold: 0.50f); Console.WriteLine($"Detected Rotation: {result.Rotation}"); // _0, _90, _180, _270 Console.WriteLine($"Confidence: {result.Confidence:F2}"); // Correct the rotation if needed if (result.Rotation != RotationDegree._0) { result.RestoreRotationInPlace(src); Cv2.ImWrite("corrected_document.jpg", src); Console.WriteLine("Image rotation corrected and saved."); } // Batch processing multiple images string[] imagePaths = Directory.GetFiles("documents/", "*.jpg"); foreach (string path in imagePaths) { using Mat img = Cv2.ImRead(path); RotationResult rot = detector.Run(img); if (rot.Rotation != RotationDegree._0 && rot.Confidence > 0.8f) { rot.RestoreRotationInPlace(img); Cv2.ImWrite(path.Replace(".jpg", "_corrected.jpg"), img); } } ``` -------------------------------- ### Detect Image Rotation Source: https://github.com/sdcb/paddlesharp/blob/master/docs/rotation-detection.md Initialize the detector and process an image to retrieve its rotation angle. ```csharp using PaddleRotationDetector detector = new PaddleRotationDetector(RotationDetectionModel.EmbeddedDefault); using Mat src = Cv2.ImRead(@"C:\your-local-file-here.jpg"); RotationResult r = detector.Run(src); Console.WriteLine(r.Rotation); // _0, _90, _180, _270 // Restore to non-rotated: // r.RestoreRotationInPlace(src); ``` -------------------------------- ### Perform Chinese Word Segmentation with ChineseSegmenter Source: https://context7.com/sdcb/paddlesharp/llms.txt Segment Chinese text and perform part-of-speech tagging. Supports custom dictionaries and batch processing. ```csharp using Sdcb.PaddleNLP.Lac; // Basic word segmentation using ChineseSegmenter segmenter = new ChineseSegmenter(); string text = "我是中国人,我爱我的祖国。"; string[] words = segmenter.Segment(text); Console.WriteLine(string.Join(" | ", words)); // Output: 我 | 是 | 中国 | 人 | , | 我 | 爱 | 我的祖国 | 。 // Word segmentation with part-of-speech tagging string sentence = "我爱北京天安门"; WordAndTag[] tagged = segmenter.Tagging(sentence); foreach (WordAndTag item in tagged) { Console.WriteLine($"Word: {item.Word}, Label: {item.Label}, Tag: {item.Tag}"); } // Output: // Word: 我, Label: r, Tag: Pronoun // Word: 爱, Label: v, Tag: Verb // Word: 北京, Label: LOC, Tag: LocationName // Word: 天安门, Label: LOC, Tag: LocationName // Using custom dictionary using ChineseSegmenter customSegmenter = new ChineseSegmenter(new LacOptions { CustomDictionary = new Dictionary { { "北京天安门", WordTag.LocationName }, { "人工智能", WordTag.Noun } } }); WordAndTag[] customResult = customSegmenter.Tagging("我爱北京天安门"); Console.WriteLine(string.Join(", ", customResult.Select(x => x.Word))); // Output: 我, 爱, 北京天安门 // Batch processing string[] texts = new[] { "自然语言处理是人工智能的重要领域", "深度学习改变了计算机视觉" }; string[][] allSegments = segmenter.SegmentAll(texts); foreach (string[] segments in allSegments) { Console.WriteLine(string.Join(" | ", segments)); } ``` -------------------------------- ### Configure Custom Operators Source: https://github.com/sdcb/paddlesharp/blob/master/docs/paddle2onnx.md Pass custom operator mappings to the converter to handle specific model layers. ```csharp CustomOp[] ops = new[] { new CustomOp("fc", "FC"), new CustomOp("softmax", "Softmax") }; bool can = Paddle2OnnxConverter.CanConvert(modelFile, paramsFile, customOps: ops); ``` -------------------------------- ### Process Segmentation Results to Mask Source: https://github.com/sdcb/paddlesharp/blob/master/src/Sdcb.PaddleSeg/paddleseg.md Converts the segmentation prediction results into a binary mask image. White regions (255) indicate the target area, and black regions (0) indicate the background. The resulting mask is saved as 'mask.png'. ```csharp // Create single-channel mask image Mat mask = new Mat(img.Size(), MatType.CV_8UC1); // Convert segmentation result to mask int width = img.Width; int height = img.Height; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int index = y * width + x; if (index < result.Length) { // Convert 1 to 255, keep 0 as 0 mask.Set(y, x, result[index] == 1 ? (byte)255 : (byte)0); } } } // Save mask image Cv2.ImWrite("mask.png", mask); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.