using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using HalconDotNet; using IronPython.Hosting; using Microsoft.Scripting.Hosting; using System.IO; using System.Diagnostics; using BroCVisionHalcon; using Bro.Tool.Redis; namespace Tool { class Program { static void Main(string[] args) { //var pyEngine = Python.CreateEngine(); //var scope = pyEngine.CreateScope(); //List searchPaths = new List(); //searchPaths.Add(@"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\Lib"); //searchPaths.Add(@"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\Lib\site-packages"); //DirectoryInfo searchPath = new DirectoryInfo(@"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\Lib"); //pyEngine.SetSearchPaths(searchPaths); Console.WriteLine($"选择需要执行的脚本:"); Console.WriteLine($"3.5.Polarity.py"); string command = Console.ReadLine(); DirectoryInfo root = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent; string pypath = $"{root.FullName}\\Profile\\"; //Task.Run(() => //{ // RunPythonScript(@"SocketService.py", "-u", new string[2]); //}); switch (command) { case "3.5": BroRedis.Test(); string[] strArr = new string[1];//参数列表 strArr[0] = "E:\\Test\\1#190110_150215\\"; RunPythonScript(@"main.py", "-u", strArr); break; } //3.2 //**********csv to tuple******************** //HDevEngineTool engineTool = new HDevEngineTool(); //engineTool.LoadProcedure("Profile"); //foreach(var file in Directory.GetFiles(@"E:\Test\Result")) //{ // FileInfo fileInfo = new FileInfo(file); // string[] lines = File.ReadAllLines(file); // double[] colArray = new double[lines.Length]; // double[] rowArray = new double[lines.Length]; // for (int i = 0; i < lines.Length; i++) // { // colArray[i] = Convert.ToDouble(lines[i].Split(',')[1]); // rowArray[i] = Convert.ToDouble(lines[i].Split(',')[2]); // } // HTuple col = new HTuple(colArray); // HTuple row = new HTuple(rowArray); // Dictionary tupleDic = new Dictionary(); // Dictionary imageDic = new Dictionary(); // //HOperatorSet.WriteTuple(col, file + "col.tup"); // //HOperatorSet.WriteTuple(row, file + "row.tup"); // File.Delete(file); // tupleDic.Add("INPUT_Col", col); // tupleDic.Add("INPUT_Row", row); // tupleDic.Add("INPUT_FileName", file); // engineTool.SetDictionary(tupleDic, imageDic); // engineTool.RunProcedure(); //} //3.4 //HOperatorSet.ReadTuple(@"C:\Users\Gavin\Desktop\profile\step3_线轮廓度\ColEnd.tup", out HTuple col); //HOperatorSet.ReadTuple(@"C:\Users\Gavin\Desktop\profile\step3_线轮廓度\RowEnd.tup", out HTuple row); //HOperatorSet.ReadTuple(@"C:\Users\Gavin\Desktop\profile\step3_线轮廓度\DistanceEnd.tup", out HTuple dis); //double[] colArray = col.DArr; //double[] rowArray = row.DArr; //double[] disArray = dis.DArr; //System.IO.File.AppendAllText("result.csv", $"x,y,dis\r\n"); //for (int i = 0; i < colArray.Length; i++) //{ // System.IO.File.AppendAllText("result.csv", $"{colArray[i]},{rowArray[i]},{disArray[i]}\r\n"); //} } public static string RunPyFunc(string path, string filename, string functionname, string parameter) { string cmd = $"-c \"import sys;sys.path.append('{path}');import {filename};print({filename}.{functionname}({parameter}))\""; return Run_cmd(@"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe", cmd); } public static string Run_cmd(string program, string cmd) { ProcessStartInfo start = new ProcessStartInfo(); start.FileName = program; start.Arguments = cmd; start.UseShellExecute = false; // Do not use OS shell start.CreateNoWindow = true; // We don't need new window start.RedirectStandardOutput = true; // Any output, generated by application will be redirected back start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions) using (Process process = Process.Start(start)) { using (StreamReader reader = process.StandardOutput) { string result = process.StandardError.ReadToEnd(); if (result == null || result == "") { result = reader.ReadToEnd(); } return result; } } } public static void RunPythonScript(string sArgName, string args = "", params string[] teps) { Process p = new Process(); DirectoryInfo root = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent; string pypath = $"{root.FullName}\\Profile\\"; string path = pypath + sArgName;// 获得python文件的绝对路径 p.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\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(); p.BeginOutputReadLine(); p.OutputDataReceived += new DataReceivedEventHandler(OnPythonOutputDataReceived); Console.ReadLine(); p.WaitForExit(); } static void OnPythonOutputDataReceived(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) { Console.WriteLine(text); //此处在控制台输出.py文件print的结果 } } }