0

I am having issues passing dashed arguments to the cmd.exe for executing a (Python) script.

Code that does work (without dashed arguments):

            ProcessStartInfo psi = new()
            {
                FileName = "cmd.exe",
                Arguments = $"/c \"rembg i {inputPath} {outputPath}\"",
                RedirectStandardError = true,
                RedirectStandardOutput = true,
            };
            Process proc = Process.Start(psi);
            string output = proc.StandardOutput.ReadToEnd();
            string error = proc.StandardError.ReadToEnd();
            await proc.WaitForExitAsync();

Code that doesn't work (with dashed arguments):

            ProcessStartInfo psi = new()
            {
                FileName = "cmd.exe",
                Arguments = $"/c \"rembg i -a -ae 15 {inputPath} {outputPath}\"",
                RedirectStandardError = true,
                RedirectStandardOutput = true,
            };
            Process proc = Process.Start(psi);
            string output = proc.StandardOutput.ReadToEnd();
            string error = proc.StandardError.ReadToEnd();
            await proc.WaitForExitAsync();

Interestingly enough, both cases produce correct results when run in cmd shell directly:

rembg i INPUT_PATH OUTPUT_PATH

Or:

rembg i -a -ae 15 INPUT_PATH OUTPUT_PATH

Even when run directly as cmd process within cmd shell it works, such as:

cmd /c "rembg i -a -ae 15 INPUT_PATH OUTPUT_PATH"

Unless it's something very obvious that I am missing, it appears that the issue lies in passing the dashed parameters to the script, which works normally from the cmd shell directly and when starting a new cmd process from the shell.

EDIT:

This is the error that is received within the StandardError, originating from rembg:

Traceback (most recent call last):
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\Scripts\rembg.exe\__main__.py", line 7, in <module>
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\click\core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\click\core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\click\core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\click\core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\click\core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\rembg\cli.py", line 87, in i
    output.write(remove(input.read(), session=new_session(model), **kwargs))
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\rembg\bg.py", line 116, in remove
    cutout = alpha_matting_cutout(
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\rembg\bg.py", line 52, in alpha_matting_cutout
    alpha = estimate_alpha_cf(img_normalized, trimap_normalized)
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\pymatting\alpha\estimate_alpha_cf.py", line 53, in estimate_alpha_cf
    L = cf_laplacian(image, **laplacian_kwargs, is_known=is_known)
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\pymatting\laplacian\cf_laplacian.py", line 168, in cf_laplacian
    _cf_laplacian(image, epsilon, radius, values, indices, indptr, is_known)
  File "C:\Users\sven.mijic\AppData\Local\Programs\Python\Python39\lib\site-packages\pymatting\laplacian\cf_laplacian.py", line 8, in _cf_laplacian
    assert d == 3
AssertionError
swanna
  • 43
  • 1
  • 7

1 Answers1

0

With some additional testing, I found the way that it works.

So the rembg script is being run as part of C# code after an image has been previously processed by SixLabors.ImageSharp.

However, when I saved the image as a PNG type (using Image.SaveAsPngAsync()), the additional dash arguments of the rembg script that represent alpha matting would not work. As soon as I saved the image as a JPEG (Image.SaveAsJpegAsync()), the alpha matting went through. Otherwise, without alpha matting arguments, the rembg script would go through correctly for PNG format, which is why I didn't suspect the image format itself to be the problem.

It would be helpful if someone else could provide additional clarity as to why this works. I assume it's due to the nature of the different image formats and their processing. Otherwise, I hope this alone might help someone else with the same issue.

swanna
  • 43
  • 1
  • 7