Как в c# применить нейросеть на python для создания синтеза речи?

  1. Есть готовая нейросеть на python, которая позволяет создать синтез речи
  2. Мне нужно сделать приложение WinForms с полем ввода текста и кнопкой для воспроизведения этого текста.

Как мне соединить c# и нейросеть на python?


Ответы (1 шт):

Автор решения: Yury Bakharev

Вы можете подключится к процессу и передавать аргументами ваши параметры.

public string run_cmd(string cmd, string args)
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = "PATH_TO_PYTHON_EXE";
        start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);
        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 stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
                string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
                return result;
            }
        }
    }

А так же вызвать скрипты.

public string PatchParameter(string parameter, int serviceid)
    {
        var engine = Python.CreateEngine(); // Extract Python language engine from their grasp
        var scope = engine.CreateScope(); // Introduce Python namespace (scope)
        var d = new Dictionary<string, object>
        {
            { "serviceid", serviceid},
            { "parameter", parameter}
        }; // Add some sample parameters. Notice that there is no need in specifically setting the object type, interpreter will do that part for us in the script properly with high probability

        scope.SetVariable("params", d); // This will be the name of the dictionary in python script, initialized with previously created .NET Dictionary
        ScriptSource source = engine.CreateScriptSourceFromFile("PATH_TO_PYTHON_SCRIPT_FILE"); // Load the script
        object result = source.Execute(scope); 
        parameter = scope.GetVariable<string>("parameter"); // To get the finally set variable 'parameter' from the python script
        return parameter;
    }
→ Ссылка