Trial 11022025

This commit is contained in:
2025-02-11 09:10:36 +07:00
parent 30ef123832
commit 00f9852fa8
29 changed files with 596 additions and 281 deletions

View File

@@ -104,10 +104,18 @@ public class Cameradetail {
private final UMat BestMat = new UMat();
private final UMat LiveMat = new UMat();
private final UMat ReducedMat = new UMat();
private @Getter Rect BestMatROI;
private @Getter Rect ReducedMatROI;
private @Getter Rect LiveMatROI;
private Size LiveSize = new Size(640, 360);
private Size ReducedSize = new Size(1280, 720);
private Size BestSize = new Size(1920, 1080);
//TODO ini angka dari Erha, cek apakah masih cocok atau tidak
private @Getter @Setter Size FullCropSize = new Size(1036,1036);
public int getBestWidth(){
return BestSize.width();
}
@@ -116,6 +124,8 @@ public class Cameradetail {
return BestSize.height();
}
private void setSliderValue(Slider sld, CameraProperty prop, double value){
if (sld!=null){
sld.setMin(prop.Min);
@@ -531,12 +541,15 @@ public class Cameradetail {
* Take Photo from Camera
* @param directory directory to save the photo, if null, will use default directory
* @param prefix filename prefix
* @param ROI_Full Region of Interest for Full Quality Photo. If Null, will not save cropped photo
* @param ROI_Compressed Region of Interest for Compressed Photo. If Null, will not save cropped photo
* @return filename path of the saved photo, or null if failed
*/
@SuppressWarnings("BusyWait")
public String TakePhoto(String directory, String prefix) throws InterruptedException {
public String TakePhoto(String directory, String prefix, Rect ROI_Full, Rect ROI_Compressed) throws InterruptedException {
String result = null;
if (!ValidDirectory(directory)) directory = currentDirectory;
if (mGrabber!=null){
while(IsGrabbingLiveView.get()){
Thread.sleep(10);
@@ -562,18 +575,36 @@ public class Cameradetail {
int[] parampng = {opencv_imgcodecs.IMWRITE_PNG_COMPRESSION, 0};
// save BestMat at quality 9 PNG
String filename = Path.of(directory, makeFileName(prefix,".png")).toString();
String filename = Path.of(directory, "FullQuality", makeFileName(prefix,".png")).toString();
if (imwrite(filename, BestMat, parampng)){
raise_log("TakePhoto success, Photo saved to " + filename);
result = filename;
} else raise_log("TakePhoto failed, Unable to Save Photo");
} else System.out.println("TakePhoto failed, Unable to Save FullQUality Photo for camera "+cameratitle.getText());
if (ROIInsideUMat(ROI_Full, BestMat)){
// save ROI_Full at 100% JPEG
String roifilename = Path.of(directory, "FullQualityCrop", makeFileName(prefix,".png")).toString();
UMat ROI = new UMat(BestMat, ROI_Full);
if (!imwrite(roifilename, ROI, paramjpeg)){
System.out.println("TakePhoto failed, Unable to Save FullQUalityCrop for camera "+cameratitle.getText());
}
} else System.out.println("ROI_Full is outside of BestMat for camera "+cameratitle.getText());
// save ReducedMat at 100% JPEG
String reducedfilename = Path.of(directory, makeReducedFileName(prefix,".jpg")).toString();
String reducedfilename = Path.of(directory, "Compressed", makeReducedFileName(prefix,".jpg")).toString();
opencv_imgproc.resize(BestMat, ReducedMat, ReducedSize);
if (imwrite(reducedfilename, ReducedMat, paramjpeg)){
raise_log("TakePhoto success, Reduced Photo saved to " + reducedfilename);
} else raise_log("TakePhoto failed, Unable to Save Reduced Photo");
if (!imwrite(reducedfilename, ReducedMat, paramjpeg)){
System.out.println("TakePhoto failed, Unable to Save Reduced Photo for camera "+cameratitle.getText());
}
if (ROIInsideUMat(ROI_Compressed, ReducedMat)){
// save ROI_Compressed at 100% JPEG
String roifilename = Path.of(directory, "CompressedCrop", makeReducedFileName(prefix,".jpg")).toString();
UMat ROI = new UMat(ReducedMat, ROI_Compressed);
if (!imwrite(roifilename, ROI, paramjpeg)){
System.out.println("TakePhoto failed, Unable to Save CompressedCrop for camera "+cameratitle.getText());
}
} else System.out.println("ROI_Compressed is outside of ReducedMat for camera "+cameratitle.getText());
} else raise_log("TakePhoto failed, Live View is Empty");
} else raise_log("TakePhoto failed, Grabber is null");
@@ -642,7 +673,8 @@ public class Cameradetail {
protected Image call() {
// repeat until capturing is false
AtomicInteger fps = new AtomicInteger(0);
boolean eye_was_closed = false;
// eye state = -1 means unknown, 0 means closed, 1 means open
int eye_state = -1;
boolean waiting_for_second_blink = false;
long last_blink = 0;
@@ -667,6 +699,7 @@ public class Cameradetail {
int _face_height;
boolean have_palm = false;
boolean have_fist = false;
long no_face_counter = 0;
while (Capturing.get()) {
try {
@@ -690,11 +723,11 @@ public class Cameradetail {
opencv_imgproc.cvtColor(LiveMat,graymat, COLOR_BGR2GRAY); // convert to grayscale
if (use_qr){
String qr = DetectQRFromMat(graymat);
if (qr!=null) {
if (ValidPatientID(qr)){
if (!qr.equals(qrtext)){
qrtext = qr;
raise_log("QR Detected: " + qr);
if (event!=null) event.onDetectedQRCode(qr);
raise_log("QR Detected: " + qrtext);
if (event!=null) event.onDetectedQRCode(qrtext);
}
}
}
@@ -709,7 +742,7 @@ public class Cameradetail {
if (!frontalfaces.isEmpty()){
for(DetectorResult rect : frontalfaces){
if (rect.haveFace()){
if (rect.haveFace() ){
rect.FaceRectangle(LiveMat);
rect.EyesRectangle(LiveMat);
if (rect.getFaceWidth()>_face_width) _face_width = rect.getFaceWidth();
@@ -737,61 +770,79 @@ public class Cameradetail {
}
if (have_frontal_face){
no_face_counter = 0;
if (event!=null) event.onFrontalFaceDetector(true, _face_width, _face_height);
LiveMatROI = theface.getFace();
if (theface.haveEyes()){
// ada mata (buka mata)
if (eye_was_closed){
eye_was_closed = false;
if (eye_state==0){
// transisi dari tutup mata ke buka mata
//System.out.println("Eye Open Detected from camera "+cameratitle);
long now = System.currentTimeMillis();
if (waiting_for_second_blink){
long diff = now - last_blink;
// kalau beda waktu antara blink 1 dan blink 2 kurang dari 2 detik
if (diff<=2000){
// kalau beda waktu antara blink 1 dan blink 2 kurang dari 10 detik
if (diff<=10000){
waiting_for_second_blink = false;
System.out.println("Blink Detected from camera "+cameratitle);
if (event!=null) event.onBlink(2);
//System.out.println("Double Blink Detected from camera "+cameratitle);
if (event!=null) event.onBlink((int)diff);
}
} else {
last_blink = now;
waiting_for_second_blink = true;
//System.out.println("First Blink Detected from camera "+cameratitle);
}
last_blink = now;
}
eye_state = 1;
} else {
// tidak ada mata
// ada muka, tidak ada mata
// transisi dari buka mata ke tutup mata
if (!eye_was_closed){
eye_was_closed = true;
if (eye_state!=0){
//System.out.println("Eye Closed Detected from camera "+cameratitle);
}
eye_state = 0;
}
} else if (have_left_45_face ){
no_face_counter = 0;
LiveMatROI = theface.getFace();
if (event!=null) event.onProfileFaceDetector(true, _face_width, _face_height);
} else {
// no face at all
// no face detected, but lets not cancel the previous state immediately
if (no_face_counter>30){
// kalau tidak ada face selama 30 frame, reset state
// 30 frame approximately 2 second
eye_state = -1;
last_blink = 0;
waiting_for_second_blink = false;
if (event!=null) {
event.onFrontalFaceDetector(false, _face_width, _face_height);
event.onProfileFaceDetector(false, _face_width, _face_height);
}
} else no_face_counter++;
if (event!=null) {
event.onFrontalFaceDetector(false, _face_width, _face_height);
event.onProfileFaceDetector(false, _face_width, _face_height);
}
}
}
if (HavePalm(graymat)) {
if (!have_palm){
have_fist = false;
have_palm = true;
System.out.println("Palm Detected from camera " + cameratitle);
}
}
if (HaveFist(graymat)) {
if (!have_fist) {
have_palm = false;
have_fist = true;
System.out.println("Fist Detected from camera "+cameratitle);
}
}
// if (HavePalm(graymat)) {
// if (!have_palm){
// have_fist = false;
// have_palm = true;
// System.out.println("Palm Detected from camera " + cameratitle);
// }
// }
// if (HaveFist(graymat)) {
// if (!have_fist) {
// have_palm = false;
// have_fist = true;
// System.out.println("Fist Detected from camera "+cameratitle);
// }
// }
UMat rgbmat = new UMat(LiveMat.size(), CV_8UC3);
cvtColor(LiveMat, rgbmat, COLOR_BGR2RGB);
@@ -829,6 +880,27 @@ public class Cameradetail {
return false;
}
public void RemapROI(){
if (ValidROI(LiveMatROI)){
if (ROIInsideUMat(LiveMatROI, LiveMat)){
System.out.println("LiveMatROI camera "+cameratitle.getText()+" = "+RectToString(LiveMatROI));
double scaleXBest = 1.0*BestSize.width()/LiveSize.width();
double scaleYBest = 1.0*BestSize.height()/LiveSize.height();
double scaleXReduced = 1.0*ReducedSize.width()/LiveSize.width();
double scaleYReduced = 1.0*ReducedSize.height()/LiveSize.height();
BestMatROI = ScaleRect(LiveMatROI, scaleXBest, scaleYBest);
ReducedMatROI = ScaleRect(LiveMatROI, scaleXReduced, scaleYReduced);
} else {
System.out.println("LiveMatROI is Outside LiveMat for camera "+cameratitle.getText());
LiveMatROI = null;
}
} else System.out.println("LiveMatROI is invalid for camera "+cameratitle.getText());
}
/**
* Detect QR Code from Mat
* @param graymat Mat in Gray Scale