Skip to content

Commit 41a4923

Browse files
committed
Cleanup
Prepare for release.
1 parent 9cc014e commit 41a4923

File tree

5 files changed

+36
-47
lines changed

5 files changed

+36
-47
lines changed

FacialRecognition/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="src" path="src"/>
43
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4+
<classpathentry kind="src" path="src"/>
55
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/OpenCV"/>
66
<classpathentry kind="output" path="bin"/>
77
</classpath>

FacialRecognition/src/io/github/mattson543/facialrecognition/FacialRecognition.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ public String identifyFace(Mat image)
141141
{
142142
//Local variables
143143
String faceID = "";
144-
int errorThreshold = 3;
145-
int similarities = 0;
146-
int mostSimilar = 0;
144+
int errorThreshold = 3, mostSimilar = 0;
147145

148146
//Refresh files
149147
File[] captures = DATABASE.listFiles();
@@ -152,7 +150,7 @@ public String identifyFace(Mat image)
152150
for (File capture : captures)
153151
{
154152
//Calculate similarity between face on screen and face in database
155-
similarities = compareFaces(image, capture.getAbsolutePath());
153+
int similarities = compareFaces(image, capture.getAbsolutePath());
156154

157155
//Find most similar face in list
158156
if (similarities > mostSimilar)
@@ -166,10 +164,10 @@ public String identifyFace(Mat image)
166164

167165
//Margin of error
168166
if (mostSimilar > errorThreshold)
169-
if (faceID.indexOf(" (") == -1)
170-
faceID = faceID.substring(0, faceID.indexOf(".")).trim();
171-
else
172-
faceID = faceID.substring(0, faceID.indexOf("(")).trim();
167+
{
168+
String delimiter = faceID.indexOf(" (") == -1 ? "." : "(";
169+
faceID = faceID.substring(0, faceID.indexOf(delimiter)).trim();
170+
}
173171
else
174172
faceID = "???";
175173

@@ -200,14 +198,14 @@ public int compareFaces(Mat currentImage, String fileName)
200198
if (descriptors1.cols() == descriptors2.cols())
201199
{
202200
//Check matches of key points
203-
MatOfDMatch matches = new MatOfDMatch();
201+
MatOfDMatch matchMatrix = new MatOfDMatch();
204202
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
205-
matcher.match(descriptors1, descriptors2, matches);
206-
DMatch[] match = matches.toArray();
203+
matcher.match(descriptors1, descriptors2, matchMatrix);
204+
DMatch[] matches = matchMatrix.toArray();
207205

208206
//Determine similarity
209-
for (int i = 0; i < descriptors1.rows(); i++)
210-
if (match[i].distance <= 50)
207+
for (DMatch match : matches)
208+
if (match.distance <= 50)
211209
similarity++;
212210
}
213211

FacialRecognition/src/io/github/mattson543/facialrecognition/ImageFrame.java

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.opencv.imgproc.Imgproc;
1212

1313
/**
14-
* Frame - GUI container for components (holds ImagePanel)
14+
* Frame - GUI container for components (holds ImagePanel).
1515
*
1616
* @author mattson543
1717
*/
@@ -36,8 +36,7 @@ public class ImageFrame extends JFrame implements ActionListener
3636
private ImagePanel imagePanel;
3737

3838
private JTextField txtFileName;
39-
private JButton btnSaveFile;
40-
private JButton btnSetColor;
39+
private JButton btnSaveFile, btnSetColor;
4140
private JComboBox<String> colorDropDown;
4241

4342
//Class constants
@@ -99,8 +98,7 @@ private JPanel createToolbarPanel()
9998
{
10099
//Create panels
101100
JPanel toolbarPanel = new JPanel(new FlowLayout());
102-
JPanel savePanel = createSavePanel();
103-
JPanel colorPanel = createColorPanel();
101+
JPanel savePanel = createSavePanel(), colorPanel = createColorPanel();
104102

105103
//Combine panels
106104
toolbarPanel.add(savePanel);
@@ -227,28 +225,21 @@ public void showImage(Mat image)
227225
}
228226

229227
/**
230-
* Convert OpenCV matrix to native Java BufferedImage.
228+
* Convert an OpenCV Mat to a Java BufferedImage.
231229
*
232230
* @param matrix
233-
* OpenCV matrix
231+
* OpenCV Mat
234232
* @return BufferedImage
235233
*/
236-
public BufferedImage convertMatToImage(Mat matrix)
234+
private BufferedImage convertMatToImage(Mat matrix)
237235
{
238236
//Get image dimensions
239-
int width = matrix.width();
240-
int height = matrix.height();
237+
int width = matrix.width(), height = matrix.height();
241238

242-
//Determine image type
243-
int type;
239+
int type = matrix.channels() != 1 ? BufferedImage.TYPE_3BYTE_BGR : BufferedImage.TYPE_BYTE_GRAY;
244240

245-
if (matrix.channels() != 1)
246-
{
247-
type = BufferedImage.TYPE_3BYTE_BGR;
241+
if (type == BufferedImage.TYPE_3BYTE_BGR)
248242
Imgproc.cvtColor(matrix, matrix, Imgproc.COLOR_BGR2RGB);
249-
}
250-
else
251-
type = BufferedImage.TYPE_BYTE_GRAY;
252243

253244
//Get matrix data
254245
byte[] data = new byte[width * height * (int) matrix.elemSize()];
@@ -263,26 +254,26 @@ public BufferedImage convertMatToImage(Mat matrix)
263254

264255
/*
265256
* (non-Javadoc)
266-
* @see
267-
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
257+
* @see java.awt.event.ActionListener
258+
* #actionPerformed(java.awt.event.ActionEvent)
268259
*/
269260
@Override
270261
public void actionPerformed(ActionEvent click)
271262
{
272-
if (click.getSource() == btnSetColor)
263+
Object src = click.getSource();
264+
265+
if (src == btnSetColor)
273266
try
274267
{
275268
//Get color from string name
276-
Field field = Class.forName("java.awt.Color").getField((String) colorDropDown.getSelectedItem());
269+
Field field = Color.class.getField((String) colorDropDown.getSelectedItem());
277270
color = (Color) field.get(null);
278271
}
279-
catch (NoSuchFieldException | SecurityException | ClassNotFoundException | IllegalArgumentException
280-
| IllegalAccessException e)
272+
catch (NoSuchFieldException | IllegalAccessException e)
281273
{
282274
color = DEFAULT_COLOR;
283-
e.printStackTrace();
284275
}
285-
else if (click.getSource() == btnSaveFile)
276+
else if (src == btnSaveFile)
286277
shouldSave = true;
287278
}
288279
}

FacialRecognition/src/io/github/mattson543/facialrecognition/ImagePanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import javax.swing.JPanel;
77

88
/**
9-
* Panel - holds image to display in GUI
9+
* Panel - holds image to display in GUI.
1010
*
1111
* @author mattson543
1212
*/

FacialRecognition/src/io/github/mattson543/facialrecognition/LibraryLoader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.opencv.core.Core;
99

1010
/**
11-
* Load the OpenCV library
11+
* Load the OpenCV library.
1212
*
1313
* @author mattson543
1414
*/
@@ -22,7 +22,7 @@ public final class LibraryLoader
2222
/**
2323
* Initiate OpenCV loading based on the launch environment.
2424
*
25-
* @return Whether or not the operation was successful
25+
* @return Whether or not OpenCV was loaded
2626
*/
2727
public static boolean loadLibrary()
2828
{
@@ -268,11 +268,11 @@ private static void createFile(File pathFile, String libraryPath)
268268
private static boolean load(String libraryPath)
269269
{
270270
File libraryFile = new File(libraryPath);
271-
272271
File tempFile = null;
272+
273+
//Create temporary file
273274
try
274275
{
275-
//Create temporary file
276276
String name = libraryFile.getName();
277277
String extension = name.substring(name.indexOf("."));
278278
tempFile = File.createTempFile("lib", extension);
@@ -289,10 +289,10 @@ private static boolean load(String libraryPath)
289289
return false;
290290
}
291291

292+
//Write library data to file
292293
try (FileInputStream in = new FileInputStream(libraryFile.getAbsolutePath());
293-
OutputStream out = new FileOutputStream(tempFile);)
294+
OutputStream out = new FileOutputStream(tempFile))
294295
{
295-
//Write library data to file
296296
in.transferTo(out);
297297
}
298298
catch (IOException e)

0 commit comments

Comments
 (0)