Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Build OpenCV DLL for MS Hololens2(Unity)

Discussion in 'Windows' started by Song_KCDR, Apr 1, 2021.

  1. Song_KCDR

    Song_KCDR

    Joined:
    Feb 15, 2021
    Posts:
    6
    I am using Unity to develop programs for MS Hololens2. Here, I am going to use OpenCV (C++ DLL) for image processing.
    In Unity, I changed the platform to Universal Windows Platform (UWP) and built it, and installed the Windows SDK and Hololens2 emulator. In order to develop a library using OpenCV, a DLL was created with a Universal Windows dedicated DLL project (C++). At this time, OpenCV proceeded using World.dll released from the official website.
    However, if the emulator calls a function in the DLL, it throws an exception saying that it cannot be loaded.

    There is no problem with how to create and build a DLL project for OpenCV. Because when I tested it without using OpenCV, it worked without any problems. Therefore, I thought I had to rebuild OpenCV World.dll using CMake, and even though I tried various things, it still didn’t work.
    Of course, the Unity project also included OpenCV World.dll and built it. (I tried to exclude it, but it still didn’t work)

    I searched for materials related to cross-compiling and tried to repeat it, but I couldn’t do it properly.
    Are there any resources to reference? How can I do it?

    OS: Windows10 (x64)
    OpenCV: 4.5.0
    Unity: 2019.4.22f1
    Visual Studio: Visual Studio Community 2019
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,530
    What is this world.dll? Is it built from a different project than the "Universal Windows dedicated DLL project (C++)" you mentioned?
     
  3. Song_KCDR

    Song_KCDR

    Joined:
    Feb 15, 2021
    Posts:
    6
    world.dll is an OpenCV DLL that is created when the OpenCV library is built. (same as opencv_world450.dll)
    Refer to opencv_world.dll in "Universal Windows dedicated DLL project (C++)" and write only the algorithm part separately. Another project is correct.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,530
    Which CPU architecture are you building it for?
     
  5. Song_KCDR

    Song_KCDR

    Joined:
    Feb 15, 2021
    Posts:
    6
    This is a Qualcomm Snapdragon based Hololens2. It runs on Windows10 64-bit, but I know it as ARM64. I don't have any equipment yet, so I'm testing it with an emulator.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,530
    HoloLens 2 emulator is x64. If you compiled the DLL to ARM64, then it will not be loadable in the emulator (but will work on the actual device).

    You should compile the DLL to all architectures you want it to work on, and then put them in the Unity project and configure them accordingly in the plugin inspector.
     
  7. Song_KCDR

    Song_KCDR

    Joined:
    Feb 15, 2021
    Posts:
    6
    Yes. So, when I made a DLL without OpenCV, I built it with x64 and tested it with an emulator.
    I made a basic DLL and tested it, and when I made a DLL by adding only OpenCV in the same environment, it didn't work.
    Should I seek input from the OpenCV or the Hololens community?
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,530
    The DLL can fail to load for several reasons:

    • It is missing/not getting deployed correctly (you should be able to verify this by calling File.Exists() in your app);
    • It is compiled for the wrong CPU architecture (looks like you already eliminated this reason);
    • It is compiled in debug mode but you're deploying the app in release mode;
    • It references DLLs/APIs not available on the devices, and thus cannot be loaded. Usually, you'd troubleshoot it by enabling loader snaps, however, I don't think that's an option on HoloLens. Perhaps you can try using https://github.com/lucasg/Dependencies to see if anything sticks out?
     
  9. SpandanaMatapathi

    SpandanaMatapathi

    Joined:
    Aug 10, 2020
    Posts:
    3
    Did you find the solution? I have the same problem.
    Edit :
    I have the dlls in Unity data folder in VS(in attached pic)


    Will that be a problem? The file structure was built by Unity. I just had the plugins in the assets.Th eonl difference is I ma using Hololens 1 so it is x86 architecture.
     

    Attached Files:

    Last edited: May 4, 2021
  10. Song_KCDR

    Song_KCDR

    Joined:
    Feb 15, 2021
    Posts:
    6
    Failed. I bought OpenCVForUnity.