Emgucv ver 4.2 on Azure Function

2

I want to use EmguCv ver 4.2 in an Azure Function V3

I have created an Azure Function v3(.Net Core) application

Installed Emgu.Cv ver 4.2.0.3636 from Nuget

Installed Emgu.CV.Runtime.windows ver 4.2.0.3636 also from Nuget

[FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");


        try
        {
            SetPathOfEmgu();
            var image = new Image<Bgr, byte>(256, 256);
        }
        catch (Exception ee)
        {
            return (ActionResult)new BadRequestObjectResult(ee.Message);
            throw;
        }



        return (ActionResult)new OkObjectResult($"All is well");
    }

    public static void SetPathOfEmgu()
    {
        string pathValue = System.Environment.GetEnvironmentVariable("PATH");
        string emguPath = @"D:\home\site\wwwroot\x64";
        var paths = pathValue.Split(';').Where(o => o != "").ToList();
        paths.Add(emguPath);
        pathValue = string.Join(";", paths.Distinct().ToArray()) + ";";

        System.Environment.SetEnvironmentVariable("PATH", pathValue);
    }

Running in Debug i get:"All is well"

Running on Azure i get:"Unable to load DLL 'cvextern' or one of its dependencies: The specified module could not be found. (0x8007007E)" at line "var image = new Image(256, 256);"

Question: How do I run Emgucv from Azure Function V3?

@Stuard: From:Emgu 42 Function V3.deps.json

{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v3.0",
    "signature": ""
  },
  "compilationOptions": {},
  "targets": {
    ".NETCoreApp,Version=v3.0": {
      "Emgu 42 Function V3/1.0.0": {
        "dependencies": {
          "Emgu.CV": "4.2.0.3636",
          "Emgu.CV.runtime.windows": "4.2.0.3636",
          "Microsoft.NET.Sdk.Functions": "3.0.1"
        },
        "runtime": {
          "Emgu 42 Function V3.dll": {}
        }
      },
      "Emgu.CV/4.2.0.3636": {
        "runtime": {
          "lib/netstandard2.0/Emgu.CV.World.NetStandard.dll": {
            "assemblyVersion": "1.0.0.0",
            "fileVersion": "1.0.0.0"
          }
        }
      },
      "Emgu.CV.runtime.windows/4.2.0.3636": {
        "dependencies": {
          "Emgu.CV": "4.2.0.3636"
        }
      },
c#
azure-functions
emgucv
asked on Stack Overflow Feb 7, 2020 by Jake_2 • edited Feb 10, 2020 by Jake_2

2 Answers

1

It seems Azure function doesn't support Emgu well, but according to some test I found a workaround which may help your problem. Please refer to the steps below and have a try.

1. Go to this link (you may need to register an account and then sign in), it will download a sample of project(which named "A_Basic_Program_x86").

2. Create a new azure function on Azure portal and open "kudu" by clicking "Advanced tools (Kudu)" enter image description here

3. Then in kudu page, click "Debug console" --> "CMD" -- "site" --> "wwwroot" then create a new folder named "bin" under the "wwwroot" directory. enter image description here

4. Open the project sample which we downloaded above, we can find some dll files in the directory of A_Basic_Program_x86\My Simple EMGU Program x86\My Simple EMGU Program\My EMGU Program\bin\Debug. In this directory, there are three dll files which are helpful to us. They are Emgu.CV.dll, Emgu.CV.UI.dll and Emgu.Util.dll. enter image description here

5. Now, go to the "bin" folder which we create under the "wwwroot" directory and drag these three dll files from local into it.(shown as below screenshot) enter image description here

6. Come back to the project sample and go to the directory of A_Basic_Program_x86\My Simple EMGU Program x86\My Simple EMGU Program\My EMGU Program. We can find two dll files named opencv_core231.dll and opencv_imgproc231.dll. Repeat the step5 above, drag these two dll files to the "bin" folder. These two dll files are also shown in the screenshot I provided above.

7. Create a http trigger function in your function app and please refer to my code below (please notice the code in red box): enter image description here Here provide the code same with the screenshot above for you to copy.

#r "Newtonsoft.Json"
#r "..\bin\Emgu.CV.dll"
#r "..\bin\Emgu.CV.UI.dll"
#r "..\bin\Emgu.Util.dll"


using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    Image<Bgr, byte> image = new Image<Bgr, byte>(256, 256);

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    return name != null
        ? (ActionResult)new OkObjectResult($"Hello, {name}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

Now we can use "Image" and "Bgr" in azure function without any error message.

Hope it helps~

answered on Stack Overflow Feb 14, 2020 by Hury Shen
0

With the new version of emgucv 4.2.0.3662 there has been an update to handle the problem described above. Install Nuget Emgu.CV (V.4.2.0.3662 or above) Install Nuget Emgu.cv.runtime.windows 4.2.0.3662

And it all works!

answered on Stack Overflow Feb 17, 2020 by Jake_2

User contributions licensed under CC BY-SA 3.0