Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Prepare for release.
  • Loading branch information
JakeJMattson committed Jun 23, 2018
1 parent 9cc014e commit 41a4923
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 47 deletions.
2 changes: 1 addition & 1 deletion FacialRecognition/.classpath
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/OpenCV"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Expand Up @@ -141,9 +141,7 @@ public String identifyFace(Mat image)
{
//Local variables
String faceID = "";
int errorThreshold = 3;
int similarities = 0;
int mostSimilar = 0;
int errorThreshold = 3, mostSimilar = 0;

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

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

//Margin of error
if (mostSimilar > errorThreshold)
if (faceID.indexOf(" (") == -1)
faceID = faceID.substring(0, faceID.indexOf(".")).trim();
else
faceID = faceID.substring(0, faceID.indexOf("(")).trim();
{
String delimiter = faceID.indexOf(" (") == -1 ? "." : "(";
faceID = faceID.substring(0, faceID.indexOf(delimiter)).trim();
}
else
faceID = "???";

Expand Down Expand Up @@ -200,14 +198,14 @@ public int compareFaces(Mat currentImage, String fileName)
if (descriptors1.cols() == descriptors2.cols())
{
//Check matches of key points
MatOfDMatch matches = new MatOfDMatch();
MatOfDMatch matchMatrix = new MatOfDMatch();
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
matcher.match(descriptors1, descriptors2, matches);
DMatch[] match = matches.toArray();
matcher.match(descriptors1, descriptors2, matchMatrix);
DMatch[] matches = matchMatrix.toArray();

//Determine similarity
for (int i = 0; i < descriptors1.rows(); i++)
if (match[i].distance <= 50)
for (DMatch match : matches)
if (match.distance <= 50)
similarity++;
}

Expand Down
Expand Up @@ -11,7 +11,7 @@
import org.opencv.imgproc.Imgproc;

/**
* Frame - GUI container for components (holds ImagePanel)
* Frame - GUI container for components (holds ImagePanel).
*
* @author mattson543
*/
Expand All @@ -36,8 +36,7 @@ public class ImageFrame extends JFrame implements ActionListener
private ImagePanel imagePanel;

private JTextField txtFileName;
private JButton btnSaveFile;
private JButton btnSetColor;
private JButton btnSaveFile, btnSetColor;
private JComboBox<String> colorDropDown;

//Class constants
Expand Down Expand Up @@ -99,8 +98,7 @@ private JPanel createToolbarPanel()
{
//Create panels
JPanel toolbarPanel = new JPanel(new FlowLayout());
JPanel savePanel = createSavePanel();
JPanel colorPanel = createColorPanel();
JPanel savePanel = createSavePanel(), colorPanel = createColorPanel();

//Combine panels
toolbarPanel.add(savePanel);
Expand Down Expand Up @@ -227,28 +225,21 @@ public void showImage(Mat image)
}

/**
* Convert OpenCV matrix to native Java BufferedImage.
* Convert an OpenCV Mat to a Java BufferedImage.
*
* @param matrix
* OpenCV matrix
* OpenCV Mat
* @return BufferedImage
*/
public BufferedImage convertMatToImage(Mat matrix)
private BufferedImage convertMatToImage(Mat matrix)
{
//Get image dimensions
int width = matrix.width();
int height = matrix.height();
int width = matrix.width(), height = matrix.height();

//Determine image type
int type;
int type = matrix.channels() != 1 ? BufferedImage.TYPE_3BYTE_BGR : BufferedImage.TYPE_BYTE_GRAY;

if (matrix.channels() != 1)
{
type = BufferedImage.TYPE_3BYTE_BGR;
if (type == BufferedImage.TYPE_3BYTE_BGR)
Imgproc.cvtColor(matrix, matrix, Imgproc.COLOR_BGR2RGB);
}
else
type = BufferedImage.TYPE_BYTE_GRAY;

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

/*
* (non-Javadoc)
* @see
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
* @see java.awt.event.ActionListener
* #actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent click)
{
if (click.getSource() == btnSetColor)
Object src = click.getSource();

if (src == btnSetColor)
try
{
//Get color from string name
Field field = Class.forName("java.awt.Color").getField((String) colorDropDown.getSelectedItem());
Field field = Color.class.getField((String) colorDropDown.getSelectedItem());
color = (Color) field.get(null);
}
catch (NoSuchFieldException | SecurityException | ClassNotFoundException | IllegalArgumentException
| IllegalAccessException e)
catch (NoSuchFieldException | IllegalAccessException e)
{
color = DEFAULT_COLOR;
e.printStackTrace();
}
else if (click.getSource() == btnSaveFile)
else if (src == btnSaveFile)
shouldSave = true;
}
}
Expand Up @@ -6,7 +6,7 @@
import javax.swing.JPanel;

/**
* Panel - holds image to display in GUI
* Panel - holds image to display in GUI.
*
* @author mattson543
*/
Expand Down
Expand Up @@ -8,7 +8,7 @@
import org.opencv.core.Core;

/**
* Load the OpenCV library
* Load the OpenCV library.
*
* @author mattson543
*/
Expand All @@ -22,7 +22,7 @@ public final class LibraryLoader
/**
* Initiate OpenCV loading based on the launch environment.
*
* @return Whether or not the operation was successful
* @return Whether or not OpenCV was loaded
*/
public static boolean loadLibrary()
{
Expand Down Expand Up @@ -268,11 +268,11 @@ private static void createFile(File pathFile, String libraryPath)
private static boolean load(String libraryPath)
{
File libraryFile = new File(libraryPath);

File tempFile = null;

//Create temporary file
try
{
//Create temporary file
String name = libraryFile.getName();
String extension = name.substring(name.indexOf("."));
tempFile = File.createTempFile("lib", extension);
Expand All @@ -289,10 +289,10 @@ private static boolean load(String libraryPath)
return false;
}

//Write library data to file
try (FileInputStream in = new FileInputStream(libraryFile.getAbsolutePath());
OutputStream out = new FileOutputStream(tempFile);)
OutputStream out = new FileOutputStream(tempFile))
{
//Write library data to file
in.transferTo(out);
}
catch (IOException e)
Expand Down

0 comments on commit 41a4923

Please sign in to comment.