/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cameradetection;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import static com.googlecode.javacv.cpp.opencv_core.CV_AA;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_GAUSSIAN;
import static com.googlecode.javacv.cpp.opencv_core.cvLoad;
import static com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;
import static com.googlecode.javacv.cpp.opencv_core.cvPoint;
import static com.googlecode.javacv.cpp.opencv_core.cvRectangle;
/**
*
* @author duo
*/
public class CameraDetection {
protected String detectionXmlFile;
protected CanvasFrame canvasFrame;
public CameraDetection() {
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
try {
grabber.start();
IplImage frame = grabber.grab();
canvasFrame = new CanvasFrame("Camera Detecção");
canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvasFrame.setCanvasSize(frame.width(), frame.height());
CvMemStorage storage = CvMemStorage.create();
while(detectionXmlFile==null || detectionXmlFile.isEmpty()) {
chooseXmlDetectionFile();
}
int detectionCount = 0;
CvSeq faces = null;
while (canvasFrame.isVisible() && (frame = grabber.grab()) != null) {
cvSmooth(frame, frame, CV_GAUSSIAN, 9, 9, 2, 2);
detectionCount++;
if (detectionCount > 5) {
detectionCount=0;
IplImage grayImage = null;
try {
grayImage = IplImage.create(frame.width(),
frame.height(), IPL_DEPTH_8U, 1);
} catch (NullPointerException e) {
System.out.println("Error: " + e);
System.out.println("Check image file!");
System.exit(1);
}
// We convert the original image to grayscale.
cvCvtColor(frame, grayImage, CV_BGR2GRAY);
// We instantiate a classifier cascade to be used for detection, using
// the cascade definition.
CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(
cvLoad(detectionXmlFile));
// We detectionExecute the faces.
faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1,
0);
}
if (faces!=null) {
// We iterate over the discovered faces and draw yellow rectangles
// around them.
for (int i = 0; i < faces.total(); i++) {
CvRect r = new CvRect(cvGetSeqElem(faces, i));
cvRectangle(frame, cvPoint(r.x(), r.y()),
cvPoint(r.x() + r.width(), r.y() + r.height()),
CvScalar.YELLOW, 1, CV_AA, 0);
}
}
canvasFrame.showImage(frame);
}
} catch (Exception ex) {
Logger.getLogger(CameraDetection.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Get the value of detectionXmlFile
*
* @return the value of detectionXmlFile
*/
public String getDetectionXmlFile() {
return detectionXmlFile;
}
/**
* Set the value of detectionXmlFile
*
* @param detectionXmlFile new value of detectionXmlFile
*/
public void setDetectionXmlFile(String detectionXmlFile) {
this.detectionXmlFile = detectionXmlFile;
}
public void chooseXmlDetectionFile() {
setDetectionXmlFile(null);
String path = new File(".").getAbsolutePath();
System.out.println("path: " + path);
JFileChooser chooser = new JFileChooser(path + "/resources");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Haarscascade xml detection", "xml");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(canvasFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
setDetectionXmlFile(chooser.getSelectedFile().getAbsolutePath());
System.out.println("You have decided to cascade: "
+ getDetectionXmlFile());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
CameraDetection cameraDetection = new CameraDetection();
}
}