xixioo2018 / PaddleSharp

.NET/C# binding for Baidu paddle inference library and PaddleOCR

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PaddleSharp

💗.NET Wrapper for PaddleInference C API, include PaddleOCR, support Windows(x64) and Linux(Ubuntu-20.04 x64).

NuGet Packages/Docker Images

NuGet Package Version Description
Sdcb.PaddleInference NuGet Paddle Inference C API .NET binding
Sdcb.PaddleOCR NuGet PaddleOCR library(based on Sdcb.PaddleInference)
Sdcb.PaddleOCR.KnownModels NuGet Helper to download PaddleOCR models
Sdcb.PaddleInference.runtime.win64.mkl NuGet Paddle Inference C API Windows x64(mkl-dnn) Native binding

Note: Linux does not need a native binding NuGet package like windows(Sdcb.PaddleInference.runtime.win64.mkl), instead, you can/should based from a Dockerfile to development:

Docker Images Version Description
sdflysha/ubuntu20-dotnet6-paddleocr2.2.1 Docker PaddleOCR 2.2.1, OpenCV 4.5.3, based on official Ubuntu 20.04 .NET 6 Runtime
sdflysha/ubuntu20-dotnet6sdk-paddleocr2.2.1 Docker PaddleOCR 2.2.1, OpenCV 4.5.3, based on official Ubuntu 20.04 .NET 6 SDK

Usage

Windows: Detection and Recognition(All)

  1. Pre-condition

Please ensure the latest Visual C++ Redistributable was installed in Windows(typically it should automatically installed if you have Visual Studio installed) Otherwise, it will failed with following error(Windows only):

DllNotFoundException: Unable to load DLL 'paddle_inference_c' or one of its dependencies (0x8007007E)
  1. Install NuGet Packages:
dotnet add package Sdcb.PaddleInference
dotnet add package Sdcb.PaddleInference.runtime.win64.mkl
dotnet add package Sdcb.PaddleOCR
dotnet add package Sdcb.PaddleOCR.KnownModels
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.runtime.win
  1. Using following C# code to get result:
OCRModel model = KnownOCRModel.PPOcrV2;
await model.EnsureAll();

byte[] sampleImageData;
string sampleImageUrl = @"https://www.tp-link.com.cn/content/images/detail/2164/TL-XDR5450易展Turbo版-3840px_03.jpg";
using (HttpClient http = new HttpClient())
{
    Console.WriteLine("Download sample image from: " + sampleImageUrl);
    sampleImageData = await http.GetByteArrayAsync(sampleImageUrl);
}

using (PaddleOcrAll all = new PaddleOcrAll(model.RootDirectory, model.KeyPath))
{
    // 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}");
        }
    }
}

Linux(Ubuntu 20.04): Detection and Recognition(All)

  1. Use sdflysha/ubuntu20-dotnet6-paddleocr2.2.1:20211223 to replace mcr.microsoft.com/dotnet/aspnet:6.0 in Dockerfile as docker base image.

The build steps for ubuntu20-dotnet6-paddleocr was described here.

And also, we also provided another dotnet6-sdk Dockerfile, described here.

  1. Install NuGet Packages:
dotnet add package Sdcb.PaddleInference
dotnet add package Sdcb.PaddleOCR
dotnet add package Sdcb.PaddleOCR.KnownModels
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.runtime.ubuntu.18.04-x64

Please aware in Linux, the native binding library is not required, instead, you should compile your own OpenCV/PaddleInference library, or just use the Docker image.

  1. write following C# code to get result(also can be exactly the same as windows):
OCRModel model = KnownOCRModel.PPOcrV2;
await model.EnsureAll();
using (PaddleOcrAll all = new PaddleOcrAll(model.RootDirectory, model.KeyPath))
// Load in-memory data by following code:
// using (Mat src = Cv2.ImDecode(sampleImageData, ImreadModes.Color))
using (Mat src = Cv2.ImRead(@"/app/test.jpg"))
{
    Console.WriteLine(all.Run(src).Text);
}

Detection Only

// Install following packages:
// Sdcb.PaddleInference
// Sdcb.PaddleInference.runtime.win64.mkl (required in Windows)
// Sdcb.PaddleOCR
// Sdcb.PaddleOCR.KnownModels
// OpenCvSharp4
// OpenCvSharp4.runtime.win (required in Windows)
// OpenCvSharp4.runtime.linux18.04 (required in Linux)
byte[] sampleImageData;
string sampleImageUrl = @"https://www.tp-link.com.cn/content/images/detail/2164/TL-XDR5450易展Turbo版-3840px_03.jpg";
using (HttpClient http = new HttpClient())
{
    Console.WriteLine("Download sample image from: " + sampleImageUrl);
    sampleImageData = await http.GetByteArrayAsync(sampleImageUrl);
}

OCRModel model = KnownOCRModel.PPOcrV2;
await model.EnsureAll();
using (PaddleOcrDetector detector = new PaddleOcrDetector(model.DetectionDirectory))
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");
        visualized.ImWrite(outputFile);
    }
}

About

.NET/C# binding for Baidu paddle inference library and PaddleOCR

License:Apache License 2.0


Languages

Language:C# 89.2%Language:Dockerfile 10.8%Language:Smalltalk 0.0%