OpenCV and Java

This is adapted from: http://dinesh-malav.blogspot.com/2015/05/image-processing-using-opencv-on-hadoop.html

build.xml

<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"/>
   </path>


   <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="${ant.project.name}"/>


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


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


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


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


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


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


</project>

DetectFaces.java

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;


import java.io.File;


/**
* 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";
       System.out.println(xmlPath);
       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);
       System.out.println(f.getName());
       // Save the visualized detection.
       String filename = f.getName();
       System.out.println(String.format("Writing %s", filename));
       Highgui.imwrite(filename, image);


   }
}

Main.java

import org.opencv.core.Core;


import java.io.File;


public class Main {


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


       System.loadLibrary(Core.NATIVE_LIBRARY_NAME);


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


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


   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());
               faces.run(file.getAbsolutePath());
           }
       }
   }
}

Leave a Reply

Your email address will not be published.