upgrade face and photo saving to database
This commit is contained in:
parent
d6c6621b28
commit
1923ec1801
@ -21,7 +21,7 @@ public class DirectoryScanner implements Runnable {
|
||||
private WorkspaceService workspaceService = WorkspaceService.getInstance();
|
||||
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
||||
private ThumbnailService thumbnailService = new ThumbnailService();
|
||||
private PhotoDAO photoDAO = new PhotoDAO();
|
||||
private PhotoDAO photoDAO = PhotoDAO.getInstance();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -18,10 +18,21 @@ public class Face {
|
||||
@Column
|
||||
private String path;
|
||||
|
||||
@Column(type = Column.Type.INT)
|
||||
private long photoId;
|
||||
|
||||
public Face() {
|
||||
this.name = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public long getPhotoId() {
|
||||
return photoId;
|
||||
}
|
||||
|
||||
public void setPhotoId(long photoId) {
|
||||
this.photoId = photoId;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -1,10 +1,27 @@
|
||||
package dev.mateuszkowalczyk.ffm.data.database.face;
|
||||
|
||||
import dev.mateuszkowalczyk.ffm.data.DatabaseService;
|
||||
import dev.mateuszkowalczyk.ffm.data.database.Dao;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
|
||||
public class FaceDAO implements Dao<Face> {
|
||||
private static FaceDAO instance;
|
||||
private DatabaseService databaseService = DatabaseService.getInstance();
|
||||
|
||||
private FaceDAO() {
|
||||
}
|
||||
|
||||
public static FaceDAO getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new FaceDAO();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Face> get(long id) {
|
||||
@ -13,7 +30,26 @@ public class FaceDAO implements Dao<Face> {
|
||||
|
||||
@Override
|
||||
public void save(Face face) {
|
||||
String sql = "INSERT INTO face (name, path, photoId) values (?, ?, ?)";
|
||||
|
||||
try {
|
||||
PreparedStatement preparedStatement = this.databaseService.getConnection().prepareStatement(sql);
|
||||
preparedStatement.setString(1, face.getName());
|
||||
preparedStatement.setString(2, face.getPath());
|
||||
preparedStatement.setLong(3, face.getPhotoId());
|
||||
preparedStatement.executeUpdate();
|
||||
|
||||
sql = "SELECT id FROM face ORDER BY id DESC LIMIT 1";
|
||||
|
||||
ResultSet resultSet = this.databaseService.getConnection().prepareStatement(sql).executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
face.setId(resultSet.getInt("id"));
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -29,6 +29,10 @@ public class Photo {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
@ -4,12 +4,25 @@ import dev.mateuszkowalczyk.ffm.data.DatabaseService;
|
||||
import dev.mateuszkowalczyk.ffm.data.database.Dao;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
|
||||
public class PhotoDAO implements Dao<Photo> {
|
||||
private static PhotoDAO instance;
|
||||
private DatabaseService databaseService = DatabaseService.getInstance();
|
||||
|
||||
private PhotoDAO() {
|
||||
}
|
||||
|
||||
public static PhotoDAO getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new PhotoDAO();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Photo> get(long id) {
|
||||
return Optional.empty();
|
||||
@ -18,12 +31,22 @@ public class PhotoDAO implements Dao<Photo> {
|
||||
@Override
|
||||
public void save(Photo photo) {
|
||||
|
||||
String sql = "INSERT INTO photos(path) values (?)";
|
||||
String sql = "INSERT INTO photos(path, fileName, cachedPath) values (?, ?, ?)";
|
||||
|
||||
try {
|
||||
PreparedStatement preparedStatement = this.databaseService.getConnection().prepareStatement(sql);
|
||||
preparedStatement.setString(1, photo.getPath());
|
||||
preparedStatement.setString(2, photo.getFileName());
|
||||
preparedStatement.setString(3, photo.getCachedPath());
|
||||
preparedStatement.executeUpdate();
|
||||
sql = "select id from photos order by id desc limit 1";
|
||||
|
||||
ResultSet resultSet = this.databaseService.getConnection().prepareStatement(sql).executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
photo.setId(resultSet.getInt("id"));
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package dev.mateuszkowalczyk.ffm.data.database.utils;
|
||||
|
||||
import dev.mateuszkowalczyk.ffm.data.database.face.Face;
|
||||
import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
|
||||
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
|
||||
|
||||
@ -32,5 +33,6 @@ public class DatabaseCreator {
|
||||
TableCreator creator = new TableCreator(connection);
|
||||
|
||||
creator.create(Photo.class);
|
||||
creator.create(Face.class);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package dev.mateuszkowalczyk.ffm.image;
|
||||
|
||||
import dev.mateuszkowalczyk.ffm.app.cache.FacesCacheService;
|
||||
import dev.mateuszkowalczyk.ffm.data.database.face.Face;
|
||||
import dev.mateuszkowalczyk.ffm.data.database.face.FaceDAO;
|
||||
import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
|
||||
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
|
||||
import org.opencv.core.Core;
|
||||
@ -19,7 +20,7 @@ import java.io.IOException;
|
||||
import static org.opencv.imgcodecs.Imgcodecs.imread;
|
||||
|
||||
public class FaceDetector implements Runnable {
|
||||
|
||||
private FaceDAO faceDAO = FaceDAO.getInstance();
|
||||
private final Photo photo;
|
||||
|
||||
public FaceDetector(Photo photo) {
|
||||
@ -47,10 +48,11 @@ public class FaceDetector implements Runnable {
|
||||
matOfRect.toList().forEach(rect -> {
|
||||
BufferedImage croppedImage = this.crop(bufferedImage, rect);
|
||||
Face face = new Face();
|
||||
face.setPhotoId(photo.getId());
|
||||
FacesCacheService facesCacheService = new FacesCacheService(croppedImage);
|
||||
facesCacheService.getPath(face);
|
||||
facesCacheService.createCachedFace();
|
||||
|
||||
this.faceDAO.save(face);
|
||||
});
|
||||
|
||||
} catch (IOException e) {
|
||||
|
Loading…
Reference in New Issue
Block a user