Machine learning notes c# call python script file for model reasoning

1. Brief introduction

        Many python-based deep learning libraries or frameworks not only perform inference, but also include image preprocessing before inference and data analysis after inference. Therefore, if it is not based on python when using it, it will require a lot of extra work, and you will need to write your own code to handle image processing before inference and data analysis after inference, etc.

        Sometimes it is really troublesome, it is easier to use python directly. But if it is not a python-based interface service or desktop application, it will involve cross-language calls.

        Let's talk about the call based on c#.

2. Call based on C#

        Directly upload the code. The following code is mainly based on c# Process, which directly calls python.exe to execute the py script. You need to install the python environment and required packages in advance, and prepare the python code.

private List<string> results = new List<string>();

//基于Process的调用
private void call_python_ocr(string pic_path)
{
    final_res.Clear();
    Process p = new Process();
    // 获得python文件的绝对路径(将文件放在c#的debug文件夹中可以这样操作)
    //string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + sArgName;
    //(因为我没放debug下,所以直接写的绝对路径,替换掉上面的路径了)
    string path = @"D:\MyOCR\recognition.py";
    //可以写python.exe的绝对路径。如果配了环境变量,直接写"

Guess you like

Origin blog.csdn.net/bashendixie5/article/details/130149313