Location>code7788 >text

The way C# calls Python code (II), with PaddleOCR-GUI as an example

Popularity:888 ℃/2024-12-18 00:11:28

preamble

Previously introduced the use of Progress class in C# call Python script method, but this method in the need for frequent calls and the need for data interaction scenarios is not good, so today we share the C# call Python code way (two): use pythonnet call Python code.

Introduction to pythonnet

is a package that provides Python programmers with nearly seamless integration with the .NET Common Language Runtime (CLR) and gives .NET developers a powerful application scripting tool. It allows Python code to interact with the CLR and can also be used to embed Python into .

image-20241217120710093

Using pythonnet, using PaddleOCR-GUI as an example

Before using pythonnet, there are three concepts that need to be figured out about it, which are, figuring this out makes it easy to use.

image-20241217120547972

Let's look at how to specify first.

For example, you created a virtual environment with Python 3.12, but in this virtual environment, you can't find the DLL file, then you need to go to the original one in the Python 3.12 folder:

image-20241217120934674

My corresponding path here is: C:\Users\25398\AppData\Local\Programs\Python\Python312\.

Look again at how to specify.

Write the one in the virtual environment you created, here I correspond:

image-20241217121302447

Finally, let's look at how to specify.

are all the directories needed to run your python code, including the directory where you wrote your python code, some directories of the virtual environment, and some directories of the original environment, where I correspond to the following:

D:\Learning\PaddleOCR\;
D:\Learning\PaddleOCR\PaddleOCRVENV\Lib;
D:\Learning\PaddleOCR\PaddleOCRVENV\Lib\site-packages;
C:\Users\25398\AppData\Local\Programs\Python\Python312\Lib;
C:\Users\25398\AppData\Local\Programs\Python\Python312\Lib\site-packages;
C:\Users\25398\AppData\Local\Programs\Python\Python312\DLLs"

When you run the prompt that there is no module called XX, you need to see if they are all included, for example, at first when I did not include C:\Users\25398\AppData\Local\Programs\Python\Python312\DLLs, it will report a no XX module error, after the addition of the error, it will not.

Now assign values to these quantities in the ViewModel:

image-20241217122302883

Just use it this way:

 using (())
 {
     dynamic example = ("test4");
     if (SelectedFilePath == null)
     {
         return;
     }
     string image_path = SelectedFilePath;
     string selected_language = selectedLanguage;
     string result = example.use_paddleocr(image_path, selected_language);
     OCRText = result;
 }

image-20241217122342153

The corresponding Python code is as follows:

import logging
from paddleocr import PaddleOCR

def use_paddleocr(image_path,selected_language):
    # Configure the logging level to WARNING,this kind of DEBUG respond in singing INFO level log messages will be hidden
    (level=)

    # Creating a custom log processor,Outputs logs to the NullHandler(non-output)
    class NullHandler():
        def emit(self, record):
            pass

    # gain PaddleOCR The logger of the
    ppocr_logger = ('ppocr')

    # Remove all default log processors
    for handler in ppocr_logger.handlers[:]:
        ppocr_logger.removeHandler(handler)

    # Add customized NullHandler
    ppocr_logger.addHandler(NullHandler())

    ocr = PaddleOCR(use_angle_cls=True, lang=selected_language) # need to run only once to download and load model into memory
    result = (image_path, cls=True)
    result1 = ""
    for idx in range(len(result)):
        res = result[idx]
        for line in res:            
            result1 += line[1][0]
    return result1

The realized effect is shown below:

image-20241217123301884

One other pitfall encountered is that running synchronously is fine, but changing to asynchronous, the first two times it works, then it doesn't, and this needs to be added:

image-20241217123639735

Above is the whole content of this sharing, all the source code has been uploaded to GitHub at:/Ming-jiayou/PaddleOCR-GUI。