I want to execute a python script in my C# project The python script has some imported modules.
I use ironpython library in my c# project and set the python modules path for it but it does not work and gives me an invalid syntax error;However, there is no error in the Python script itself.
I also use pyinstaller to create an executable file from my python script for run it in my c# project but the output executable file also does not work and gives me the "Failed to execute script...." error.
Please help me....
Note that I want to be able to run my project on any computers without any problems.
Thanks.
What I have tried:
My python script codes:
import speaker_verification_toolkit.tools as svt
import os
def MyFile(fileName):
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
my_file = os.path.join(THIS_FOLDER, fileName)
return my_file
def GetFiles():
Files=[]
basepath = MyFile('voices/')
for entry in os.listdir(basepath):
if os.path.isfile(os.path.join(basepath, entry)):
Files.append(os.path.join(basepath, entry))
return Files
def FilesMFCC():
Signals=[]
Files=GetFiles()
for i in Files:
s=svt.extract_mfcc_from_wav_file(MyFile(i),16000,0.025,0.01)
Signals.append(svt.rms_silence_filter(s,16000,None,0.001135))
return Signals
def SetResult(distance):
if distance<70000:
f= open(MyFile('verifylog.txt'),"w+")
f.write("true")
f.close()
else:
f= open(MyFile('verifylog.txt'),"w+")
f.write("false")
f.close()
x=svt.extract_mfcc_from_wav_file(MyFile('voicein.wav'),16000,0.025,0.01)
xx=svt.rms_silence_filter(x,16000,None,0.001135)
SigList=FilesMFCC()
result=svt.find_nearest_voice_data(FilesMFCC(),xx)
rr=svt.rms_silence_filter(SigList[result],16000,None,0.001135)
f1=svt.compute_distance(xx,rr)
SetResult(f1)
My C# Codes to run the python script:
private bool RunSpeakVerifier()
{
var Engine = Python.CreateEngine();
var Script = "SpeakerVerifier.py";
var source = Engine.CreateScriptSourceFromFile(Script);
ICollection<string> paths = Engine.GetSearchPaths();
string dir = @"C:\Users\Mohammad Tavoosi\AppData\Local\Programs\Python\Python38\Lib\";
paths.Add(dir);
string dir2 = @"C:\Users\Mohammad Tavoosi\AppData\Local\Programs\Python\Python38\Lib\site-packages";
paths.Add(dir2);
Engine.SetSearchPaths(paths);
source.Execute();
if (File.Exists("verifylog.txt"))
{
string txt = File.ReadAllText("verifylog.txt");
if (txt == "true") return true;
else return false;
}
else return false;
}