将Image对象作为参数从C传递到Python

2024-09-29 23:31:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将一个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))
            {

            }

Tags: 项目对象字符串image脚本true内容参数
3条回答

当我使用Python的Anaconda发行版时,在一个独立的conda环境上的测试中,OCR通过Python脚本在测试图像上成功地使用了pytesseract。在

测试的先决条件

  • 安装Anaconda并创建一个名为py3.7.4的env:conda create --name py3.7.4
  • conda activate py3.7.4激活env
  • 使用conda install -c conda-forge pytesseract安装pytesseract
  • {create文件夹名为jpg}>光学字符识别.jpg带有以下示例图像: enter image description here
  • 在同一个Test文件夹中,还放置一个名为ocr_test.py的Python脚本,代码如下:

    import pytesseract
    from PIL import Image
    import argparse
    
    parser = argparse.ArgumentParser(
        description='perform OCR on image')
    parser.add_argument("--path", "-p", help="path for image")
    args = parser.parse_args()
    print(pytesseract.image_to_string(Image.open(args.path)))
    print("done")
    

上面的代码段接受图像路径作为命令行参数。必须指定--path标志才能将图像路径作为参数传递。在

现在,在下面的C代码片段中,我们将:

  • 启动cmdshell
  • 通过为process.start()方法指定WorkingDirectory参数,导航到工作目录Test文件夹。在
  • anaconda.bat文件激活Anaconda(根据文件在计算机上的位置替换文件路径)
  • 激活上述conda环境
  • 调用Python脚本,将imageFileName作为参数传递。在

C代码片段:

^{pr2}$

如果您已执行上述步骤,则在Visual Studio中执行C代码段时应收到以下输出:

输出

Microsoft Windows [Version 10.0.18362.535]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\xxxxxxx\Projects\Scripts>C:\Users\xxxxx\Anaconda3\Scripts\activate.bat

(base) C:\xxxxxx\Projects\Scripts>conda activate py3.7.4

(py3.7.4) C:\xxxxxxx\Projects\Scripts>python ocr_test.py --path ocr.JPG
Introduction

This is a test to see accuracy of Tesseract OCR
Test 1

Test 2
done

注意:我无法用独立的Python发行版进行测试,但我相信它也可以很好地工作。关键是将图像文件路径作为参数传递给Python脚本。这样,Python也会对作为参数从C#传递的图像文件路径进行类似的处理。另外,使用Image.open()可以执行以下操作(来自文档):

Opens and identifies the given image file. This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data

您可以将图像保存为本地计算机上某个地方的文件,并给python程序读取它的路径。在

这是我认为你能做的最简单的方法。在

编辑:您可以使用临时文件来确保以后可以删除该文件

当手动执行OCRonImage2.py时,它是作为参数传递的图像文件位置吗?如果您从命令行传入一个流,我会很惊讶。尝试将整个图像的字节放入参数中会导致字符串太长,这一点也不奇怪。但是,对于您报告的错误,我还认为python脚本需要指向图像的文件路径。但是,如果您查看python代码,如果您发现它使用filepath参数来打开文件,我不会感到惊讶,可能使用图像.打开(文件路径,模式=r)。模式是可选的,r是默认值。在

但是你很幸运,图像.打开也需要一条小溪。如果您愿意修改python代码,有两个选项:

  1. 尝试将参数转换为流对象,因为参数是字符串斯金吉奥()
  2. 使用input()代替传递的参数,然后可以重定向进程的输入并将文件流式传输到python中。在
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = @"C:\Python\Python36\python.exe";
    start.Arguments = string.Format("{0}", @"C:\OCRonImage2.py");
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;
    start.RedirectStandardInput = true;
    start.CreateNoWindow = true;
    using (Process process = Process.Start(start))
    {
        StreamWriter streamWriter = process.StandardInput;
        streamWriter.Write({imageString});
        // ...
    }

确保在python脚本中执行与解码相同的imageString编码方式。在

希望这些解决方案中的一个对你有用。在

相关问题 更多 >

    热门问题