OpenCV and Java

This is adapted from:


<project name="Main" basedir="." default="rebuild-run">

   <property name="src.dir" value="src"/>

   <property name="lib.dir" value="${ocvJarDir}"/>
   <path id="classpath">
       <fileset dir="${lib.dir}" includes="**/*.jar"/>

   <property name="build.dir" value="build"/>
   <property name="classes.dir" value="${build.dir}/classes"/>
   <property name="jar.dir" value="${build.dir}/jar"/>

   <property name="main-class" value="${}"/>

   <target name="clean">
       <delete dir="${build.dir}"/>

   <target name="compile">
       <mkdir dir="${classes.dir}"/>
       <javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>

   <target name="jar" depends="compile">
       <mkdir dir="${jar.dir}"/>
       <jar destfile="${jar.dir}/${}.jar" basedir="${classes.dir}">
               <attribute name="Main-Class" value="${main-class}"/>

   <target name="run" depends="jar">
       <java fork="true" classname="${main-class}">
           <sysproperty key="java.library.path" path="${ocvLibDir}"/>
               <path refid="classpath"/>
               <path location="${jar.dir}/${}.jar"/>

   <target name="rebuild" depends="clean,jar"/>

   <target name="rebuild-run" depends="clean,run"/>


import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.highgui.*;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.objdetect.CascadeClassifier;


* Created by dmalav on 4/30/15.
public class DetectFaces {

   public void run(String imageFile) {
       System.out.println("\nRunning DetectFaceDemo");

       // Create a face detector from the cascade file in the resources
       // directory.
       String xmlPath = "/home/cloudera/project/opencv-examples/lbpcascade_frontalface.xml";
       CascadeClassifier faceDetector = new CascadeClassifier(xmlPath);
       Mat image = Highgui.imread(imageFile);

       // Detect faces in the image.
       // MatOfRect is a special container class for Rect.
       MatOfRect faceDetections = new MatOfRect();
       faceDetector.detectMultiScale(image, faceDetections);

       System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

       // Draw a bounding box around each face.
       for (Rect 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));

       File f = new File(imageFile);
       // Save the visualized detection.
       String filename = f.getName();
       System.out.println(String.format("Writing %s", filename));
       Highgui.imwrite(filename, image);


import org.opencv.core.Core;


public class Main {

   public static void main(String... args) {


if (args.length == 0) {
   System.err.println("Usage Main /path/to/images");

       File[] files = new File(args[0]).listFiles();

   public static void showFiles(File[] files) {
       DetectFaces faces = new DetectFaces();
       for (File file : files) {
           if (file.isDirectory()) {
               System.out.println("Directory: " + file.getName());
               showFiles(file.listFiles()); // Calls same method again.
           } else {
               System.out.println("File: " + file.getAbsolutePath());

Leave a Reply

Your email address will not be published. Required fields are marked *