Commit 18022025

This commit is contained in:
2025-02-18 15:30:02 +07:00
parent 00f9852fa8
commit 64f5b619b7
32 changed files with 1777 additions and 229 deletions

View File

@@ -5,6 +5,7 @@ import Camera.LiveCamEvent;
import Camera.ObsbotMeet2Preset;
import Config.CameraConfigEnum;
import Config.SomeCodes;
import ErhaAPI.PhotoResult;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
@@ -109,9 +110,14 @@ public class Cameradetail {
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);
// private Size LiveSize = new Size(640, 360);
// private Size ReducedSize = new Size(1280, 720);
// private Size BestSize = new Size(1920, 1080);
// putar portrait
private Size LiveSize = new Size(360, 640);
private Size ReducedSize = new Size(720, 1280);
private Size BestSize = new Size(2160, 3840);
//TODO ini angka dari Erha, cek apakah masih cocok atau tidak
private @Getter @Setter Size FullCropSize = new Size(1036,1036);
@@ -345,15 +351,28 @@ public class Cameradetail {
* @param photoheight Height used on photo capture
* @param reducedwidth Width used on reduced resolution
* @param reducedheight Height used on reduced resolution
* @param isPotrait if true, set to portrait mode, otherwise landscape
*/
public void SetGrabber(OpenCVFrameGrabber grabber, int livewidth, int liveheight, int photowidth, int photoheight, int reducedwidth, int reducedheight){
public void SetGrabber(OpenCVFrameGrabber grabber, int livewidth, int liveheight, int photowidth, int photoheight, int reducedwidth, int reducedheight, boolean isPotrait){
if (mGrabber!=null) {
StopLiveView();
}
LiveSize = new Size(livewidth, liveheight);
BestSize = new Size(photowidth, photoheight);
ReducedSize = new Size(reducedwidth, reducedheight);
if (isPotrait){
// putar portrait
LiveSize = new Size(liveheight, livewidth);
BestSize = new Size(photoheight, photowidth);
ReducedSize = new Size(reducedheight, reducedwidth);
} else {
LiveSize = new Size(livewidth, liveheight);
BestSize = new Size(photowidth, photoheight);
ReducedSize = new Size(reducedwidth, reducedheight);
}
mGrabber = grabber;
}
//Exposure and Focus Tricks :
@@ -545,9 +564,8 @@ public class Cameradetail {
* @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, Rect ROI_Full, Rect ROI_Compressed) throws InterruptedException {
String result = null;
public PhotoResult TakePhoto(String directory, String prefix, Rect ROI_Full, Rect ROI_Compressed) throws InterruptedException {
PhotoResult result = new PhotoResult(cameratitle.getText());
if (!ValidDirectory(directory)) directory = currentDirectory;
if (mGrabber!=null){
@@ -577,33 +595,31 @@ public class Cameradetail {
// save BestMat at quality 9 PNG
String filename = Path.of(directory, "FullQuality", makeFileName(prefix,".png")).toString();
if (imwrite(filename, BestMat, parampng)){
result = filename;
result.setFullres(filename);
} 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
UMat FullCrop = CropUMat(BestMat, ROI_Full);
if (FullCrop!=null){
String roifilename = Path.of(directory, "FullQualityCrop", makeFileName(prefix,".png")).toString();
UMat ROI = new UMat(BestMat, ROI_Full);
if (!imwrite(roifilename, ROI, paramjpeg)){
if (!imwrite(roifilename, FullCrop, parampng)){
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());
} else result.setFullcrop(roifilename);
}
// save ReducedMat at 100% JPEG
String reducedfilename = Path.of(directory, "Compressed", makeReducedFileName(prefix,".jpg")).toString();
opencv_imgproc.resize(BestMat, ReducedMat, ReducedSize);
if (!imwrite(reducedfilename, ReducedMat, paramjpeg)){
System.out.println("TakePhoto failed, Unable to Save Reduced Photo for camera "+cameratitle.getText());
}
} else result.setCompressedfile(reducedfilename);
if (ROIInsideUMat(ROI_Compressed, ReducedMat)){
// save ROI_Compressed at 100% JPEG
UMat CompressedCrop = CropUMat(ReducedMat, ROI_Compressed);
if (CompressedCrop!=null){
String roifilename = Path.of(directory, "CompressedCrop", makeReducedFileName(prefix,".jpg")).toString();
UMat ROI = new UMat(ReducedMat, ROI_Compressed);
if (!imwrite(roifilename, ROI, paramjpeg)){
if (!imwrite(roifilename, CompressedCrop, 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 result.setCompressedcrop(roifilename);
}
} else raise_log("TakePhoto failed, Live View is Empty");
@@ -699,7 +715,8 @@ public class Cameradetail {
int _face_height;
boolean have_palm = false;
boolean have_fist = false;
long no_face_counter = 0;
int no_face_counter = 0;
int face_counter = 0;
while (Capturing.get()) {
try {
@@ -710,23 +727,30 @@ public class Cameradetail {
if (!Capturing.get()) return null;
IsGrabbingLiveView.set(true);
Frame frame = mGrabber.grab(); // grab frame
Frame frame=null;
try{
frame = mGrabber.grab(); // grab frame
} catch (Exception e){
System.out.println("Exception on grab frame from camera "+cameratitle+", Message : "+e.getMessage());
}
if (frame==null) continue;
Mat mat = matconverter.convert(frame); // convert to Mat
fps.incrementAndGet();
mat.copyTo(BestMat); // copy to BestMat for using OpenCL
UMat originalmat = new UMat();
mat.copyTo(originalmat); // copy to BestMat for using OpenCL
opencv_core.rotate(originalmat, BestMat, opencv_core.ROTATE_90_COUNTERCLOCKWISE);
IsGrabbingLiveView.set(false);
if (frame != null) {
if (!BestMat.empty()) {
opencv_imgproc.resize(BestMat, LiveMat, LiveSize); // resize to LiveSize
UMat graymat = new UMat(); // use OpenCL for grayscale
opencv_imgproc.cvtColor(LiveMat,graymat, COLOR_BGR2GRAY); // convert to grayscale
if (use_qr){
String qr = DetectQRFromMat(graymat);
if (ValidPatientID(qr)){
if (ValidBarCode(qr)){
if (!qr.equals(qrtext)){
qrtext = qr;
raise_log("QR Detected: " + qrtext);
if (event!=null) event.onDetectedQRCode(qrtext);
}
}
@@ -771,6 +795,12 @@ public class Cameradetail {
if (have_frontal_face){
if (face_counter<5){
face_counter++;
//System.out.println("Frontal Face Counter = "+face_counter+ " from camera "+cameratitle+" eye count = "+theface.getEyesCount());
continue;
}
no_face_counter = 0;
if (event!=null) event.onFrontalFaceDetector(true, _face_width, _face_height);
LiveMatROI = theface.getFace();
@@ -805,20 +835,25 @@ public class Cameradetail {
}
} 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 detected, but lets not cancel the previous state immediately
if (no_face_counter>30){
} else {
// no face detected, but let's not cancel the previous state immediately
if (no_face_counter>60){
// kalau tidak ada face selama 30 frame, reset state
// 30 frame approximately 2 second
// 60 frame approximately 2 second
eye_state = -1;
last_blink = 0;
waiting_for_second_blink = false;
face_counter = 0;
if (event!=null) {
event.onFrontalFaceDetector(false, _face_width, _face_height);
event.onProfileFaceDetector(false, _face_width, _face_height);
@@ -880,16 +915,26 @@ public class Cameradetail {
return false;
}
public void RemapROI(){
/**
* Remap LiveMatROI to BestMatROI and ReducedMatROI Resolution
* @param scale scale factor, 1.0 for 100%, 0.5 for 50%, etc
*/
public void RemapROI(double scale){
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);
double scaleXBest = scale*BestSize.width()/LiveSize.width();
double scaleYBest = scale*BestSize.height()/LiveSize.height();
System.out.println("scaleXBest = "+scaleXBest+" scaleYBest = "+scaleYBest);
double scaleXReduced = scale*ReducedSize.width()/LiveSize.width();
double scaleYReduced = scale*ReducedSize.height()/LiveSize.height();
System.out.println("scaleXReduced = "+scaleXReduced+" scaleYReduced = "+scaleYReduced);
BestMatROI = ResizeRect(LiveMatROI, scaleXBest, scaleYBest, 50,250);
System.out.println("BestMatROI camera "+cameratitle.getText()+" = "+RectToString(BestMatROI));
ReducedMatROI = ResizeRect(LiveMatROI, scaleXReduced, scaleYReduced, 50,250);
System.out.println("ReducedMatROI camera "+cameratitle.getText()+" = "+RectToString(ReducedMatROI));
} else {
System.out.println("LiveMatROI is Outside LiveMat for camera "+cameratitle.getText());