Trying to import external package (OpenCV)

I'm still very new to Swift, so if someone wouldn't mind providing some help, I'd really appreciate it. I'm trying out making a Swift CLI program, and I'd like to incorporate OpenCV (a C library for image processing). I found this package, which indicates that it provides a wrapper around the C library in the form of a binary: GitHub - r0ml/OpenCV: Swift Package for OpenCV

It doesn't provide any coding examples, nor can I find any, which leaves me a bit concerned (maybe I'm misunderstanding and I have to build the wrapper myself?). Anyway, I've modified my package file as appropriate, I believe (see below). When I build my program, I can see that it has downloaded the OpenCV package. It gives me no errors with including "OpenCV" as a dependency of my executable in the package file, suggesting everything is good there.

However, I cannot figure out how to interact with the code in my main.swift. "import OpenCV" gives me a "No such module" error, as does any other capitalization of "opencv." I just can't figure out whether I'm importing the package wrong or misunderstanding what the package actually provides. And advice would be greatly appreciated. Thank you!

let package = Package(
    name: "MyPackage",
    platforms: [.iOS(.v17), .macOS(.v13)],
    dependencies: [
        .package(url: "https://github.com/r0ml/OpenCV", from: "4.9.0")
    ],
    targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .executableTarget(
            name: "Swarcadia", dependencies: ["OpenCV"])
    ]
)```

I wasn’t able to get as far as you (for some reason git couldn’t pull the submodules. Something to do with being unable to probe the identity of GitHub.com).

Anyway. Looking at the package it seems to only define one swift function empty() which is, true to its name, empty.

Perhaps the package is simply an easy way to access the C++ API (looking at the docs, the C API seems to be deprecated).

@Joseph_Heck has contributed to the repo, so may have more info.

1 Like

Thanks for looking at this. I had the same problem with the submodules--apparently it clones them from github via ssh, so I had to add an ssh key on github to make it work.

I think you're likely right about its providing access to the C++ API. I just don't have a clear understanding of what would be the first step, in terms of interacting with that API.

2 Likes

I'm afraid my knowledge of the OpenCV library is limited to helping r0ml assemble the XCFramework with an embedded privacy manifest. I haven't used it directly.

The article Swift.org - Setting Up Mixed-Language Swift and C++ Projects would be a good starting point, and most notably, since this is Swift/C++ interop, I think you might be required to add in the .swiftSettings stanza to match what r0ml calls out in the readme, something along the lines of:

.executableTarget(
            name: "yourExecutable",
            dependencies: ["OpenCV"],
            swiftSettings: [.interoperabilityMode(.Cxx)])

Beyond that, I'd recommend Calling APIs Across Language Boundaries | Apple Developer Documentation and the associated WWDC23 Video: Mix Swift and C++ - WWDC23 - Videos - Apple Developer

1 Like

Thanks for the pointers! I'll take a look through that.