diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java index 653e57b..96f9dbf 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java @@ -10,7 +10,6 @@ public class DetectionReport { public String detectionType; public float detectedValue; public boolean detectionState; - //public String detectorID; public DetectionReport(boolean detectionState, String detectionType, float detectedAmplitude) { this.timeStamp = String.valueOf(Calendar.getInstance().getTime()); @@ -24,12 +23,12 @@ public class DetectionReport { /** Get Detection Report in String format */ public String toString() { + String state = "State: " + "[" + this.detectionState + "]"; String time = "Time: " + "[" + this.timeStamp + "]"; String type = "Type: " + "[" + this.detectionType + "]"; String value = "Value: " + "[" + this.detectedValue + "]"; - //String id = "ID: " + "[" + this.detectorID + "]"; - return String.join("\t", time, type, value); + return String.join("\t", state, time, type, value); } /** Debug Report */ diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/Detector.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/Detector.java index 38f55f0..30ced55 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/Detector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/Detector.java @@ -1,10 +1,18 @@ package com.example.ueberwachungssystem.Detection; +import android.os.CountDownTimer; + import androidx.annotation.NonNull; abstract public class Detector { private OnDetectionListener listener; + private boolean isDetecting = false; + private boolean extendDetection = false; + + // Countdown parameters + private int COUNTDOWN_TIME = 1000; // milliseconds + private int COUNTDOWN_POLLING_TIME = 100; // milliseconds /** Constructor - takes context of current activity */ public Detector() {} @@ -18,17 +26,44 @@ abstract public class Detector { this.listener = listener; } - /** Triggers onDetectionListener - call this to trigger violation/alarm */ public void reportViolation(String detectionType, float amplitude) { if (listener != null) { - DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude); - listener.onDetection(detectionReport); + if (!isDetecting) { + isDetecting = true; + DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude); + listener.onDetection(detectionReport); + startDetectionTimer(detectionType, amplitude); + } else { + extendDetection = true; + } } else { + isDetecting = false; + extendDetection = false; throw new IllegalStateException("No listener set for violation reporting"); } } + private void startDetectionTimer(String detectionType, float amplitude) { + isDetecting = true; + new CountDownTimer((long) COUNTDOWN_TIME, COUNTDOWN_POLLING_TIME) { + @Override + public void onTick(long millisUntilFinished) { + if (extendDetection) { + extendDetection = false; + startDetectionTimer(detectionType, amplitude); + this.cancel(); + } + } + @Override + public void onFinish() { + isDetecting = false; + DetectionReport detectionReport = new DetectionReport(false, detectionType, amplitude); + listener.onDetection(detectionReport); + } + }.start(); + } + /** Starts Detection (abstract method: needs to be overridden in child class) */ public abstract void startDetection(); diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java index 72a548c..3b99ef4 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java @@ -72,7 +72,7 @@ public class VideoDetector extends Detector { private static final int DILATE_KERNEL_SIZE = 5; private static final float CONTOUR_THRESHOLD = 100; private static final float ALARM_THRESHOLD = 0.2f; // Percent of pixels changed - private static final long START_DELAY = 1; // milliseconds + private static final long START_DELAY = 1000; // milliseconds private static final android.util.Size IMAGE_RES = new android.util.Size(640, 480); @@ -155,15 +155,22 @@ public class VideoDetector extends Detector { //mat = alternativeProcessImage(mat); mat = processImage(mat); debugMat(mat, imageView1); + + if (frameCnt <= 5) { + reportViolation("Video", 0); + frameCnt++; + } } imageProxy.close(); }); + + // Create Preview Preview preview = new Preview.Builder().build(); // Specify which Camera to use CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build(); // Delay till start - new CountDownTimer((long)(START_DELAY * 1000), 1000){ + new CountDownTimer((long)(START_DELAY), 1000){ @Override public void onTick(long millisUntilFinished) {} @Override diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index 9bfbed8..6206490 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -31,6 +31,8 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + getCameraAccess(); + ImageView imageView = findViewById(R.id.imageView); ImageView ogiv = findViewById(R.id.ogiv);