Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running python script with arguments on c# and crashes when on cmd doesn't

somehow I'm running a python script with arguments, on cmd works perfectly, but when I pass it through my C# it seems it's not passing the arguments correctly.

Cmd results:

C:\Users\Sick\source\repos\phoneScraper\phoneScraper\bin\Debug\netcoreapp3.1\Captchas>script.py dztfi.png 
mysycd

Code:

static string run_cmd(string arg) // arg value = image.png
    {
        string result = ""; 
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"C:\Users\Sick\AppData\Local\Programs\Python\Python38-32\python.exe";//cmd is full path to python.exe
        start.Arguments = "Captchas/script.py " + arg;//args is path to .py file and any cmd line args
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                result = reader.ReadToEnd();
                Console.WriteLine(result);
            }
        }
        return result;
    }

    c# error:
Traceback (most recent call last):
  File "Captchas/script.py", line 11, in <module>
    close = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:757: error: (-215:Assertion failed) dims <= 2 && step[0] > 0 in function 'cv::Mat::locateROI'

Python code:

import cv2
import numpy as np
import pytesseract
import sys

img = cv2.imread(sys.argv[1], cv2.IMREAD_GRAYSCALE)

img = cv2.bitwise_not(img)

kernel = np.ones((7, 7), np.uint8)
close = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
newkernel = np.ones((5, 5), np.uint8)
inv = cv2.erode(close, newkernel, iterations=1)

inv = cv2.bitwise_not(inv)

custom_config = r'-l eng --oem 3 --psm 7 -c tessedit_char_whitelist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"'
text = pytesseract.image_to_string(inv, config=custom_config)
print(text)

Thanks!

like image 557
Sickness Avatar asked May 08 '26 11:05

Sickness


1 Answers

You should specify the WorkingDirectory of ProcessStartInfo. For example you use Application.StartupPath in your application. But that depends where your application starts. So if you start your app from cmd i think python.exe is running your script. But your applicationstarts in where your scripts. So try the script give script directory.

static string run_cmd(string arg) // arg value = image.png
{
    string result = ""; 
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = @"C:\Users\Sick\AppData\Local\Programs\Python\Python38-32\python.exe";//cmd is full path to python.exe
    start.Arguments = "Captchas/script.py " + arg;//args is path to .py file and any cmd line args
    start.UseShellExecute = false;
start.WorkingDirectory = ""//scriptPath
    start.RedirectStandardOutput = true;
    using (Process process = Process.Start(start))
    {
        using (StreamReader reader = process.StandardOutput)
        {
            result = reader.ReadToEnd();
            Console.WriteLine(result);
        }
    }
    return result;
}

For more info visit here: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.workingdirectory?view=netcore-3.1

like image 75
DLL_Whisperer Avatar answered May 11 '26 00:05

DLL_Whisperer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!