共尝试了三种方式,引用了以下三个
using IronPython.Hosting; using Microsoft.Scripting.Hosting; using Python.Runtime;
Python.Runtime需要是 .net standard 类库 我用的是.net.0
1.没有引用第三方的时候可以, 引用第三方就会找不到第三方 例如 找不到import的paddleocr
ScriptRuntime pyRuntime = IronPython.Hosting.Python.CreateRuntime(); dynamic obj = pyRuntime.UseFile(@"D:\Develop\TEST\OCR\OCRdemo.py"); dynamic dy = obj.my_ocr(@"C:\Users\Thinkpad\Pictures\0\258.jpg"); Console.WriteLine(dy.ToString());
猜测大概等同于下面这种其他人的方式(未验证)
ScriptEngine pyEngine = Python.CreateEngine();//创建Python解释器对象 dynamic py = pyEngine.ExecuteFile(@"test.py");//读取脚本文件 int[] array = new int[9] { 9, 3, 5, 7, 2, 1, 3, 6, 8 }; string reStr = py.main(array);//调用脚本文件中对应的函数 Console.WriteLine(reStr); Console.ReadKey(); ———————————————— 版权声明:本文为CSDN博主「人工智能之浪潮」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/mago2015/article/details/119450865
2.这种方式可以把py文件的内容运行出来,分为两种(同步获取和异步回调两种方式)
2.1、同步
Process p = new Process(); //环境安装路径 (已经配置了系统变量指向了paddle38,所以可以直接写python.exe) p.StartInfo.FileName = @"python.exe"; //dll+空格+参数 p.StartInfo.Arguments = @"D:\Develop\TEST\OCR\OCRdemo.py C:\Users\Thinkpad\Pictures\0\258.jpg";//参数以空格分隔,如果某个参数为空,可以传入”” p.StartInfo.UseShellExecute = false; //必需 p.StartInfo.RedirectStandardOutput = true;//输出参数设定 p.StartInfo.RedirectStandardInput = true;//传入参数设定 p.StartInfo.CreateNoWindow = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit();//关键,等待外部程序退出后才能往下执行} Console.Write(output);//输出 p.Close(); //txt1.Text = output;
备注:传参方式 直接空格接在文件名后面,python里接参数要写 例如:sys.args[1] 以下是python代码
from paddleocr import PaddleOCR import sys def my_ocr(img_path): ocr = PaddleOCR(lang='ch') result = ocr.ocr(img_path) for line in result: print(line) return result if __name__ == '__main__': # print(len(sys.argv)) if len(sys.argv) > 1 : arg1 = sys.argv[1] else: arg1 = r'C:\Users\Thinkpad\Pictures\0\259.jpg' r1 = my_ocr(arg1) # for line in r1: # print(line) print("main ok") # pass
2.2、异步回调
public static void function2() { Process p = new Process(); //string path = "OCRdemo.py";//待处理python文件的路径,本例中放在debug文件夹下 string sArguments = @"D:\Develop\TEST\OCR\OCRdemo.py"; string[] strArr = new string[1]; strArr[0] = @"C:\Users\Thinkpad\Pictures\0\258.jpg"; foreach (var sigstr in strArr)//添加参数 { sArguments += " " + sigstr; } p.StartInfo.FileName = @"python.exe"; //python2.7的安装路径 p.StartInfo.Arguments = sArguments;//python命令的参数 p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start();//启动进程 p.BeginOutputReadLine(); p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); //Console.ReadLine(); p.WaitForExit(); } private static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data)) { Console.WriteLine(e.Data); //此处在控制台输出.py文件print的结果 } }
以上两种 py文件可以放在任意路径。
3.这种 我电脑暂时没运行出来,别的电脑可以
PythonEngine.Initialize(); var pyThread = PythonEngine.BeginAllowThreads(); var pyLock = PythonEngine.AcquireLock(); //多线程调用,先锁定线程,最后再释放线程 功能同using (Py.GIL()){} Console.WriteLine("开启线程,锁定线程"); //using (Py.GIL()) //Initialize the Python engine and acquire the interpreter lock //{ // import your script into the process dynamic AL = Py.Import("test1");//python脚本文件名 string image_path = @"C:\Users\Thinkpad\Pictures\0\258.jpg"; AL.main(image_path); Console.WriteLine("执行测试图片后多线程无报错");
最终,第三种方式未继续研究,这个文章对之后可能有帮助,先记下来 《
C#调用PYD
》 https://www.freesion.com/article/71281350870/
备注给自己: 本文是为了C#调用百度飞桨OCR识别搭建的临时环境,用了anaconda集成了python3.9。 过程中比较着重的参考了以下文章:
paddleocr:python脚本使用
https://blog.csdn.net/wss794/article/details/122476110
windows10通过anaconda安装paddle和paddleOCR并将图片转为excel
https://blog.csdn.net/qq_39898066/article/details/123553021?utm_source=app&app_version=5.2.0
深度学习Python环境打包到另外一台电脑(详细教程)
https://blog.csdn.net/excelNo1/article/details/117733718
C#调用Python3(在VisualStudio2022中基于.NET6.0框架调用Py。。。
https://wenku.baidu.com/view/2c8453e08aeb172ded630b1c59eef8c75fbf9587.html
C#调用python脚本并传递参数的一种方法
https://wenku.baidu.com/view/40d6f8840329bd64783e0912a216147917117e7b.html
这个也该和我的差不多
https://blog.csdn.net/ShyLoneGirl/article/details/114171320