add reading all faces from database

This commit is contained in:
Mateusz Kowalczyk 2020-01-24 11:45:44 +01:00
parent 8387243c60
commit 06848bb2d2
7 changed files with 97 additions and 8 deletions

View File

@ -51,7 +51,7 @@ public class DirectoryScanner implements Runnable {
photo.setFileName(filename); photo.setFileName(filename);
BufferedImage image = this.thumbnailService.createThumbnail(photo); BufferedImage image = this.thumbnailService.createThumbnail(photo);
this.photoDAO.save(photo); this.photoDAO.add(photo);
WritableImage wr = null; WritableImage wr = null;
if (image != null) { if (image != null) {

View File

@ -1,6 +1,9 @@
package dev.mateuszkowalczyk.ffm.app.cache; package dev.mateuszkowalczyk.ffm.app.cache;
import dev.mateuszkowalczyk.ffm.data.database.face.Face; import dev.mateuszkowalczyk.ffm.data.database.face.Face;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@ -9,13 +12,25 @@ import java.io.IOException;
public class FacesCacheService implements Runnable { public class FacesCacheService implements Runnable {
protected static final String DIRECTORY_NAME = "/faces"; protected static final String DIRECTORY_NAME = "/faces";
private final BufferedImage faceImage; private CacheService cacheService = CacheService.getInstance();
private BufferedImage faceImage;
private String path; private String path;
public FacesCacheService(BufferedImage face) { public FacesCacheService(BufferedImage face) {
this.faceImage = face; this.faceImage = face;
} }
public FacesCacheService() {
}
public Mat readFaceToProcess(String name) {
Mat image = Imgcodecs.imread(this.cacheService.getPath() + DIRECTORY_NAME + "/" + name + ".JPG");
Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY);
return image;
}
@Override @Override
public void run() { public void run() {

View File

@ -1,11 +1,14 @@
package dev.mateuszkowalczyk.ffm.data.database; package dev.mateuszkowalczyk.ffm.data.database;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public interface Dao<T> { public interface Dao<T> {
Optional<T> get(long id); Optional<T> get(long id);
void save(T t); List<T> getAll();
List<T> getAll(boolean refresh);
void add(T t);
void update(T t); void update(T t);
void delete (T t); void delete (T t);

View File

@ -1,9 +1,13 @@
package dev.mateuszkowalczyk.ffm.data.database.face; package dev.mateuszkowalczyk.ffm.data.database.face;
import dev.mateuszkowalczyk.ffm.app.cache.FacesCacheService;
import dev.mateuszkowalczyk.ffm.data.database.annotation.Column; import dev.mateuszkowalczyk.ffm.data.database.annotation.Column;
import dev.mateuszkowalczyk.ffm.data.database.annotation.PrimaryKey; import dev.mateuszkowalczyk.ffm.data.database.annotation.PrimaryKey;
import dev.mateuszkowalczyk.ffm.data.database.annotation.Table; import dev.mateuszkowalczyk.ffm.data.database.annotation.Table;
import org.opencv.core.Mat;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID; import java.util.UUID;
@Table @Table
@ -21,10 +25,19 @@ public class Face {
@Column(type = Column.Type.INT) @Column(type = Column.Type.INT)
private long photoId; private long photoId;
private Mat faceToProcess;
public Face() { public Face() {
this.name = UUID.randomUUID().toString(); this.name = UUID.randomUUID().toString();
} }
public Face(ResultSet resultSet) throws SQLException {
this.id = resultSet.getInt("id");
this.name = resultSet.getString("name");
this.path = resultSet.getString("path");
this.photoId = resultSet.getInt("photoId");
}
public long getPhotoId() { public long getPhotoId() {
return photoId; return photoId;
} }
@ -56,4 +69,17 @@ public class Face {
public void setPath(String path) { public void setPath(String path) {
this.path = path; this.path = path;
} }
public Mat getFaceToProcess() {
if (this.faceToProcess == null) {
FacesCacheService facesCacheService = new FacesCacheService();
this.faceToProcess = facesCacheService.readFaceToProcess(this.name);
}
return faceToProcess;
}
public void setFaceToProcess(Mat faceToProcess) {
this.faceToProcess = faceToProcess;
}
} }

View File

@ -6,11 +6,14 @@ import dev.mateuszkowalczyk.ffm.data.database.Dao;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public class FaceDAO implements Dao<Face> { public class FaceDAO implements Dao<Face> {
private static FaceDAO instance; private static FaceDAO instance;
private DatabaseService databaseService = DatabaseService.getInstance(); private DatabaseService databaseService = DatabaseService.getInstance();
private List<Face> faceList = new ArrayList<>();
private FaceDAO() { private FaceDAO() {
} }
@ -29,7 +32,35 @@ public class FaceDAO implements Dao<Face> {
} }
@Override @Override
public void save(Face face) { public List<Face> getAll() {
return this.getAll(false);
}
@Override
public List<Face> getAll(boolean refresh) {
if (this.faceList.size() == 0 || refresh) {
this.faceList.clear();
String sql = "SELECT * FROM face";
try {
ResultSet resultSet = this.databaseService.getConnection().prepareStatement(sql).executeQuery();
while (resultSet.next()) {
Face face = new Face(resultSet);
this.faceList.add(face);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return this.faceList;
}
@Override
public void add(Face face) {
String sql = "INSERT INTO face (name, path, photoId) values (?, ?, ?)"; String sql = "INSERT INTO face (name, path, photoId) values (?, ?, ?)";
try { try {
@ -47,6 +78,8 @@ public class FaceDAO implements Dao<Face> {
face.setId(resultSet.getInt("id")); face.setId(resultSet.getInt("id"));
} }
this.faceList.add(face);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -6,6 +6,7 @@ import dev.mateuszkowalczyk.ffm.data.database.Dao;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public class PhotoDAO implements Dao<Photo> { public class PhotoDAO implements Dao<Photo> {
@ -29,7 +30,17 @@ public class PhotoDAO implements Dao<Photo> {
} }
@Override @Override
public void save(Photo photo) { public List<Photo> getAll() {
return null;
}
@Override
public List<Photo> getAll(boolean refresh) {
return null;
}
@Override
public void add(Photo photo) {
String sql = "INSERT INTO photos(path, fileName, cachedPath) values (?, ?, ?)"; String sql = "INSERT INTO photos(path, fileName, cachedPath) values (?, ?, ?)";

View File

@ -9,6 +9,7 @@ import org.opencv.core.Core;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import org.opencv.core.MatOfRect; import org.opencv.core.MatOfRect;
import org.opencv.core.Rect; import org.opencv.core.Rect;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier; import org.opencv.objdetect.CascadeClassifier;
@ -39,7 +40,7 @@ public class FaceDetector implements Runnable {
private void detectFaces() { private void detectFaces() {
CascadeClassifier cascadeClassifier = new CascadeClassifier(ResourceLoader.getInstance().getPath("haarcascade_frontalface_alt2.xml")); CascadeClassifier cascadeClassifier = new CascadeClassifier(ResourceLoader.getInstance().getPath("haarcascade_frontalface_alt2.xml"));
Mat imageMat = imread(this.photo.getPath()); Mat imageMat = Imgcodecs.imread(this.photo.getPath());
Mat imageGrey = new Mat(); Mat imageGrey = new Mat();
Imgproc.cvtColor(imageMat, imageGrey, Imgproc.COLOR_RGB2GRAY); Imgproc.cvtColor(imageMat, imageGrey, Imgproc.COLOR_RGB2GRAY);
@ -57,7 +58,7 @@ public class FaceDetector implements Runnable {
FacesCacheService facesCacheService = new FacesCacheService(croppedImage); FacesCacheService facesCacheService = new FacesCacheService(croppedImage);
facesCacheService.getPath(face); facesCacheService.getPath(face);
facesCacheService.createCachedFace(); facesCacheService.createCachedFace();
this.faceDAO.save(face); this.faceDAO.add(face);
}); });
} catch (IOException e) { } catch (IOException e) {