Gavin
2021-02-04 4e5aaefc7162b700b95c750caeff35e6323631d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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<string> searchPaths = new List<string>();
            //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<string, HTuple> tupleDic = new Dictionary<string, HTuple>();
            //    Dictionary<string, HObject> imageDic = new Dictionary<string, HObject>();
 
            //    //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的结果         
        }
          
    }
}