如何从 VB.net 轻松调用 IronPython 函数?

发布于 2024-11-30 16:12:18 字数 427 浏览 0 评论 0原文

我在这个使用 emgucv(.net 的 opencv 包装器)的程序中挣扎了大约两周。不幸的是,问题不是编程,而是以有效的方式设置 emgucv。我没能为 vb.net 做到这一点,所以我尝试为ironpython 做到这一点(因为我也了解 python)。使用 IronPython 时,Emgucv 似乎工作得很好,所以我创建了一个函数,它将图像作为参数并按照我想要的方式对其进行分析,返回另一个图像及其结果。问题是我想调用这个函数,从 VB.net 中给它图像参数(它可以是包含路径的字符串),并成为另一个包含结果图像作为返回的字符串。我稍后计划将该项目打包到一个设置中,以便我可以重新分发它。

所以我问你们:你们知道在 VB.net 中调用 IronPython 函数的简单方法吗?这样我也可以打包整个项目并将其重新分发给人们?

非常感谢您阅读本文,如果您也能帮助我解决我的问题,那就太好了! :)

I am struggling with this program which uses emgucv(an opencv wrapper for .net) for about 2 weeks. The problem is unfortunately not programming, but setting up emgucv in such a way that it works. I didn't manage to do so for vb.net so I tried doing it for ironpython(because I know python too). Emgucv seems to work perfectly when using ironpython, so I created a function that takes an image as an argument and analyses it in the way I want, returning another image with the results in it. The problem is I want to call this function, giving it the image argument(it could be a string containing the path) from within VB.net and become another string containing the result image as return. I later plan to package that project in a setup so I can redistribute it.

So I am asking you guys: Do you know an easy way to call an IronPython function in VB.net in such a way so I can also package the whole project and redistribute it to people?

Thank you so much for reading this and it would be great if you could also help me with my problem! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀里藏娇 2024-12-07 16:12:18

虽然 IronPython 不是我的专长,但我非常熟悉 EMGU 及其应用程序。如果您坚持使用 IronPython,以下网站清楚地展示了如何将字符串传递给 IronPython 类。

以下代码取自链接,不是我自己的:

Option Explicit On
Option Strict On

Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types

Module Module1
    Sub Main()
        Dim helloWorld As New HelloWorldVB()
        Console.WriteLine(helloWorld.HelloWorld("Maurice"))

    Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
    Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
    Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
    helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
    Console.WriteLine(helloWorld.HelloWorld("Maurice"))

    Console.ReadLine()
End Sub
End Module

我会按照链接中的教程进行操作,但重要的代码如下因为这会导入 IronPython 所需的运行时信息:

**Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types**

&

 **Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
    Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
    Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
    helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
    Console.WriteLine(helloWorld.HelloWorld("Maurice"))**

显然 Console.WriteLine(helloWorld.HelloWorld("Maurice")) 将被更正为:

Dim result_location As String = helloWorld.HelloWorld("Maurice")

其中“Maurice”将是包含图像位置的字符串。

现在我必须询问您在 Visual Studio 中设置 EMGU 时遇到的问题,我知道这可能会令人沮丧,尤其是对于刚接触它的人来说。如果您愿意,我很乐意帮助您正确设置。我问的原因是因为您将其提供给最终用户,因此您的代码可以在不调用 IronPython 的情况下更加高效。特别是因为每个进程都需要从硬盘驱动器读取和写入。

首先:我假设您已在项目中包含对 Emgu.CV、Emgu.CV.UI 和 EMGU.Util 的引用。但必须将“opencv_core220.dll”、“opencv_imgproc220.dll”文件直接添加到项目中,并确保在属性窗口中将“复制到输出”选项设置为“始终复制”。如果不是,您将收到图像格式不正确等错误。您只需要这两个 .dll 来读取图像并访问数据等,例如,您可能需要其他 .dll 来播放 .avi 电影。请注意,这两个 .dll 现在必须与您的项目一起分发才能运行。

请注意,这可能会发生变化,具体取决于您使用的是 64 位计算机还是 32 位计算机,但 64 位 EMGU 版本将无法在 X86 计算机上运行。您还必须确保 Visual Studio 中的目标平台正确。

我希望这对你有帮助,

干杯
克里斯

While IronPython is not my expertise I am well versed in EMGU and its applications. If you insist in using IronPython the following website clearly shows how to pass a string to an IronPython Class.

The following code is taken from the link and is not my own:

Option Explicit On
Option Strict On

Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types

Module Module1
    Sub Main()
        Dim helloWorld As New HelloWorldVB()
        Console.WriteLine(helloWorld.HelloWorld("Maurice"))

    Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
    Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
    Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
    helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
    Console.WriteLine(helloWorld.HelloWorld("Maurice"))

    Console.ReadLine()
End Sub
End Module

I would follow the tutorial from the link but the important code is bellow as this imports the require runtime information for IronPython:

**Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types**

&

 **Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
    Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
    Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
    helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
    Console.WriteLine(helloWorld.HelloWorld("Maurice"))**

Obviously Console.WriteLine(helloWorld.HelloWorld("Maurice")) would be corrected to:

Dim result_location As String = helloWorld.HelloWorld("Maurice")

Where "Maurice" would be the string containing your image location.

Now I have to ask about the problems you were having setting up EMGU in visual studio I know it can be frustrating to do especially to people who are new to it. If you would like I would be happy to help you set it up properly. The reason I ask is since you are providing this to and end user your code could be more efficient without calling IronPython. Especially since each process will require reading and writing from the hard drive.

To start: I will assume that you have included References to Emgu.CV, Emgu.CV.UI, and EMGU.Util in your project. But it is essential that you add "opencv_core220.dll", "opencv_imgproc220.dll" files directly to your project and ensure in the properties window that the 'Copy to Output' option is set to "Copy always". If it isn't you will get errors of not a having the image in the right format etc. You only really need these two .dll to read images in and access the data etc you may need others for .avi movies for example. Note that these two .dll must now be distributed with your project for it to work.

To note this may change depending if your using a 64 bit machine or a 32 bit machine but the 64 bit EMGU version will not run on X86 machines. You must also ensure that your target platform is correct in Visual Studio.

I hope this helps you,

Cheers
Chris

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文