我想将一个image对象从我的c项目传递到python脚本,但是根据我的理解,不管参数中有什么内容,它都被认为是字符串,而且当我尝试在python中使用type(passedImage)
时,它会将它标识为字符串,即使我试图输入一个数字而不是image变量。在
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"C:\Python\Python36\python.exe";
start.Arguments = string.Format("{0} {1}", @"C:\OCRonImage2.py", image );
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.CreateNoWindow = true;
using (Process process = Process.Start(start))
{
}
当我使用Python的Anaconda发行版时,在一个独立的
conda
环境上的测试中,OCR通过Python脚本在测试图像上成功地使用了pytesseract
。在测试的先决条件:
conda create --name py3.7.4
conda activate py3.7.4
激活envconda install -c conda-forge pytesseract
安装pytesseract在同一个
Test
文件夹中,还放置一个名为ocr_test.py
的Python脚本,代码如下:上面的代码段接受图像路径作为命令行参数。必须指定
--path
标志才能将图像路径作为参数传递。在现在,在下面的C代码片段中,我们将:
cmd
shellprocess.start()
方法指定WorkingDirectory
参数,导航到工作目录Test
文件夹。在anaconda.bat
文件激活Anaconda(根据文件在计算机上的位置替换文件路径)imageFileName
作为参数传递。在C代码片段:
^{pr2}$如果您已执行上述步骤,则在Visual Studio中执行C代码段时应收到以下输出:
输出:
注意:我无法用独立的Python发行版进行测试,但我相信它也可以很好地工作。关键是将图像文件路径作为参数传递给Python脚本。这样,Python也会对作为参数从C#传递的图像文件路径进行类似的处理。另外,使用Image.open()可以执行以下操作(来自文档):
您可以将图像保存为本地计算机上某个地方的文件,并给python程序读取它的路径。在
这是我认为你能做的最简单的方法。在
编辑:您可以使用临时文件来确保以后可以删除该文件
http://www.java2s.com/Tutorial/CSharp/0300__File-Directory-Stream/Createatempfileanddeleteit.htm
http://www.vcskicks.com/temporary-file.php
当手动执行OCRonImage2.py时,它是作为参数传递的图像文件位置吗?如果您从命令行传入一个流,我会很惊讶。尝试将整个图像的字节放入参数中会导致字符串太长,这一点也不奇怪。但是,对于您报告的错误,我还认为python脚本需要指向图像的文件路径。但是,如果您查看python代码,如果您发现它使用filepath参数来打开文件,我不会感到惊讶,可能使用图像.打开(文件路径,模式=r)。模式是可选的,r是默认值。在
但是你很幸运,图像.打开也需要一条小溪。如果您愿意修改python代码,有两个选项:
确保在python脚本中执行与解码相同的imageString编码方式。在
希望这些解决方案中的一个对你有用。在
相关问题 更多 >
编程相关推荐