方法二:查找资料后,发现还有一种方法
即使用安装好的python环境,利用c#命令行,调用.py文件执行
优点:执行速度只比在python本身环境中慢一点,步骤也相对简单。
缺点:需要用户安装配置python环境
下面展示代码`。
#multi.py
def multiplication(a,b):
return a*b
import numpy as n p
import multi
import sys
def func(a,b):
result=np.sqrt(multi.multiplication(int(a),int(b)))
return result
if __name__ == '__main__':
print(func(sys.argv[1],sys.argv[2]))
//c#代码如下
private void button1_Click(object sender, EventArgs e)
{
string[] strArr = new string[2];//参数列表
string sArguments = @"main.py";//这里是python的文件名字
strArr[0] = "5";
strArr[1] = "5";
RunPythonScript(sArguments, "-u", strArr);
}
public static void RunPythonScript(string sArgName, string args = "", params string[] teps)
{ //实例一个Process类,启动一个独立进程
Process p = new Process();
string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + sArgName;// 获得python文件的绝对路径(将文件放在c#的debug文件夹中可以这样操作)
path = "C:\\Users\\82357\\Desktop\\SHENDU\\python_exe\\ironpython_exe\\ironpython_exe\\bin\\Debug\\" + sArgName;//(因为我没放debug下,所以直接写的绝对路径,替换掉上面的路径了)
//Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,下面我们用到了他的几个属性:
//p.StartInfo.FileName设定程序名
p.StartInfo.FileName = @"C:\\Users\\82357\\Anaconda3\\python.exe";//没有配环境变量的话,可以像我这样写python.exe的绝对路径。如果配了,直接写"python.exe"即可
string sArguments = path;
foreach (string sigstr in teps)
{
sArguments += " " + sigstr;//传递参数
}
sArguments += " " + args;
// p.StartInfo.Arguments设定程序执行参数
p.StartInfo.Arguments = sArguments;
//p.StartInfo.UseShellExecute开关Shell的使用
p.StartInfo.UseShellExecute = false;
//p.StartInfo.RedirectStandardOutput重新定向标准输出
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.RedirectStandardInput重新定向标准输入
p.StartInfo.RedirectStandardInput = true;
//p.StartInfo.RedirectStandardError重新定向错误输出
p.StartInfo.RedirectStandardError = true;
// p.StartInfo.CreateNoWindow设置不显示窗口
p.StartInfo.CreateNoWindow = true;
//p.StandardInput.WriteLine(command); //也可以用这种方式出入要执行的命令
//p.StandardInput.WriteLine("exit"); //不过要记得加上Exit要不然下一行程序执行的时候要当机
p.Start();
p.BeginOutputReadLine();
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
Console.ReadLine();
p.WaitForExit();
}
//输出打印的信息
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
AppendText(e.Data + Environment.NewLine);
}
}
public delegate void AppendTextCallback(string text);
public static void AppendText(string text)
{
MessageBox.Show(text);
//此处在控制台输出.py文件print的结果
//Console.WriteLine(text);
}*/
该方法我发现使用简单,即通过调用python.exe运行py文件。例子设置了返回string参数于是想把代码写简单一点调用看看:
if __name__ == '__main__':
# img = cv2.imread(r'C:/Users/82357/Desktop/SHENDU/deep_dll/deep_dll/x64/Release/pic_temp/now.jpg')
img = cv2.imread( sys.argv[1])
#load_image()是自己定义的一个深度学习模型处理函数
load_image(img)
cv2.waitKey(0)
# print("调用成功")
private void button4_Click(object sender, EventArgs e)
{
string strArr = filename;
string sArguments = @"zzz.py";
RunPythonScript(sArguments, strArr);
}
public static void RunPythonScript(string sArgName, params string[] teps)
{
Process p = new Process();
string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + sArgName;// 获得python文件的绝对路径(将文件放在c#的debug文件夹中可以这样操作)
path = "C:\\Users\\82357\\Desktop\\SHENDU\\deep_dll\\deep_dll\\x64\\Release\\" + sArgName;//(因为我没放debug下,所以直接写的绝对路径,替换掉上面的路径了)
//p.StartInfo.FileName设定程序名
p.StartInfo.FileName = @"C:\\Users\\82357\\Anaconda3\\python.exe";//没有配环境变量的话,可以像我这样写python.exe的绝对路径。如果配了,直接写"python.exe"即可
string sArguments = path;
foreach (string sigstr in teps)
{
sArguments += " " + sigstr;//传递参数
}
// sArguments += " " + args;
p.StartInfo.Arguments = sArguments;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();//启动
}
这里只做到传递一个string类型参数进去,然后运行py文件,py文件中包含深度学习模型和opencv库,能够调用成功。

1921

被折叠的 条评论
为什么被折叠?



