Skip to content

Commit bb01270

Browse files
authored
Create FaceRecognitionExample.java
1 parent 3af8795 commit bb01270

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

Java/FaceRecognitionExample.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package cv;
2+
3+
/**
4+
* By downloading, copying, installing or using the software you agree to this license.
5+
* If you do not agree to this license, do not download, install,
6+
* copy or use the software.
7+
*
8+
*
9+
* License Agreement
10+
* For Open Source Computer Vision Library
11+
* (3-clause BSD License)
12+
*
13+
* Copyright (C) 2000-2015, Intel Corporation, all rights reserved.
14+
* Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15+
* Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved.
16+
* Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
17+
* Copyright (C) 2015, OpenCV Foundation, all rights reserved.
18+
* Copyright (C) 2015, Itseez Inc., all rights reserved.
19+
* Third party copyrights are property of their respective owners.
20+
*
21+
* Redistribution and use in source and binary forms, with or without modification,
22+
* are permitted provided that the following conditions are met:
23+
*
24+
* * Redistributions of source code must retain the above copyright notice,
25+
* this list of conditions and the following disclaimer.
26+
*
27+
* * Redistributions in binary form must reproduce the above copyright notice,
28+
* this list of conditions and the following disclaimer in the documentation
29+
* and/or other materials provided with the distribution.
30+
*
31+
* * Neither the names of the copyright holders nor the names of the contributors
32+
* may be used to endorse or promote products derived from this software
33+
* without specific prior written permission.
34+
*
35+
* This software is provided by the copyright holders and contributors "as is" and
36+
* any express or implied warranties, including, but not limited to, the implied
37+
* warranties of merchantability and fitness for a particular purpose are disclaimed.
38+
* In no event shall copyright holders or contributors be liable for any direct,
39+
* indirect, incidental, special, exemplary, or consequential damages
40+
* (including, but not limited to, procurement of substitute goods or services;
41+
* loss of use, data, or profits; or business interruption) however caused
42+
* and on any theory of liability, whether in contract, strict liability,
43+
* or tort (including negligence or otherwise) arising in any way out of
44+
* the use of this software, even if advised of the possibility of such damage.
45+
*/
46+
47+
import javax.swing.*;
48+
import java.awt.*;
49+
import java.awt.event.ActionEvent;
50+
import java.awt.event.ActionListener;
51+
import java.awt.image.BufferedImage;
52+
import java.io.File;
53+
54+
public class FaceRecognitionExample extends JFrame implements ActionListener {
55+
56+
private JPanel imagePanel;
57+
private JLabel imageLabel;
58+
private final JButton loadImage;
59+
private final JButton exit;
60+
private final JFileChooser fileChooser;
61+
private final FaceDetector faceDetector;
62+
63+
public FaceRecognitionExample() {
64+
imagePanel = new JPanel();
65+
imageLabel = new JLabel();
66+
loadImage = new JButton("Load Image from disk");
67+
exit = new JButton("Exit");
68+
fileChooser = new JFileChooser();
69+
faceDetector = new FaceDetector();
70+
71+
loadImage.addActionListener(this);
72+
exit.addActionListener(this);
73+
JPanel buttonPanel = new JPanel();
74+
buttonPanel.add(loadImage);
75+
buttonPanel.add(exit);
76+
imagePanel.add(imageLabel);
77+
78+
this.add(imagePanel, BorderLayout.CENTER);
79+
this.add(buttonPanel, BorderLayout.PAGE_END);
80+
this.setMinimumSize(new Dimension(600, 500));
81+
this.setLocationRelativeTo(null);
82+
this.setVisible(true);
83+
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
84+
}
85+
86+
public void actionPerformed(ActionEvent e) {
87+
if (e.getSource() == loadImage) {
88+
int returnVal = fileChooser.showOpenDialog(FaceRecognitionExample.this);
89+
90+
if (returnVal == JFileChooser.APPROVE_OPTION) {
91+
File image = fileChooser.getSelectedFile();
92+
drawImage(image);
93+
}
94+
} else if (e.getSource() == exit) {
95+
System.exit(0);
96+
}
97+
}
98+
99+
private void drawImage(File image) {
100+
BufferedImage modifiedImageWithRectaglesDrawnAroundFaces = faceDetector.detectFace(image.getPath());
101+
imageLabel.setIcon(new ImageIcon(modifiedImageWithRectaglesDrawnAroundFaces));
102+
imagePanel.revalidate();
103+
this.pack();
104+
}
105+
106+
public static void main(String... args) {
107+
new FaceRecognitionExample();
108+
}
109+
}

0 commit comments

Comments
 (0)