Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using JavaCV with Scala and SBT

DZone's Guide to

Using JavaCV with Scala and SBT

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.


Recently I’ve been doing some simple face detection in a Scala-based project. The “industry standard” for such kind of tasks is OpenCV; face detection is one of its basic use-cases.

Everybody knows this picture, right?

Everybody knows this picture, right?

However OpenCV is written in C/C++, so obviously to use it from Scala a JVM interface is needed. One of such interfaces is JavaCV, which wraps several computer-vision related libraries, one of them being OpenCV.

The process of actually doing face detection from Java using JavaCV is fairly well documented (see for example here), but what I had some trouble with, is getting SBT to get the JavaCV jars from the Maven central repository.

There are two steps. First, you need to add the dependency to your build settings:

val javacv = 
  "com.googlecode.javacv" % 
  "javacv" % 
  "0.7" classifier "linux-x86_64" classifier "macosx-x86_64" classifier ""
 
libraryDependencies += javacv

The unusual thing here are the classifiers. To get the basic jars you need an empty classifier, but you also need some platform-dependent bindings to the native libraries. Here I’m including bindings for OSX (my dev platform) and Linux (my deployment platform). There are also bindings for Windows, e.g. windows-x86.

But that won’t get you all the jars yet. The second SBT setting you must modify is:

classpathTypes += "maven-plugin"

That is because some of the dependent jars (javacpp) are packaged with the maven-pluginpackaging. And as SBT isn’t Maven, they won’t be included by default.

With the two settings above, you should be all set to start doing computer vision with JavaCV, SBT and Scala :)

Adam

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Adam Warski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}