TheGuidebook

Setting up Computer Vision Libraries in Unity

Setting up Computer Vision Libraries in Unity

Overview

Computer vision is an approach for analyzing image data, often from realtime video feeds like webcams. Various tools are available to integrate computer vision into Unity projects, the most popular is a library called ‘OpenCV’ which is primarily interfaced with from C++ or Python. OpenCV is open source and free to use.

In order to use it within Unity, we need some translation or conversation layer to talk to the OpenCV Library. This guide explores three different wrappers for the OpenCV computer vision framework in the Unity game engine.

Methods

Emgu CV Asset

Manual Integration of Emgu

  • Resources: GitHub Repository
  • Procedure: Follow documentation and instructions provided on the GitHub repository for manual integration of Emgu using NuGet in Unity.

OpenCV For Unity (Enox Software)

OpenCV Plus Unity (Paper Planes Tools)

  • Resource: OpenCV Plus Unity
  • Issues: Has not been updated since 2019, resulting in code errors.
  • Resolution: Enable ‘Allow Unsafe Code’ in Unity. For Unity 2022. Modify this outdated code to ensure compatibility:

The error that appears from this line:

void OnDestroy() { 			 
    SceneManager.UnloadScene ("DocumentScannerScene");
}

Can be changed it to this to work with newer Unity versions (tested with 2022.2):

void OnDestroy() {
    Scene scene = SceneManager.GetSceneByName("DocumentScannerScene");
    if (scene.isLoaded) {
        SceneManager.UnloadSceneAsync(scene);
    } else {
        Debug.LogError("Scene not loaded: DocumentScannerScene");
    }
}
  • Integration: Implement computer vision scenes by:
    • Selecting the example scene that serves the purpose you want. The example scene should just work if you follow the instructions above.
    • Note: You may have to build the scene for it to work without buffering, it will run smoothly in a build, not as well in the editor.