Skip to content

Instantly share code, notes, and snippets.

@cimadai
Created January 27, 2016 10:59
Show Gist options
  • Save cimadai/f399057fceb6b6f68773 to your computer and use it in GitHub Desktop.
Save cimadai/f399057fceb6b6f68773 to your computer and use it in GitHub Desktop.
opencv3.1 usage of scala / sbt
name := "opencv31_usage_scala"
scalaVersion := "2.11.7"
scalacOptions ++= Seq(
"-unchecked",
"-deprecation",
"-optimize",
"-Xlint"
)
javaOptions in run += "-Djava.library.path=./lib"
lazy val root = (project in file("."))
fork := true
import org.opencv.core.{Core, MatOfRect, Point, Scalar}
import org.opencv.imgcodecs.Imgcodecs
import org.opencv.imgproc.Imgproc
import org.opencv.objdetect.CascadeClassifier
object DetectFaceDemo extends App {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
// Create a face detector from the cascade file in the resources
// directory.
val faceDetector = new CascadeClassifier(getClass.getResource("/lbpcascade_frontalface.xml").getPath)
val image = Imgcodecs.imread(getClass.getResource("/lena.png").getPath)
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
val faceDetections = new MatOfRect()
faceDetector.detectMultiScale(image, faceDetections)
println("Detected %s faces".format(faceDetections.toArray.size))
faceDetections.toArray.foreach(rect => {
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0, 0))
})
// Save the visualized detection.
val filename = "faceDetection.png"
System.out.println(String.format("Writing %s", filename))
Imgcodecs.imwrite(filename, image)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment