Last active
July 19, 2019 06:34
-
-
Save arnold4037/b003ee1975dc2dc450b42e5b122ce8a0 to your computer and use it in GitHub Desktop.
Face Detection System using Java & opencv
This file contains 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
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Author: Arnold Coutinho | |
// | |
// | |
// | |
// | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
package gui; | |
import java.awt.Graphics; | |
import java.awt.Image; | |
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayInputStream; | |
import javax.imageio.ImageIO; | |
import org.opencv.core.Core; | |
import org.opencv.core.Mat; | |
import org.opencv.core.MatOfByte; | |
import org.opencv.core.MatOfRect; | |
import org.opencv.core.Point; | |
import org.opencv.core.Rect; | |
import org.opencv.core.Scalar; | |
import org.opencv.imgcodecs.Imgcodecs; | |
import org.opencv.videoio.VideoCapture; | |
import org.opencv.imgproc.Imgproc; | |
import org.opencv.objdetect.CascadeClassifier; | |
/** | |
* | |
* | |
*/ | |
public class FaceDetection extends javax.swing.JFrame { | |
/// | |
private DaemonThread myThread = null; | |
int count = 0; | |
VideoCapture webSource = null; | |
Mat frame = new Mat(); | |
MatOfByte mem = new MatOfByte(); | |
CascadeClassifier faceDetector = new CascadeClassifier(FaceDetection.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1).replaceAll("%20", " ")); | |
MatOfRect faceDetections = new MatOfRect(); | |
/// | |
class DaemonThread implements Runnable { | |
protected volatile boolean runnable = false; | |
@Override | |
public void run() { | |
synchronized (this) { | |
while (runnable) { | |
if (webSource.grab()) { | |
try { | |
webSource.retrieve(frame); | |
Graphics g = jPanel1.getGraphics(); | |
faceDetector.detectMultiScale(frame, faceDetections); | |
for (Rect rect : faceDetections.toArray()) { | |
// System.out.println("ttt"); | |
Imgproc.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), | |
new Scalar(0, 255,0)); | |
} | |
Imgcodecs.imencode(".bmp", frame, mem); | |
Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray())); | |
BufferedImage buff = (BufferedImage) im; | |
if (g.drawImage(buff, 0, 0, getWidth(), getHeight()-150 , 0, 0, buff.getWidth(), buff.getHeight(), null)) { | |
if (runnable == false) { | |
System.out.println("Paused ..... "); | |
this.wait(); | |
} | |
} | |
} catch (Exception ex) { | |
System.out.println("Error"); | |
} | |
} | |
} | |
} | |
} | |
} | |
///////// | |
/** | |
* Creates new form FaceDetection | |
*/ | |
public FaceDetection() { | |
initComponents(); | |
System.out.println(FaceDetection.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1)); | |
} | |
/** | |
* This method is called from within the constructor to initialize the form. | |
* WARNING: Do NOT modify this code. The content of this method is always | |
* regenerated by the Form Editor. | |
*/ | |
@SuppressWarnings("unchecked") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code"> | |
private void initComponents() { | |
jPanel1 = new javax.swing.JPanel(); | |
jButton1 = new javax.swing.JButton(); | |
jButton2 = new javax.swing.JButton(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); | |
jPanel1.setLayout(jPanel1Layout); | |
jPanel1Layout.setHorizontalGroup( | |
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGap(0, 0, Short.MAX_VALUE) | |
); | |
jPanel1Layout.setVerticalGroup( | |
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGap(0, 373, Short.MAX_VALUE) | |
); | |
jButton1.setText("Start"); | |
jButton1.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButton1ActionPerformed(evt); | |
} | |
}); | |
jButton2.setText("Pause"); | |
jButton2.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButton2ActionPerformed(evt); | |
} | |
}); | |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
getContentPane().setLayout(layout); | |
layout.setHorizontalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addGap(62, 62, 62) | |
.addComponent(jButton1) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 399, Short.MAX_VALUE) | |
.addComponent(jButton2) | |
.addGap(39, 39, 39)) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap() | |
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addContainerGap()) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap() | |
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(jButton1) | |
.addComponent(jButton2)) | |
.addGap(24, 24, 24)) | |
); | |
pack(); | |
}// </editor-fold> | |
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { | |
myThread.runnable = false; // stop thread | |
jButton2.setEnabled(false); // activate start button | |
jButton1.setEnabled(true); // deactivate stop button | |
webSource.release(); // stop caturing fron cam | |
} | |
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { | |
webSource = new VideoCapture(0); // video capture from default cam | |
myThread = new DaemonThread(); //create object of threat class | |
Thread t = new Thread(myThread); | |
t.setDaemon(true); | |
myThread.runnable = true; | |
t.start(); //start thrad | |
jButton1.setEnabled(false); // deactivate start button | |
jButton2.setEnabled(true); // activate stop button | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String args[]) { | |
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |
/* Set the Nimbus look and feel */ | |
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
*/ | |
try { | |
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
if ("Nimbus".equals(info.getName())) { | |
javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
break; | |
} | |
} | |
} catch (ClassNotFoundException ex) { | |
java.util.logging.Logger.getLogger(FaceDetection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (InstantiationException ex) { | |
java.util.logging.Logger.getLogger(FaceDetection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (IllegalAccessException ex) { | |
java.util.logging.Logger.getLogger(FaceDetection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
java.util.logging.Logger.getLogger(FaceDetection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} | |
//</editor-fold> | |
/* Create and display the form */ | |
java.awt.EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
new FaceDetection().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify | |
private javax.swing.JButton jButton1; | |
private javax.swing.JButton jButton2; | |
private javax.swing.JPanel jPanel1; | |
// End of variables declaration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1)open netbeans open new project named FacialDetection do not create main class open new Jframe named FacialDetection and copy code.
2)add (haarcascade_frontalface_alt.xml) file from your opencv "C:\opencv\sources\data\haarcascades" along with your FaceDetection.java
3)add opencv libraries from C:\opencv\build\java
4)Click on FacialDetection go to properties click on run and in vm options add this -Djava.library.path="C:\opencv\opencv\build\java\x64"