add detecting images
This commit is contained in:
parent
27b762ef00
commit
9be0aed2da
@ -2,6 +2,7 @@ package dev.mateuszkowalczyk.ffm.app;
|
||||
|
||||
import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
|
||||
import dev.mateuszkowalczyk.ffm.data.database.photo.PhotoDAO;
|
||||
import dev.mateuszkowalczyk.ffm.image.FaceDetector;
|
||||
import dev.mateuszkowalczyk.ffm.image.ThumbnailService;
|
||||
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
|
||||
import dev.mateuszkowalczyk.ffm.utils.Property;
|
||||
@ -54,6 +55,10 @@ public class DirectoryScanner implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
new Thread(
|
||||
new FaceDetector(photo)
|
||||
).start();
|
||||
|
||||
ImageView imageView = new ImageView(wr);
|
||||
imageViews.add(imageView);
|
||||
|
||||
|
@ -0,0 +1,63 @@
|
||||
package dev.mateuszkowalczyk.ffm.image;
|
||||
|
||||
import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
|
||||
import dev.mateuszkowalczyk.ffm.utils.ResourceLoader;
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfRect;
|
||||
import org.opencv.core.Rect;
|
||||
import org.opencv.objdetect.CascadeClassifier;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.opencv.imgcodecs.Imgcodecs.imread;
|
||||
|
||||
public class FaceDetector implements Runnable {
|
||||
|
||||
private final Photo photo;
|
||||
|
||||
public FaceDetector(Photo photo) {
|
||||
this.photo = photo;
|
||||
}
|
||||
|
||||
static {
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.detectFaces();
|
||||
}
|
||||
|
||||
private void detectFaces() {
|
||||
CascadeClassifier cascadeClassifier = new CascadeClassifier(ResourceLoader.getInstance().getPath("haarcascade_frontalface_alt.xml"));
|
||||
Mat imageMat = imread(this.photo.getPath());
|
||||
MatOfRect matOfRect = new MatOfRect();
|
||||
cascadeClassifier.detectMultiScale(imageMat, matOfRect);
|
||||
|
||||
try {
|
||||
BufferedImage bufferedImage = ImageIO.read(new File(this.photo.getPath()));
|
||||
|
||||
matOfRect.toList().forEach(rect -> {
|
||||
BufferedImage croppedImage = this.crop(bufferedImage, rect);
|
||||
});
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println(String.format("Detected %s faces", matOfRect.toArray().length));
|
||||
}
|
||||
|
||||
private BufferedImage crop(BufferedImage bufferedImage, Rect rect) {
|
||||
BufferedImage dest = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_ARGB_PRE);
|
||||
Graphics g = dest.getGraphics();
|
||||
g.drawImage(bufferedImage, 0, 0, rect.width, rect.height, rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, null);
|
||||
g.dispose();
|
||||
return dest;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user