I have been stuck on this issue for three days, hence resorting to Stack Overflow. On my server's localhost, the following executes fine. The Python script also runs fine. Everything works... perfectly!
But when I put it onto the domain, it doesn't. As I cannot debug on the website, I've had to send back error messages through ajax.
The error message that keeps coming is:
The system cannot find the file specified
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)\r\n at ImageProcessingService.generatePicture(String imgString) in C:\Websites\SAASTemplate\App_Code\ImageProcessingService.cs:line 152\r\n at ImageProcessingService.ProcessImageRedirectToOrder() in C:\Websites\SAASTemplate\App_Code\ImageProcessingService.cs:line 84"
I have tried many, many things. Initially, I thought it was the file I was sending to the Python, but it's not. It passes perfectly, and saves. Next, I thought it could be the Python, but by process of elimination, I don't think that's the case. My highest suspicion is that the Process needs a path like explained here. I don't fully understand it. However, from what I tried... it still doesn't work. Any help would be MASSIVELY appreciated.
public string generatePicture(string imgString)
{
var pythonScript = @"C:\Websites\SAASTemplate\pyScript\BackgroundRemoval\SemanticGuidedHumanMatting\remove_background.py";
string pythonWorkingDir = @"C:\Websites\SAASTemplate\pyScript\BackgroundRemoval\SemanticGuidedHumanMatting\";
string imagePath = imgString;
var reply = "null";
if (File.Exists(imagePath))
{
string savePath = Server.MapPath("/processed");
// Create a new process to run the Python script
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
// Set the working directory to the location of the Python script
startInfo.WorkingDirectory = pythonWorkingDir;
// Specify the path to the Python executable and the script with its arguments
var theScript = $"{pythonScript} --image \"{imagePath}\" --picture --save_path \"{savePath}\"";
//reply = theScript;
startInfo.FileName = "python";
startInfo.Arguments = theScript;
// Redirect standard output and errors to be able to capture the output
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
// Start the process
process.Start();
// Read the standard output and errors
string output = process.StandardOutput.ReadToEnd();
string errors = process.StandardError.ReadToEnd();
if (string.IsNullOrWhiteSpace(errors))
{
reply = "Complete";
}
else
{
reply = "Error";
}
// Wait for the process to exit
process.WaitForExit();
}
return reply;
}