-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathface_detection_opencv.py
More file actions
82 lines (69 loc) · 2.57 KB
/
face_detection_opencv.py
File metadata and controls
82 lines (69 loc) · 2.57 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
import javax.swing.*;
import face.util.VideoPanel;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
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.objdetect.Objdetect;
import org.opencv.videoio.VideoCapture;
//Download file haarcascade_frontalface_alt.xml and save in folder OpenCV
//This is file feature extract of face
public class FaceDetection {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
JFrame cameraFrame = new JFrame("camera");
cameraFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cameraFrame.setSize(740, 580);
cameraFrame.setBounds(0, 0, cameraFrame.getWidth(), cameraFrame.getHeight());
VideoPanel videoPanel = new VideoPanel();
cameraFrame.setContentPane(videoPanel);
cameraFrame.setVisible(true);
CascadeClassifier faceCascade = new CascadeClassifier();
faceCascade.load("OpenCV/haarcascades/haarcascade_frontalface_alt.xml");
VideoCapture capture = new VideoCapture();
try {
capture.open(0);
if (capture.isOpened()) {
Mat image = new Mat();
while(true) {
capture.read(image);
if (!image.empty()) {
detectAndDisplay(image, faceCascade);
videoPanel.setImageWithMat(image);
cameraFrame.repaint();
} else {
break;
}
}
}
} finally {
capture.release();
}
}
public static void detectAndDisplay(Mat frame, CascadeClassifier faceCascade)
{
MatOfRect faces = new MatOfRect();
Mat grayFrame = new Mat();
// convert the frame in gray scale
Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
// equalize the frame histogram to improve the result
Imgproc.equalizeHist(grayFrame, grayFrame);
// compute minimum face size (20% of the frame height, in our case)
int absoluteFaceSize = 0;
int height = grayFrame.rows();
if (Math.round(height * 0.2f) > 0) {
absoluteFaceSize = Math.round(height * 0.2f);
}
// detect faces
faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE,
new Size(absoluteFaceSize, absoluteFaceSize), new Size());
// each rectangle in faces is a face: draw them!
Rect[] facesArray = faces.toArray();
for (int i = 0; i < facesArray.length; i++)
Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0), 3);
}
}