forked from hellonico/opencv4_java_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaceDetection.java
More file actions
149 lines (130 loc) · 5.37 KB
/
Copy pathFaceDetection.java
File metadata and controls
149 lines (130 loc) · 5.37 KB
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
package webcam;
/*
* Captures the camera stream with OpenCV
* Search for the faces
* Display a circle around the faces using Java
* https://udallascs.wordpress.com/2014/04/01/face-detection-with-a-webcam-using-only-opencv-with-java-and-not-javacv/
*/
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
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.scijava.nativelib.NativeLoader;
class My_Panel extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage image;
// Create a constructor method
public My_Panel() {
super();
}
/**
* Converts/writes a Mat into a BufferedImage.
*
* @param matrix Mat of type CV_8UC3 or CV_8UC1
* @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
*/
public boolean MatToBufferedImage(Mat matBGR) {
long startTime = System.nanoTime();
int width = matBGR.width(), height = matBGR.height(), channels = matBGR.channels();
byte[] sourcePixels = new byte[width * height * channels];
matBGR.get(0, 0, sourcePixels);
// create new image and get reference to backing data
image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);
long endTime = System.nanoTime();
System.out.println(String.format("Elapsed time: %.2f ms", (float) (endTime - startTime) / 1000000));
return true;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.image == null)
return;
g.drawImage(this.image, 10, 10, 2 * this.image.getWidth(), 2 * this.image.getHeight(), null);
// g.drawString("This is my custom Panel!",10,20);
}
}
class processor {
private CascadeClassifier face_cascade;
// Create a constructor method
public processor() {
face_cascade = new CascadeClassifier("data/haarcascades/haarcascade_frontalface_alt.xml");
if (face_cascade.empty()) {
System.out.println("--(!)Error loading A\n");
return;
} else {
System.out.println("Face classifier loooaaaaaded up");
}
}
public Mat detect(Mat inputframe) {
long startTime = System.nanoTime();
Mat mRgba = new Mat();
Mat mGrey = new Mat();
MatOfRect faces = new MatOfRect();
inputframe.copyTo(mRgba);
inputframe.copyTo(mGrey);
Imgproc.cvtColor(mRgba, mGrey, Imgproc.COLOR_BGR2GRAY);
Imgproc.equalizeHist(mGrey, mGrey);
face_cascade.detectMultiScale(mGrey, faces);
long endTime = System.nanoTime();
System.out.println(String.format("Detect time: %.2f ms", (float) (endTime - startTime) / 1000000));
System.out.println(String.format("Detected %s faces", faces.toArray().length));
for (Rect rect : faces.toArray()) {
Point center = new Point(rect.x + rect.width * 0.5, rect.y + rect.height * 0.5);
Imgproc.ellipse(mRgba, center, new Size(rect.width * 0.5, rect.height * 0.5), 0, 0, 360,
new Scalar(255, 0, 255), 4, 8, 0);
}
return mRgba;
}
}
public class FaceDetection {
public static void main(String arg[]) throws IOException {
NativeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String window_name = "Capture - Face detection";
JFrame frame = new JFrame(window_name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
processor my_processor = new processor();
My_Panel my_panel = new My_Panel();
frame.setContentPane(my_panel);
frame.setVisible(true);
// -- 2. Read the video stream
Mat webcam_image = new Mat();
VideoCapture capture = new VideoCapture(0);
// capture.set(Video., value)
if (capture.isOpened()) {
// capture.set(Videoio.CV_CAP_PROP_FRAME_WIDTH, 300);
// capture.set(Videoio.CV_CAP_PROP_FRAME_HEIGHT, 200);
while (true) {
capture.read(webcam_image);
// Mat resizeimage = new Mat();
Size sz = new Size(300, 200);
Imgproc.resize(webcam_image, webcam_image, sz);
if (!webcam_image.empty()) {
frame.setSize(2 * webcam_image.width() + 40, 2 * webcam_image.height() + 60);
// -- 3. Apply the classifier to the captured image
webcam_image = my_processor.detect(webcam_image);
// -- 4. Display the image
my_panel.MatToBufferedImage(webcam_image); // We could look at the error...
my_panel.repaint();
} else {
System.out.println(" --(!) No captured frame -- Break!");
break;
}
}
}
return;
}
}