Azure Functions 上的 Emgucv 4.2 版

Emgucv ver 4.2 on Azure Function

我想在 Azure Function V3 中使用 EmguCv ver 4.2

我创建了一个 Azure Function v3(.Net Core) 应用程序

已从 Nuget 安装 Emgu.Cv 版本 4.2.0.3636

已安装 Emgu.CV.Runtime.windows 版本 4.2.0.3636 也来自 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);
    }

运行 在调试中我得到:"All is well"

运行 在 Azure 上我得到:"Unable to load DLL 'cvextern' or one of its dependencies: The specified module could not be found. (0x8007007E)" 在第 "var image = new Image(256, 256);"

问题:如何从 Azure Function V3 运行 Emgucv?

@斯图尔德: From:Emgu 42 函数 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"
        }
      },

似乎 Azure 函数对 Emgu 的支持不是很好,但根据一些测试,我找到了一个解决方法,可能会帮助解决你的问题。请参考以下步骤试一试。

1. 转到这个link(您可能需要注册一个帐户然后登录),它会下载一个项目示例(名为"A_Basic_Program_x86").

2. 在 Azure 门户上创建一个新的 azure 函数并通过单击 "Advanced tools (Kudu)" 打开 "kudu"

3.然后在kudu页面,点击"Debug console"-->"CMD"--"site"-->"wwwroot" 然后在"wwwroot" 目录下新建文件夹"bin"。

4.打开我们上面下载的工程示例,在A_Basic_Program_x86\My Simple EMGU Program x86\My Simple EMGU Program\My EMGU Program\bin\Debug目录下可以找到一些dll文件。在这个目录下,有三个对我们有帮助的dll文件。它们是 Emgu.CV.dllEmgu.CV.UI.dllEmgu.Util.dll

5. 现在,进入我们在"wwwroot"目录下创建的"bin"文件夹,将这三个dll文件从本地拖入其中。 (如下截图所示)

6. 回到项目示例,进入A_Basic_Program_x86\My Simple EMGU Program x86\My Simple EMGU Program\My EMGU Program的目录。我们可以找到两个名为opencv_core231.dllopencv_imgproc231.dll的dll文件。重复上面的step5,将这两个dll文件拖到"bin"文件夹中。这两个dll文件在我上面提供的截图中也有显示。

7.在你的函数应用中创建一个http触发函数,请参考我下面的代码(请注意红框内的代码): 这里提供与上面截图相同的代码供您复制。

#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");
}

现在我们可以在 azure 函数中使用 "Image" 和 "Bgr" 而不会出现任何错误消息。

希望对你有帮助~

emgucv 4.2.0.3662 的新版本有一个更新来处理上述问题。 安装 Nuget Emgu.CV (V.4.2.0.3662 或以上版本) 安装 Nuget Emgu.cv.runtime.windows 4.2.0.3662

一切正常!