Improvement 21/01/2025
This commit is contained in:
@@ -23,8 +23,10 @@ import javafx.scene.image.WritableImage;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import org.bytedeco.javacv.Frame;
|
||||
import org.bytedeco.javacv.OpenCVFrameGrabber;
|
||||
import org.bytedeco.opencv.global.opencv_core;
|
||||
import org.bytedeco.opencv.global.opencv_imgcodecs;
|
||||
import org.bytedeco.opencv.global.opencv_imgproc;
|
||||
import org.bytedeco.opencv.opencv_core.*;
|
||||
@@ -42,6 +44,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static Config.SomeCodes.*;
|
||||
import static id.co.gtc.erhacam.Detectors.faceDetector;
|
||||
import static id.co.gtc.erhacam.Detectors.eyeDetector;
|
||||
import static org.bytedeco.opencv.global.opencv_core.CV_8UC3;
|
||||
import static org.bytedeco.opencv.global.opencv_core.mean;
|
||||
import static org.bytedeco.opencv.global.opencv_imgcodecs.imwrite;
|
||||
@@ -49,6 +53,15 @@ import static org.bytedeco.opencv.global.opencv_imgproc.*;
|
||||
|
||||
@SuppressWarnings({"unused"})
|
||||
public class Cameradetail {
|
||||
private static final boolean isCudaAvailable ;
|
||||
static{
|
||||
isCudaAvailable = opencv_core.getCudaEnabledDeviceCount()>0;
|
||||
if (isCudaAvailable){
|
||||
System.out.println("CUDA is available");
|
||||
} else {
|
||||
System.out.println("CUDA is not available");
|
||||
}
|
||||
}
|
||||
private final AtomicBoolean Capturing = new AtomicBoolean(false);
|
||||
private final AtomicBoolean TakingPhoto = new AtomicBoolean(false);
|
||||
private final AtomicBoolean IsGrabbingLiveView = new AtomicBoolean(false);
|
||||
@@ -87,7 +100,6 @@ public class Cameradetail {
|
||||
@FXML
|
||||
private Slider exposureSlider;
|
||||
|
||||
|
||||
private final UMat BestMat = new UMat();
|
||||
private final UMat LiveMat = new UMat();
|
||||
private final UMat ReducedMat = new UMat();
|
||||
@@ -95,6 +107,14 @@ public class Cameradetail {
|
||||
private Size ReducedSize = new Size(1280, 720);
|
||||
private Size BestSize = new Size(1920, 1080);
|
||||
|
||||
public int getBestWidth(){
|
||||
return BestSize.width();
|
||||
}
|
||||
|
||||
public int getBestHeight(){
|
||||
return BestSize.height();
|
||||
}
|
||||
|
||||
private void setSliderValue(Slider sld, CameraProperty prop, double value){
|
||||
if (sld!=null){
|
||||
sld.setMin(prop.Min);
|
||||
@@ -621,6 +641,9 @@ public class Cameradetail {
|
||||
protected Image call() {
|
||||
// repeat until capturing is false
|
||||
AtomicInteger fps = new AtomicInteger(0);
|
||||
AtomicBoolean eye_was_closed = new AtomicBoolean(false);
|
||||
AtomicInteger blink_counter = new AtomicInteger(0);
|
||||
|
||||
TimerTask timerTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -640,8 +663,8 @@ public class Cameradetail {
|
||||
IsGrabbingLiveView.set(true);
|
||||
Frame frame = mGrabber.grab(); // grab frame
|
||||
Mat mat = matconverter.convert(frame); // convert to Mat
|
||||
if (mat.empty()) continue; // kalau gak ada data, continue
|
||||
int counter = fps.incrementAndGet();
|
||||
if (mat.empty()) continue; // kalau gak ada data, continue
|
||||
if (counter % 5 != 0) continue; // frame skip to 6 fps
|
||||
|
||||
mat.copyTo(BestMat); // copy to BestMat for using OpenCL
|
||||
@@ -664,12 +687,47 @@ public class Cameradetail {
|
||||
if (use_face){
|
||||
RectVector face = DetectFace(graymat);
|
||||
if (face!=null && face.size()>0){
|
||||
// ada muka
|
||||
if (event!=null) event.onFaceDetector(true, BestSize.width(), BestSize.height());
|
||||
for(int i=0; i<face.size(); i++){
|
||||
Rect rect = face.get(i);
|
||||
rectangle(LiveMat, rect, Scalar.GREEN);
|
||||
}
|
||||
} else if (event!=null) event.onFaceDetector(false, BestSize.width(), BestSize.height());
|
||||
|
||||
// kalau detect muka, baru coba detect mata
|
||||
RectVector eye = DetectEye(graymat);
|
||||
if (eye!=null && eye.size()>=2){
|
||||
// ada mata
|
||||
if (eye_was_closed.get()){
|
||||
eye_was_closed.set(false);
|
||||
if (blink_counter.get()>=2){
|
||||
blink_counter.set(0);
|
||||
if (event!=null) event.onBlink(blink_counter.get());
|
||||
}
|
||||
}
|
||||
|
||||
if (event!=null) event.onEyeDetector(true, BestSize.width(), BestSize.height());
|
||||
for(int i=0; i<eye.size(); i++){
|
||||
Rect rect = eye.get(i);
|
||||
rectangle(LiveMat, rect, Scalar.RED);
|
||||
}
|
||||
} else {
|
||||
// mata tidak ada
|
||||
if (!eye_was_closed.get()){
|
||||
eye_was_closed.set(true);
|
||||
blink_counter.incrementAndGet();
|
||||
}
|
||||
if (event!=null) event.onEyeDetector(false, BestSize.width(), BestSize.height());
|
||||
}
|
||||
|
||||
} else {
|
||||
eye_was_closed.set(false);
|
||||
blink_counter.set(0);
|
||||
// tidak ada muka, tidak ada mata
|
||||
if (event!=null) event.onFaceDetector(false, BestSize.width(), BestSize.height());
|
||||
if (event!=null) event.onEyeDetector(false, BestSize.width(), BestSize.height());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -747,6 +805,15 @@ public class Cameradetail {
|
||||
return null;
|
||||
}
|
||||
|
||||
private RectVector DetectEye(UMat graymat){
|
||||
if (eyeDetector!=null){
|
||||
RectVector eye = new RectVector();
|
||||
eyeDetector.detectMultiScale(graymat, eye);
|
||||
return eye;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private double getBrightnessFromGrayMat(Mat graymat){
|
||||
Scalar mean = mean(graymat);
|
||||
return mean.get(0);
|
||||
|
||||
Reference in New Issue
Block a user