sdcb / PaddleSharp

.NET/C# binding for Baidu paddle inference library and PaddleOCR
Apache License 2.0
1.05k stars 197 forks source link

cpu 100% #54

Closed bxjg1987 closed 1 year ago

bxjg1987 commented 1 year ago

nuget package

   <PackageReference Include="OpenCvSharp4.runtime.win" Version="4.7.0.20230115" />
    <PackageReference Include="Sdcb.PaddleInference" Version="2.4.1.2" />
    <PackageReference Include="Sdcb.PaddleInference.runtime.win64.mkl" Version="2.4.1" />
    <PackageReference Include="Sdcb.PaddleOCR" Version="2.6.0.4" />
    <PackageReference Include="Sdcb.PaddleOCR.Models.LocalV3" Version="2.6.0.3" />

test code

// See https://aka.ms/new-console-template for more information

using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR.Models.LocalV3;
using Sdcb.PaddleOCR.Models;
using Sdcb.PaddleOCR;

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度的文字 */
})
{
    var fs =Directory.GetFiles(@"D:\Pictures\发票\新发票");
    foreach (var item in fs)
    {
        // Load local file by following code:
        using (Mat src = Cv2.ImRead(item))
        // 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}");
            //}
        }
    }
}
Console.WriteLine("Hello, World!");

image

I don't understand ocr, just need to recognize text, I'm not sure if this is a normal phenomenon.

jaydubal commented 1 year ago

What is your image size in foreach (var item in fs) if they are large image then CPU use will be high.

I am also trying to avoid this, and use GPU but facing issues.

n0099 commented 1 year ago

https://github.com/sdcb/PaddleSharp/blob/7ad353470f24ef0d8e5d3039d570ea72d07d12c3/src/Sdcb.PaddleInference/PaddleDevice.cs#L17

bxjg1987 commented 1 year ago

My image has a maximum size of 427kb, 2592 * 1944 px PaddleDevice. Mkldnn (cpuMathThreadCount: 2) or 4

The problem still exists.

jaydubal commented 1 year ago

Please try this:

  1. try installing Sdcb.PaddleInference.runtime.win64.openblas and use PaddleDevice.Openblas() instead
  2. If you have a GPU then using the CUDA runtime packages will give maximum speed
  3. Try resizing your image using OpenCV Resize method, till you think the image is still readable, this will also help decreasing the CPU use
sdcb commented 1 year ago

@bxjg1987 CPU 100% is expected when using mkldnn, CpuMathThreadCount won't working as your expectation. If you wants to reduce your CPU, can either using GPU or Openblas as jaydubal said.

n0099 commented 1 year ago

If you have multiple cpu cores, you may use https://en.wikipedia.org/wiki/Processor_affinity to limit which core to run with your program.

bxjg1987 commented 1 year ago

Thank you very much, everyone! try installing Sdcb.PaddleInference.runtime.win64.openblas and use PaddleDevice.Openblas() instead Works well