Search code examples
vb.netfunctionopencvironpythonemgucv

How can I easily call an IronPython function from VB.net?


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! :)


Solution

  • 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