2

I'm trying to execute the detect.py file using C# by creating a new process and passing ProcessStartInfo

Here is my method

         Process p = new Process();
            ProcessStartInfo info = new ProcessStartInfo
            {
                FileName = @"C:\Users\malsa\anaconda3\python.exe",
                RedirectStandardInput = true,
                RedirectStandardError = true,
                CreateNoWindow = false,
                UseShellExecute = false
            };
            p.StartInfo = info;
            p.Start();

            using (StreamWriter sw = p.StandardInput)
            {
                if (sw.BaseStream.CanWrite)
                {
                    sw.WriteLine(string.Format("cd {0}", @"C:\Users\malsa\Desktop\Yolo"));
                    sw.WriteLine(string.Format("detect.py --source {0}", ImagePath));
                }
            }

            p.WaitForExit();

For now the arguments should be passed to cmd, but since am setting the file to python.exe the arguments should be written in python.

Running detect.py using cmd

As shown in the image this how we can execute the detect.py by giving the --source ImgFile.jpg in cmd. The problem is that how can I execute the detect.py in Python-Shell by giving arguments such as ImgFile.jpg so that I can write these arguments in C# ?

I have tried something such as

subprocess.call(['C:\Users\malsa\Desktop\Yolo\detect.py', 0])

but I couldn't get it work.

1 Answers1

0

why dont use arguments?:

 Process p = new Process();
            ProcessStartInfo info = new ProcessStartInfo
            {
                FileName = @"C:\Users\malsa\anaconda3\python.exe",
                RedirectStandardError = true,
                CreateNoWindow = false,
                UseShellExecute = false,
                // here 3 args with name of program python with 2 args
                Arguments = string.Format("{0} --source {1} {2}", "detect.py", "0", "image.jpg")
            };
            p.StartInfo = info;
            p.Start();
Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • This is exactly what I needed, It's working successfully, I was writing the arguments using the StandardInput Stream but when I passed the arguments to the Argument Property it worked. – Mohammed Alsabi Sep 19 '20 at 14:59
  • standardinput is used to create command below os python – Frenchy Sep 19 '20 at 17:15