-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.java
97 lines (84 loc) · 2.92 KB
/
App.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.FaceDetection;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;
public class App {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
//imgProcessor proc;
private JFrame frame;
private JLabel imageLabel;
private CascadeClassifier faceDetector;
public static void main(String[] args) {
App app = new App();
app.initGUI();//A method for initializing GUI components
app.loadCascade();//Loading the cascades classifiers
app.runMainLoop(args);
}
private void loadCascade() {
String cascadePath = "D:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_alt.xml";//path for the cascade classifier
faceDetector = new CascadeClassifier(cascadePath);
}
private void initGUI() {
// Loading gui components
frame = new JFrame("Face detection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
imageLabel = new JLabel();
frame.add(imageLabel);
frame.setVisible(true);
}
private void runMainLoop(String[] args) {
imgProcessor imageProcessor = new imgProcessor();
Mat webcamImage = new Mat();
Image tempImage;
VideoCapture capture = new VideoCapture(0);
capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640);
capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480);
if(capture.isOpened()) {
while(true) {
capture.read(webcamImage);
if(!webcamImage.empty()) {
detectAndDrawFace(webcamImage);
tempImage = imageProcessor.toBufferImage(webcamImage);//Convert image to buffer from our imgProcess class
ImageIcon imageIcon= new ImageIcon(tempImage, "Captured Video");
imageLabel.setIcon(imageIcon);
frame.pack();
}
else {
JOptionPane.showMessageDialog(frame, "Error ----- could not capture frames");
}
}
}
else {
JOptionPane.showMessageDialog(frame, "Could not open webcam");
}
}
private void detectAndDrawFace(Mat image) {
// Detect face and draw
MatOfRect faceDetection = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetection,1.1,7,0,new Size(50,20),new Size());
//Draw a box around each face
for(Rect rect : faceDetection.toArray()) {
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width,rect.y + rect.height),
new Scalar(0,255,0));
//Put text around the rectangle
Imgproc.putText(image, "Face", new Point(rect.x + rect.height, rect.y + rect.width / 10),
1,1,new Scalar(124,252,0),1);
}
}
}