D
D
Dmitriy46rus2020-06-24 01:22:13
Python
Dmitriy46rus, 2020-06-24 01:22:13

How to use Python in C# code?

Hello, I have a Python code that converts speech to text. (Python version 3.6)
I want to recognize this result and then execute the code in C#, since it seems to me much more convenient.
Python code:

import speech_recognition as sr
sock = socket.socket()


r = sr.Recognizer()
m = sr.Microphone()


try:
  with m as source:
    r.adjust_for_ambient_noise(source)
    audio = r.listen(source)
    result =r.recognize_google(audio, language = "ru-RU").lower()
    #передать result в C# код

except:
  pass

I tried to write the result to a text document, but if you call the Python exe file through c#, then for some reason it did not work. I also tried through the server but did not fully figure it out and also did not work. Please help, otherwise I'm already tired of looking for solutions and every time I understand that it won't suit me. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitriy46rus, 2020-06-24
@Dmitriy46rus

For those who are interested, I solved the problem like this:

ProcessStartInfo startInfo = new ProcessStartInfo("python");
            Process process = new Process();

            string directory = @"C:\Users\User\";
            string script = "Name.py";

            startInfo.WorkingDirectory = directory;
            startInfo.Arguments = script;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardOutput = true;

            process.StartInfo = startInfo;

            process.Start();
            await Task.Delay(5000);

            process.Close();

M
Maxim Petelin, 2020-06-24
@petelinmn

Try IronPython, I think this is what you need. https://metanit.com/sharp/tutorial/9.3.php

V
Vladimir Korotenko, 2020-06-24
@firedragon

Dump python and call google api directly. Capture audio using NAudio. However, the engine is not fundamental, you can choose Yandex, Microsoft, Amazon

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question