Created
April 4, 2013 05:56
-
-
Save rladstaetter/5308173 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.ladstatt.apps | |
import java.io.File | |
import org.opencv.core.Core | |
import org.opencv.core.MatOfRect | |
import org.opencv.core.Point | |
import org.opencv.core.Scalar | |
import org.opencv.highgui.Highgui | |
import org.opencv.objdetect.CascadeClassifier | |
/** | |
* Scala version of the introductory tutorial on opencv.org | |
*/ | |
object HelloOpenCV { | |
System.load(new File("/opt/local/share/OpenCV/java/libopencv_java244.dylib").getAbsolutePath()) | |
def main(args: Array[String]): Unit = { | |
new DetectFaceDemo().run() | |
} | |
} | |
class DetectFaceDemo { | |
def run(): Unit = { | |
println("\nRunning DetectFaceDemo") | |
// Create a face detector from the cascade file in the resources | |
// directory. | |
val faceDetector = new CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath()) | |
val image = Highgui.imread(getClass().getResource("/person.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().length)) | |
// Draw a bounding box around each face. | |
for (rect <- faceDetections.toArray()) { | |
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)) | |
} | |
// Save the visualized detection. | |
val filename = "faceDetection.png" | |
println(String.format("Writing %s", filename)) | |
Highgui.imwrite(filename, image) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment