FFM-Friend_Face_Matching/src/main/java/dev/mateuszkowalczyk/ffm/app/cache/FacesCacheService.java

75 lines
2.0 KiB
Java
Raw Normal View History

2020-01-23 18:50:38 +01:00
package dev.mateuszkowalczyk.ffm.app.cache;
import dev.mateuszkowalczyk.ffm.data.database.face.Face;
2020-01-24 11:45:44 +01:00
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
2020-01-23 18:50:38 +01:00
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class FacesCacheService implements Runnable {
protected static final String DIRECTORY_NAME = "/faces";
2020-01-24 11:45:44 +01:00
private CacheService cacheService = CacheService.getInstance();
private BufferedImage faceImage;
2020-01-23 18:50:38 +01:00
private String path;
2020-01-24 11:45:44 +01:00
public FacesCacheService(BufferedImage face) {
2020-01-23 18:50:38 +01:00
this.faceImage = face;
}
2020-01-24 11:45:44 +01:00
public FacesCacheService() {
}
2020-01-24 23:04:07 +01:00
public Mat readFaceToProcess(Face face) {
Mat image = Imgcodecs.imread(face.getPath());
Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY);
return image;
}
2020-01-24 11:45:44 +01:00
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;
}
2020-01-23 18:50:38 +01:00
@Override
public void run() {
}
public void getPath(Face face) {
this.path = CacheService.getInstance().getPath() + DIRECTORY_NAME + "/" + face.getName() + ".PNG";
face.setPath(this.path);
}
public void createCachedFace() {
if (faceImage != null && this.path != null) {
File imageFile = new File(this.path);
try {
ImageIO.write(this.faceImage, "PNG", imageFile);
System.out.println(String.format("Saved on location: %s", imageFile.getAbsolutePath()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
2020-01-30 20:54:46 +01:00
public BufferedImage getFaceImage(Face face) {
File file = new File(face.getPath());
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
2020-01-23 18:50:38 +01:00
}