共尝试了三种方式,1.2引用了以下三个
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using Python.Runtime;
Python.Runtime需要是 .net standard 类库 我用的是.net6
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();
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;
from paddleocr import PaddleOCR
import sys
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 </span>= <span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">python.exe</span><span style="color: rgba(128, 0, 0, 1)">"</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">python2.7的安装路径</span>
p.StartInfo.Arguments = sArguments;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">python命令的参数</span>
p.StartInfo.UseShellExecute = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
p.StartInfo.RedirectStandardOutput </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
p.StartInfo.RedirectStandardInput </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
p.StartInfo.RedirectStandardError </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
p.StartInfo.CreateNoWindow </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
p.Start();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">启动进程</span>
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("执行测试图片后多线程无报错");
https://blog.csdn.net/qq_39898066/article/details/123553021?utm_source=app&app_version=5.2.0