forked from bytedeco/javacv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaceApplet.java
152 lines (140 loc) · 5.62 KB
/
FaceApplet.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.applet.Applet;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import org.bytedeco.opencv.opencv_objdetect.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_objdetect.*;
/**
*
* @author Samuel Audet
*/
public class FaceApplet extends Applet implements Runnable {
private CvHaarClassifierCascade classifier = null;
private CvMemStorage storage = null;
private FrameGrabber grabber = null;
private IplImage grabbedImage = null, grayImage = null, smallImage = null;
private CvSeq faces = null;
private boolean stop = false;
private Exception exception = null;
OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
Java2DFrameConverter paintConverter = new Java2DFrameConverter();
@Override public void init() {
try {
// Load the classifier file from Java resources.
String classiferName = "haarcascade_frontalface_alt.xml";
File classifierFile = Loader.extractResource(classiferName, null, "classifier", ".xml");
if (classifierFile == null || classifierFile.length() <= 0) {
throw new IOException("Could not extract \"" + classiferName + "\" from Java resources.");
}
// Preload the opencv_objdetect module to work around a known bug.
Loader.load(opencv_objdetect.class);
classifier = new CvHaarClassifierCascade(cvLoad(classifierFile.getAbsolutePath()));
classifierFile.delete();
if (classifier.isNull()) {
throw new IOException("Could not load the classifier file.");
}
storage = CvMemStorage.create();
} catch (Exception e) {
if (exception == null) {
exception = e;
repaint();
}
}
}
@Override public void start() {
try {
new Thread(this).start();
} catch (Exception e) {
if (exception == null) {
exception = e;
repaint();
}
}
}
public void run() {
try {
try {
grabber = FrameGrabber.createDefault(0);
grabber.setImageWidth(getWidth());
grabber.setImageHeight(getHeight());
grabber.start();
grabbedImage = grabberConverter.convert(grabber.grab());
} catch (Exception e) {
if (grabber != null) grabber.release();
grabber = new OpenCVFrameGrabber(0);
grabber.setImageWidth(getWidth());
grabber.setImageHeight(getHeight());
grabber.start();
grabbedImage = grabberConverter.convert(grabber.grab());
}
grayImage = IplImage.create(grabbedImage.width(), grabbedImage.height(), IPL_DEPTH_8U, 1);
smallImage = IplImage.create(grabbedImage.width()/4, grabbedImage.height()/4, IPL_DEPTH_8U, 1);
stop = false;
while (!stop && (grabbedImage = grabberConverter.convert(grabber.grab())) != null) {
if (faces == null) {
cvClearMemStorage(storage);
cvCvtColor(grabbedImage, grayImage, CV_BGR2GRAY);
cvResize(grayImage, smallImage, CV_INTER_AREA);
faces = cvHaarDetectObjects(smallImage, classifier, storage, 1.1, 3,
CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH);
repaint();
}
}
grabbedImage = grayImage = smallImage = null;
grabber.stop();
grabber.release();
grabber = null;
} catch (Exception e) {
if (exception == null) {
exception = e;
repaint();
}
}
}
@Override public void update(Graphics g) {
paint(g);
}
@Override public void paint(Graphics g) {
if (grabbedImage != null) {
Frame frame = grabberConverter.convert(grabbedImage);
BufferedImage image = paintConverter.getBufferedImage(frame, 2.2/grabber.getGamma());
Graphics2D g2 = image.createGraphics();
if (faces != null) {
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(2));
int total = faces.total();
for (int i = 0; i < total; i++) {
CvRect r = new CvRect(cvGetSeqElem(faces, i));
g2.drawRect(r.x()*4, r.y()*4, r.width()*4, r.height()*4);
}
faces = null;
}
g.drawImage(image, 0, 0, null);
}
if (exception != null) {
int y = 0, h = g.getFontMetrics().getHeight();
g.drawString(exception.toString(), 5, y += h);
for (StackTraceElement e : exception.getStackTrace()) {
g.drawString(" at " + e.toString(), 5, y += h);
}
}
}
@Override public void stop() {
stop = true;
}
@Override public void destroy() { }
}