I have C # code that helps to start the python environment first and then it runs my python process. But the problem is that it takes a long time to complete.
Actually, I just want to pass my values and execute one line of code in a python script. But you need to execute all the python code every time. Is there a way to start a python process and just run a single line whenever I want.
I attached c # code and python process with this
C # code
public String Insert(float[] values)
{
string python = @"C:\ProgramData\Anaconda2\python.exe";
string myPythonApp = @"C:\classification.py";
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.CreateNoWindow = true;
myProcessStartInfo.WindowStyle = ProcessWindowStyle.Minimized;
myProcessStartInfo.Arguments = myPythonApp + " " + values[0] + " " + values[1] + " " + values[2] + " " + values[3] + " " + values[4] + " " + values[5];
Process myProcess = new Process();
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();
myProcess.WaitForExit();
myProcess.Close();
Console.WriteLine("Value received from script: " + myString);
Console.WriteLine("Value received from script: " + myString);
And python script
import numpy as np
import sys
val1 = float(sys.argv[1])
val2 = float(sys.argv[2])
val3 = float(sys.argv[3])
val4 = float(sys.argv[4])
val5 = float(sys.argv[5])
val6 = float(sys.argv[6])
url = "F:\FINAL YEAR PROJECT\Amila\data2.csv"
names = ['JawLower', 'BrowLower', 'BrowRaiser', 'LipCornerDepressor', 'LipRaiser','LipStretcher','Emotion_Id']
dataset = pandas.read_csv(url, names=names)
array = dataset.values
X = array[:,0:6]
Y = array[:,6]
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, Y)
print(neigh.predict([[val1,val2,val3,val4,val5,val6]]))
print (neigh.predict ([[val1, val2, val3, val4, val5, val6]])) is a line of code that I want to execute separately.
source
share