add change face to another person

This commit is contained in:
Mateusz Kowalczyk 2020-01-31 22:38:26 +01:00
parent 8a747a5982
commit 73a921e175
10 changed files with 142 additions and 13 deletions

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class DirectoryScanner implements Runnable {
private WorkspaceService workspaceService = WorkspaceService.getInstance();
@ -82,11 +83,29 @@ public class DirectoryScanner implements Runnable {
}
private boolean isImagePath(String filename) {
switch (filename.substring(filename.length() - 3).toLowerCase()) {
case "jpg":
List<String> whiteListExtension = new ArrayList<String>();
whiteListExtension.add("jpg");
whiteListExtension.add("jpeg");
whiteListExtension.add("png");
for (String extension: whiteListExtension) {
var fileExtension = filename.substring(filename.length() - extension.length());
if (fileExtension.toLowerCase().equals(extension)){
return true;
default:
}
}
return false;
}
// filename.endsWith("jpeg");
//
// switch (filename.substring(filename.length() - 3).toLowerCase()) {
// case "jpeg":
// case "jpg":
// return true;
// default:
// return false;
// }
}
}

View File

@ -4,8 +4,10 @@ import dev.mateuszkowalczyk.ffm.app.exception.NotFoundException;
import dev.mateuszkowalczyk.ffm.data.database.face.Face;
import dev.mateuszkowalczyk.ffm.data.database.face.FaceDAO;
import dev.mateuszkowalczyk.ffm.data.database.person.Person;
import dev.mateuszkowalczyk.ffm.data.database.person.PersonDAO;
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
import dev.mateuszkowalczyk.ffm.view.workspace.MainPageController;
import dev.mateuszkowalczyk.ffm.view.workspace.elements.ElementsEnum;
import dev.mateuszkowalczyk.ffm.view.workspace.elements.FaceContainerController;
import dev.mateuszkowalczyk.ffm.view.workspace.elements.FacePaneController;
import javafx.fxml.FXMLLoader;
@ -22,6 +24,7 @@ public class FaceWorkspace {
public void loadFacesForPerson(MainPageController mainPageController, FaceContainerController faceContainerController, Person person) throws NotFoundException {
List<Face> faceList = FaceDAO.getInstance().getAllForPerson(person);
List<Person> personList = PersonDAO.getInstance().getAll(true);
var resourceLoader = ResourceLoader.getInstance();
if (faceList.size() == 0) {
@ -31,7 +34,9 @@ public class FaceWorkspace {
for (Face face : faceList) {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(resourceLoader.getResource("templates/workspace/elements/face_pane.fxml"));
fxmlLoader.setController(new FacePaneController(mainPageController, faceContainerController, face));
var controller = new FacePaneController(mainPageController, faceContainerController, face);
controller.setPersonList(personList);
fxmlLoader.setController(controller);
try {
faceContainerController.addFacePane(fxmlLoader.load());

View File

@ -89,7 +89,18 @@ public class FaceDAO implements Dao<Face> {
@Override
public void update(Face face) {
String sql = "UPDATE face set personId = ? WHERE id = ?";
try {
PreparedStatement preparedStatement = this.databaseService.getConnection().prepareStatement(sql);
preparedStatement.setLong(1, face.getPersonId());
preparedStatement.setLong(2, face.getId());
preparedStatement.executeUpdate();
this.getAll(true);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override

View File

@ -82,7 +82,20 @@ public class PersonDAO implements Dao<Person> {
@Override
public void update(Person person) {
String sql = "UPDATE persons SET name = ? WHERE id = ?";
try {
PreparedStatement preparedStatement = this.databaseService.getConnection().prepareStatement(sql);
preparedStatement.setString(1, person.getName());
preparedStatement.setLong(2, person.getId());
preparedStatement.executeUpdate();
this.getAll(true);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override

View File

@ -16,8 +16,8 @@ public class FaceRecognition {
private FaceDAO faceDAO = FaceDAO.getInstance();
public void recognize(Face faceToRecognize) {
List<Face> faceList = faceDAO.getAll();
List<Person> personList = personDAO.getAll();
List<Face> faceList = faceDAO.getAll(true);
List<Person> personList = personDAO.getAll(true);
ArrayList<Mat> facesImages = new ArrayList<>();
ArrayList<Integer> faceImagesIndex = new ArrayList<>();
Person person;

View File

@ -22,9 +22,9 @@ public class ThumbnailService {
float divider;
if (originalImage.getWidth() > originalImage.getHeight()) {
divider = originalImage.getWidth() / 200;
divider = (float) originalImage.getWidth() / (float) 200;
} else {
divider = originalImage.getHeight() / 200;
divider = (float) originalImage.getHeight() / (float) 200;
}
int width = (int) (originalImage.getWidth() / divider);

View File

@ -2,13 +2,14 @@ package dev.mateuszkowalczyk.ffm.view.workspace.elements;
import dev.mateuszkowalczyk.ffm.app.exception.NotFoundException;
import dev.mateuszkowalczyk.ffm.app.face.FaceWorkspace;
import dev.mateuszkowalczyk.ffm.data.database.face.FaceDAO;
import dev.mateuszkowalczyk.ffm.data.database.person.Person;
import dev.mateuszkowalczyk.ffm.data.database.person.PersonDAO;
import dev.mateuszkowalczyk.ffm.view.workspace.MainPageController;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import java.net.URL;
@ -21,6 +22,9 @@ public class FaceContainerController implements Initializable {
@FXML
private FlowPane facesContainer;
@FXML
private TextField personName;
public FaceContainerController(MainPageController mainPageController, Person person) {
this.mainPageController = mainPageController;
this.person = person;
@ -34,6 +38,13 @@ public class FaceContainerController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
this.refresh();
this.personName.setText(person.getName());
}
public void updatePersonName(Event event) {
this.person.setName(this.personName.getText());
PersonDAO.getInstance().update(person);
}
public void refresh() {

View File

@ -1,25 +1,41 @@
package dev.mateuszkowalczyk.ffm.view.workspace.elements;
import dev.mateuszkowalczyk.ffm.app.face.FaceWorkspace;
import dev.mateuszkowalczyk.ffm.data.database.face.Face;
import dev.mateuszkowalczyk.ffm.data.database.face.FaceDAO;
import dev.mateuszkowalczyk.ffm.data.database.person.Person;
import dev.mateuszkowalczyk.ffm.data.database.person.PersonDAO;
import dev.mateuszkowalczyk.ffm.view.workspace.MainPageController;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
public class FacePaneController implements Initializable {
private MainPageController mainPageController;
private FaceContainerController faceContainerController;
private FaceWorkspace faceWorkspace = FaceWorkspace.getInstance();
private Face face;
private ObservableList<String> list = FXCollections.observableArrayList();
@FXML
private ImageView faceImage;
@FXML
private ChoiceBox<String> choicePerson;
private List<Person> personList;
public FacePaneController(MainPageController mainPageController, FaceContainerController faceContainerController, Face face) {
this.mainPageController = mainPageController;
@ -32,13 +48,47 @@ public class FacePaneController implements Initializable {
this.faceContainerController.refresh();
}
public void markAsNewPerson(Event event) {
Person person = new Person();
PersonDAO.getInstance().add(person);
this.changePerson(person, this.face);
}
private void changePerson(Person person, Face face) {
face.setPersonId(person.getId());
FaceDAO.getInstance().update(face);
PersonDAO.getInstance().getAll(true);
this.faceContainerController.refresh();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
this.loadFace();
this.setChoiceBox();
}
private void setChoiceBox() {
for (Person person: this.personList) {
this.list.add(person.getName());
}
this.choicePerson.setItems(this.list);
}
private void loadFace() {
Image image = new Image("file:" + this.face.getPath());
this.faceImage.setImage(image);
}
public void setPersonList(List<Person> personList) {
this.personList = personList;
}
public void writePersonToFace(Event event) {
var selectedPerson = personList.get(list.indexOf(this.choicePerson.getValue()));
this.changePerson(selectedPerson, this.face);
}
}

View File

@ -1,6 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.FlowPane?>
<FlowPane fx:id="facesContainer" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" />
<BorderPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<center>
<FlowPane fx:id="facesContainer" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" />
</center>
<top>
<ToolBar prefHeight="40.0" prefWidth="3485.0" BorderPane.alignment="CENTER">
<items>
<TextField fx:id="personName" />
<Button mnemonicParsing="false" onAction="#updatePersonName" text="Button" />
</items>
</ToolBar>
</top>
</BorderPane>

View File

@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="250.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Pane layoutX="27.0" layoutY="25.0" prefHeight="200.0" prefWidth="200.0" style="-fx-border-color: grey;">
@ -14,5 +15,9 @@
</children>
</Pane>
<Button layoutX="309.0" layoutY="197.0" mnemonicParsing="false" onAction="#removeThis" text="This is not a face" />
<Button layoutX="323.0" layoutY="25.0" mnemonicParsing="false" onAction="#markAsNewPerson" text="New person" />
<ChoiceBox fx:id="choicePerson" layoutX="248.0" layoutY="76.0" prefWidth="150.0" />
<Label layoutX="322.0" layoutY="58.0" text="Change person" />
<Button layoutX="405.0" layoutY="76.0" mnemonicParsing="false" onAction="#writePersonToFace" text="change" />
</children>
</AnchorPane>