Changde Detector class

This commit is contained in:
Bastian Kohler 2023-06-14 16:38:33 +02:00
parent bbd333426d
commit 8551abfc5d
4 changed files with 51 additions and 8 deletions

View File

@ -10,7 +10,6 @@ public class DetectionReport {
public String detectionType; public String detectionType;
public float detectedValue; public float detectedValue;
public boolean detectionState; public boolean detectionState;
//public String detectorID;
public DetectionReport(boolean detectionState, String detectionType, float detectedAmplitude) { public DetectionReport(boolean detectionState, String detectionType, float detectedAmplitude) {
this.timeStamp = String.valueOf(Calendar.getInstance().getTime()); this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
@ -24,12 +23,12 @@ public class DetectionReport {
/** Get Detection Report in String format */ /** Get Detection Report in String format */
public String toString() { public String toString() {
String state = "State: " + "[" + this.detectionState + "]";
String time = "Time: " + "[" + this.timeStamp + "]"; String time = "Time: " + "[" + this.timeStamp + "]";
String type = "Type: " + "[" + this.detectionType + "]"; String type = "Type: " + "[" + this.detectionType + "]";
String value = "Value: " + "[" + this.detectedValue + "]"; 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 */ /** Debug Report */

View File

@ -1,10 +1,18 @@
package com.example.ueberwachungssystem.Detection; package com.example.ueberwachungssystem.Detection;
import android.os.CountDownTimer;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
abstract public class Detector { abstract public class Detector {
private OnDetectionListener listener; 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 */ /** Constructor - takes context of current activity */
public Detector() {} public Detector() {}
@ -18,17 +26,44 @@ abstract public class Detector {
this.listener = listener; this.listener = listener;
} }
/** Triggers onDetectionListener - call this to trigger violation/alarm */ /** Triggers onDetectionListener - call this to trigger violation/alarm */
public void reportViolation(String detectionType, float amplitude) { public void reportViolation(String detectionType, float amplitude) {
if (listener != null) { if (listener != null) {
DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude); if (!isDetecting) {
listener.onDetection(detectionReport); isDetecting = true;
DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude);
listener.onDetection(detectionReport);
startDetectionTimer(detectionType, amplitude);
} else {
extendDetection = true;
}
} else { } else {
isDetecting = false;
extendDetection = false;
throw new IllegalStateException("No listener set for violation reporting"); 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) */ /** Starts Detection (abstract method: needs to be overridden in child class) */
public abstract void startDetection(); public abstract void startDetection();

View File

@ -72,7 +72,7 @@ public class VideoDetector extends Detector {
private static final int DILATE_KERNEL_SIZE = 5; private static final int DILATE_KERNEL_SIZE = 5;
private static final float CONTOUR_THRESHOLD = 100; private static final float CONTOUR_THRESHOLD = 100;
private static final float ALARM_THRESHOLD = 0.2f; // Percent of pixels changed 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); 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 = alternativeProcessImage(mat);
mat = processImage(mat); mat = processImage(mat);
debugMat(mat, imageView1); debugMat(mat, imageView1);
if (frameCnt <= 5) {
reportViolation("Video", 0);
frameCnt++;
}
} }
imageProxy.close(); imageProxy.close();
}); });
// Create Preview // Create Preview
Preview preview = new Preview.Builder().build(); Preview preview = new Preview.Builder().build();
// Specify which Camera to use // Specify which Camera to use
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build(); CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
// Delay till start // Delay till start
new CountDownTimer((long)(START_DELAY * 1000), 1000){ new CountDownTimer((long)(START_DELAY), 1000){
@Override @Override
public void onTick(long millisUntilFinished) {} public void onTick(long millisUntilFinished) {}
@Override @Override

View File

@ -31,6 +31,8 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
getCameraAccess();
ImageView imageView = findViewById(R.id.imageView); ImageView imageView = findViewById(R.id.imageView);
ImageView ogiv = findViewById(R.id.ogiv); ImageView ogiv = findViewById(R.id.ogiv);