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()); | ||||
/** 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 */ |
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() {} | ||||
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); | |||||
listener.onDetection(detectionReport); | |||||
if (!isDetecting) { | |||||
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(); | ||||
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); | ||||
//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 |
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); | ||||