Compare commits
43
Commits
tw
...
f329fab62d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f329fab62d | ||
|
|
4baf3db1bb | ||
|
|
29c193b716 | ||
|
|
76442888f7 | ||
|
|
d32e0a11f5 | ||
|
|
fb8bdcd895 | ||
|
|
7ef0df48dd | ||
|
|
50b579705e | ||
|
|
8551abfc5d | ||
|
|
bbd333426d | ||
|
|
e98ae10267 | ||
|
|
7d29ed1ca9 | ||
|
|
f98079d431 | ||
|
|
047d45f1cb | ||
|
|
95919a6602 | ||
|
|
19d1bfe1e3 | ||
|
|
e9da712882 | ||
|
|
804c5aba36 | ||
|
|
c5577b125f | ||
|
|
6100d17bd2 | ||
|
|
74a291bdd6 | ||
|
|
ba6765b1fb | ||
|
|
9fce3be6a8 | ||
|
|
9892fc35bc | ||
|
|
62a528d33b | ||
|
|
37617dbd54 | ||
|
|
569bd43163 | ||
|
|
fdb0036ea6 | ||
|
|
f9797f2dca | ||
|
|
ba75c58a23 | ||
|
|
3d799ae461 | ||
|
|
2501662737 | ||
|
|
d04d166e14 | ||
|
|
1576719d10 | ||
|
|
362af75b9a | ||
|
|
bef5d03494 | ||
|
|
80ebf77eb6 | ||
|
|
c4ed247a05 | ||
|
|
323786859e | ||
|
|
354bf937b2 | ||
|
|
39bb37f5c3 | ||
|
|
b198dc2e06 | ||
|
|
d83cd3dc32 |
No files matched your search
Whitespace-only changes.
@@ -36,5 +36,22 @@ dependencies {
|
|||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||||
|
|
||||||
|
|
||||||
|
// OpenCV (from: https://github.com/QuickBirdEng/opencv-android)
|
||||||
|
def opencv_version = "4.5.3.0"
|
||||||
|
implementation "com.quickbirdstudios:opencv:${opencv_version}"
|
||||||
|
|
||||||
|
|
||||||
|
// Required for CameraX
|
||||||
|
def camerax_version = "1.2.2"
|
||||||
|
implementation "androidx.camera:camera-core:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-camera2:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-video:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-view:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-extensions:${camerax_version}"
|
||||||
|
|
||||||
|
|
||||||
implementation 'com.jjoe64:graphview:4.2.2'
|
implementation 'com.jjoe64:graphview:4.2.2'
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,10 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
|
<uses-feature android:name="android.hardware.camera"/>
|
||||||
<uses-permission android:name="android.permission.CAMERA"/>
|
<uses-permission android:name="android.permission.CAMERA"/>
|
||||||
|
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||||
|
|
||||||
<!--<uses-permission android:name="android.permission.BLUETOOTH"/>-->
|
<!--<uses-permission android:name="android.permission.BLUETOOTH"/>-->
|
||||||
<!--<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>-->
|
<!--<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>-->
|
||||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.example.ueberwachungssystem.Detection;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
/** Detection Report Class */
|
||||||
|
public class DetectionReport {
|
||||||
|
public String timeStamp;
|
||||||
|
public String detectionType;
|
||||||
|
public float detectedValue;
|
||||||
|
public boolean detectionState;
|
||||||
|
|
||||||
|
public DetectionReport(boolean detectionState, String detectionType, float detectedAmplitude) {
|
||||||
|
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
|
||||||
|
this.detectionType = detectionType;
|
||||||
|
this.detectedValue = detectedAmplitude;
|
||||||
|
this.detectionState = detectionState;
|
||||||
|
|
||||||
|
//this.detectorID = detectorID;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** 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 + "]";
|
||||||
|
|
||||||
|
return String.join("\t", state, time, type, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Debug Report */
|
||||||
|
public void log(String tag) {
|
||||||
|
Log.d(tag, this.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
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 extendViolation = false;
|
||||||
|
|
||||||
|
// Countdown parameters
|
||||||
|
private final int COUNTDOWN_TIME = 5000; // milliseconds
|
||||||
|
private final int COUNTDOWN_POLLING_TIME = 100; // milliseconds
|
||||||
|
|
||||||
|
/** Constructor - takes context of current activity */
|
||||||
|
public Detector() {}
|
||||||
|
|
||||||
|
|
||||||
|
/** On Detection Listener - runs when violation is reported */
|
||||||
|
public interface OnDetectionListener {
|
||||||
|
void onDetection(@NonNull DetectionReport detectionReport);
|
||||||
|
}
|
||||||
|
public void setOnDetectionListener(@NonNull OnDetectionListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Triggers onDetectionListener - call this to trigger violation/alarm */
|
||||||
|
public void reportViolation(String detectionType, float amplitude) {
|
||||||
|
if (listener != null) {
|
||||||
|
if (!isDetecting) {
|
||||||
|
isDetecting = true;
|
||||||
|
DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude);
|
||||||
|
listener.onDetection(detectionReport);
|
||||||
|
startDetectionTimer(detectionType, amplitude);
|
||||||
|
} else {
|
||||||
|
extendViolation = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
isDetecting = false;
|
||||||
|
extendViolation = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startDetectionTimer(String detectionType, float amplitude) {
|
||||||
|
isDetecting = true;
|
||||||
|
new CountDownTimer((long) COUNTDOWN_TIME, COUNTDOWN_POLLING_TIME) {
|
||||||
|
@Override
|
||||||
|
public void onTick(long millisUntilFinished) {
|
||||||
|
if (extendViolation) {
|
||||||
|
extendViolation = false;
|
||||||
|
startDetectionTimer(detectionType, amplitude);
|
||||||
|
this.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onFinish() {
|
||||||
|
isDetecting = false;
|
||||||
|
DetectionReport detectionReport = new DetectionReport(false, detectionType, amplitude);
|
||||||
|
listener.onDetection(detectionReport);
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void extendViolation(){
|
||||||
|
this.extendViolation = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Starts Detection (abstract method: needs to be overridden in child class) */
|
||||||
|
public abstract void startDetection();
|
||||||
|
|
||||||
|
/** Stops Detection (abstract method: needs to be overridden in child class) */
|
||||||
|
public abstract void stopDetection();
|
||||||
|
}
|
||||||
@@ -0,0 +1,441 @@
|
|||||||
|
package com.example.ueberwachungssystem.Detection;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.ImageFormat;
|
||||||
|
import android.media.Image;
|
||||||
|
import android.os.CountDownTimer;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.Surface;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.camera.core.CameraSelector;
|
||||||
|
import androidx.camera.core.ExperimentalGetImage;
|
||||||
|
import androidx.camera.core.ImageAnalysis;
|
||||||
|
import androidx.camera.core.ImageProxy;
|
||||||
|
import androidx.camera.core.Preview;
|
||||||
|
import androidx.camera.core.VideoCapture;
|
||||||
|
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||||
|
import androidx.camera.view.PreviewView;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
|
|
||||||
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
|
|
||||||
|
import org.opencv.android.OpenCVLoader;
|
||||||
|
import org.opencv.android.Utils;
|
||||||
|
import org.opencv.core.Core;
|
||||||
|
import org.opencv.core.CvType;
|
||||||
|
import org.opencv.core.Mat;
|
||||||
|
import org.opencv.core.MatOfPoint;
|
||||||
|
import org.opencv.core.Scalar;
|
||||||
|
import org.opencv.core.Size;
|
||||||
|
import org.opencv.imgproc.Imgproc;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Video Detector inherits some methods from abstract Detector class (more info there)
|
||||||
|
* USE FROM MAIN ACTIVITY:
|
||||||
|
* VideoDetector vd = new VideoDetector(this);
|
||||||
|
* */
|
||||||
|
|
||||||
|
|
||||||
|
@ExperimentalGetImage
|
||||||
|
public class VideoDetector extends Detector {
|
||||||
|
|
||||||
|
// Calling Activity
|
||||||
|
private final Context context;
|
||||||
|
|
||||||
|
// Permission handling
|
||||||
|
private static final int PERMISSION_REQUEST_CODE = 3691;
|
||||||
|
|
||||||
|
// Camera Provider
|
||||||
|
private ProcessCameraProvider cameraProvider;
|
||||||
|
private final ImageAnalysis imageAnalysis;
|
||||||
|
private final VideoCapture videoCapture;
|
||||||
|
private final Preview preview;
|
||||||
|
|
||||||
|
// Logic
|
||||||
|
private boolean isDetecting = false;
|
||||||
|
private boolean isRecording = false;
|
||||||
|
private boolean allowReportViolation = false;
|
||||||
|
|
||||||
|
// Image Processing
|
||||||
|
private Mat previousImage = null;
|
||||||
|
|
||||||
|
// Debugging
|
||||||
|
private ImageView inputImageView = null;
|
||||||
|
private ImageView outputImageView = null;
|
||||||
|
|
||||||
|
|
||||||
|
// Recording
|
||||||
|
private final String outputName = "video.mp4";
|
||||||
|
|
||||||
|
|
||||||
|
// Parameters
|
||||||
|
private static final float ALARM_THRESHOLD = 0.5f; // Percent of pixels changed
|
||||||
|
private static final long START_DELAY = 20000; // milliseconds
|
||||||
|
private static final android.util.Size IMAGE_RES = new android.util.Size(640, 480);
|
||||||
|
|
||||||
|
|
||||||
|
private enum UseCase {
|
||||||
|
ImageAnalysis,
|
||||||
|
Preview,
|
||||||
|
VideoCapture
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/** Constructor */
|
||||||
|
public VideoDetector(Context context) {
|
||||||
|
super();
|
||||||
|
this.context = context;
|
||||||
|
this.imageAnalysis = setupImageAnalysis();
|
||||||
|
this.videoCapture = setupVideoCapture();
|
||||||
|
this.preview = new Preview.Builder().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get State of the Detector */
|
||||||
|
public boolean isRunning() {
|
||||||
|
return isDetecting;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Starts the Video Detection */
|
||||||
|
@Override
|
||||||
|
public void startDetection() {
|
||||||
|
// Check States
|
||||||
|
if (isDetecting)
|
||||||
|
return;
|
||||||
|
// Return On Request Permissions
|
||||||
|
if (!hasPermissions()) {
|
||||||
|
getPermissions();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Open CV startup check
|
||||||
|
if (!OpenCVLoader.initDebug()) {
|
||||||
|
Log.e("OpenCV", "Unable to load OpenCV!");
|
||||||
|
return;
|
||||||
|
} else
|
||||||
|
Log.d("OpenCV", "OpenCV loaded Successfully!");
|
||||||
|
// Get Process Camera Provider and start
|
||||||
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||||
|
cameraProviderFuture.addListener(() -> {
|
||||||
|
try {
|
||||||
|
cameraProvider = cameraProviderFuture.get();
|
||||||
|
isDetecting = true;
|
||||||
|
bindCameraProvider(UseCase.ImageAnalysis);
|
||||||
|
} catch (ExecutionException | InterruptedException e) {}
|
||||||
|
}, ContextCompat.getMainExecutor(context));
|
||||||
|
|
||||||
|
startViolationTimer();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Stops the Video Detection */
|
||||||
|
@Override
|
||||||
|
public void stopDetection() {
|
||||||
|
if (!isDetecting || imageAnalysis == null)
|
||||||
|
return;
|
||||||
|
cameraProvider.unbind(imageAnalysis);
|
||||||
|
isDetecting = false;
|
||||||
|
allowReportViolation = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Permission handling */
|
||||||
|
private boolean hasPermissions() {
|
||||||
|
return ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
|
||||||
|
ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
|
||||||
|
}
|
||||||
|
private void getPermissions() {
|
||||||
|
if (!hasPermissions())
|
||||||
|
ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, PERMISSION_REQUEST_CODE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
||||||
|
private void bindCameraProvider(UseCase useCase) {
|
||||||
|
// Specify which Camera to use
|
||||||
|
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
||||||
|
cameraProvider.unbindAll();
|
||||||
|
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, videoCapture);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Start delay until Violation Report is allowed */
|
||||||
|
private void startViolationTimer() {
|
||||||
|
new CountDownTimer((long) (START_DELAY), 100) {
|
||||||
|
@Override
|
||||||
|
public void onTick(long millisUntilFinished) {
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onFinish() {
|
||||||
|
allowReportViolation = true;
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private ImageAnalysis setupImageAnalysis() {
|
||||||
|
// Configure and create Image Analysis
|
||||||
|
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
||||||
|
builder.setTargetResolution(IMAGE_RES);
|
||||||
|
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
||||||
|
builder.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888);
|
||||||
|
ImageAnalysis imageAnalysis = builder.build();
|
||||||
|
// Set Analyzer
|
||||||
|
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> {
|
||||||
|
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
||||||
|
Image image = imageProxy.getImage();
|
||||||
|
assert image != null;
|
||||||
|
|
||||||
|
// Violation Handling
|
||||||
|
Mat processed = processImage(imageProxy);
|
||||||
|
int n = OpenCVHelper.countNonZeroPixels(processed);
|
||||||
|
int pixelCount = image.getWidth() * image.getHeight();
|
||||||
|
float percentChanged = (float) n / pixelCount;
|
||||||
|
|
||||||
|
// Violation Condition
|
||||||
|
if (percentChanged * 100 > ALARM_THRESHOLD) {
|
||||||
|
if (allowReportViolation)
|
||||||
|
reportViolation("Video", n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
imageProxy.close();
|
||||||
|
});
|
||||||
|
return imageAnalysis;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("RestrictedApi")
|
||||||
|
private VideoCapture setupVideoCapture() {
|
||||||
|
return new VideoCapture.Builder()
|
||||||
|
.setTargetRotation(Surface.ROTATION_0)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("RestrictedApi")
|
||||||
|
public void startRecording() {
|
||||||
|
// Check States
|
||||||
|
if (isRecording){
|
||||||
|
extendViolation();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return On Request Permissions
|
||||||
|
if (!hasPermissions()) {
|
||||||
|
getPermissions();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||||
|
cameraProviderFuture.addListener(() -> {
|
||||||
|
try {
|
||||||
|
cameraProvider = cameraProviderFuture.get();
|
||||||
|
isRecording = true;
|
||||||
|
bindCameraProvider(UseCase.VideoCapture);
|
||||||
|
|
||||||
|
File vidFile = new File(context.getFilesDir() + "/" + outputName);
|
||||||
|
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
videoCapture.startRecording(
|
||||||
|
new VideoCapture.OutputFileOptions.Builder(vidFile).build(),
|
||||||
|
context.getMainExecutor(),
|
||||||
|
new VideoCapture.OnVideoSavedCallback() {
|
||||||
|
@Override
|
||||||
|
public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) {
|
||||||
|
isRecording = false;
|
||||||
|
Toast.makeText(context, "recording saved", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
|
||||||
|
isRecording = false;
|
||||||
|
Toast.makeText(context, "recording failed", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (ExecutionException | InterruptedException e) {}
|
||||||
|
}, ContextCompat.getMainExecutor(context));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("RestrictedApi")
|
||||||
|
public void stopRecording(){
|
||||||
|
videoCapture.stopRecording();
|
||||||
|
cameraProvider.unbind(videoCapture);
|
||||||
|
isRecording = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Process Image to be used for Motion Detection */
|
||||||
|
private Mat processImage(ImageProxy imageProxy){
|
||||||
|
if (imageProxy == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Image Transformation
|
||||||
|
Mat imageMat = OpenCVHelper.extractYChannel(imageProxy);
|
||||||
|
|
||||||
|
// Show Input Image
|
||||||
|
if (inputImageView != null)
|
||||||
|
OpenCVHelper.debugMat(imageMat, inputImageView);
|
||||||
|
|
||||||
|
// Preprocess Image
|
||||||
|
Mat preprocessed = imageMat;
|
||||||
|
preprocessed = OpenCVHelper.addGaussianBlur(preprocessed, new Size(21, 21));
|
||||||
|
preprocessed = OpenCVHelper.addBlur(preprocessed, new Size(3, 3));
|
||||||
|
|
||||||
|
if (previousImage == null) {
|
||||||
|
previousImage = preprocessed;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process Image
|
||||||
|
Mat processed = preprocessed.clone();
|
||||||
|
processed = OpenCVHelper.thresholdPixels(processed, previousImage, 25);
|
||||||
|
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
||||||
|
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
||||||
|
processed = OpenCVHelper.thresholdContourArea(processed, 500);
|
||||||
|
|
||||||
|
// Output
|
||||||
|
previousImage = preprocessed.clone();
|
||||||
|
// Show Output Image
|
||||||
|
if (outputImageView != null)
|
||||||
|
OpenCVHelper.debugMat(processed, outputImageView);
|
||||||
|
return processed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void debugProcessing(@NonNull ImageView inputImageView, @NonNull ImageView outputImageView){
|
||||||
|
this.inputImageView = inputImageView;
|
||||||
|
this.outputImageView = outputImageView;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPreviewView(@NonNull PreviewView previewView) {
|
||||||
|
// Create Preview
|
||||||
|
if (this.preview != null)
|
||||||
|
this.preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static class OpenCVHelper{
|
||||||
|
private OpenCVHelper() {}
|
||||||
|
|
||||||
|
/** OpenCV helper methods **/
|
||||||
|
private static Mat addGaussianBlur(Mat inputMat, Size kernelSize){
|
||||||
|
Mat outputMat = new Mat();
|
||||||
|
Imgproc.GaussianBlur(inputMat, outputMat, kernelSize, 0);
|
||||||
|
return outputMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Mat addBlur(Mat inputMat, Size kernelSize){
|
||||||
|
Mat outputMat = new Mat();
|
||||||
|
Imgproc.blur(inputMat, outputMat, kernelSize);
|
||||||
|
return outputMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Mat extractYChannel(@NonNull ImageProxy imgProxy) {
|
||||||
|
Image img = imgProxy.getImage();
|
||||||
|
|
||||||
|
assert img != null;
|
||||||
|
ByteBuffer yBuffer = img.getPlanes()[0].getBuffer();
|
||||||
|
byte[] yData = new byte[yBuffer.remaining()];
|
||||||
|
yBuffer.get(yData);
|
||||||
|
|
||||||
|
Mat yMat = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
|
||||||
|
yMat.put(0, 0, yData);
|
||||||
|
|
||||||
|
return yMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Mat thresholdPixels(Mat inputMat, Mat previousImage, int threshold){
|
||||||
|
Mat diffImage = new Mat();
|
||||||
|
Core.absdiff(inputMat, previousImage, diffImage);
|
||||||
|
Mat binaryMat = new Mat();
|
||||||
|
Imgproc.threshold(diffImage, binaryMat, threshold, 255, Imgproc.THRESH_BINARY);
|
||||||
|
return binaryMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Mat imageProxyToGrayscaleMat(ImageProxy imageProxy) {
|
||||||
|
// Step 1: Extract the image data from ImageProxy
|
||||||
|
ImageProxy.PlaneProxy[] planes = imageProxy.getPlanes();
|
||||||
|
ByteBuffer yBuffer = planes[0].getBuffer();
|
||||||
|
byte[] yData = new byte[yBuffer.remaining()];
|
||||||
|
yBuffer.get(yData);
|
||||||
|
|
||||||
|
// Step 2: Convert the image data to NV21 format
|
||||||
|
int width = imageProxy.getWidth();
|
||||||
|
int height = imageProxy.getHeight();
|
||||||
|
byte[] nv21Data = new byte[width * height * 3 / 2];
|
||||||
|
// Assuming the image format is YUV_420_888
|
||||||
|
System.arraycopy(yData, 0, nv21Data, 0, yData.length);
|
||||||
|
for (int i = yData.length; i < nv21Data.length; i += 2) {
|
||||||
|
nv21Data[i] = yData[i + 1];
|
||||||
|
nv21Data[i + 1] = yData[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 3: Create a grayscale Mat from the NV21 data
|
||||||
|
Mat grayscaleMat = new Mat(height, width, CvType.CV_8UC1);
|
||||||
|
grayscaleMat.put(0, 0, nv21Data);
|
||||||
|
|
||||||
|
return grayscaleMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Mat thresholdContourArea(Mat inputMat, float areaThreshold){
|
||||||
|
List<MatOfPoint> contours = new ArrayList<>();
|
||||||
|
Mat hierarchy = new Mat();
|
||||||
|
Imgproc.findContours(inputMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
||||||
|
|
||||||
|
Mat outputMat = new Mat(inputMat.size(), inputMat.type(), new Scalar(0));
|
||||||
|
// Iterate over the contours and draw only the larger contours on the outputMat
|
||||||
|
for (MatOfPoint contour : contours) {
|
||||||
|
double contourArea = Imgproc.contourArea(contour);
|
||||||
|
if (contourArea > areaThreshold) {
|
||||||
|
Imgproc.drawContours(outputMat, Collections.singletonList(contour), 0, new Scalar(255), -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Apply the outputMat as a mask to the dilatedImage
|
||||||
|
Mat maskedImage = new Mat();
|
||||||
|
inputMat.copyTo(maskedImage, outputMat);
|
||||||
|
return outputMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Mat dilateBinaryMat(Mat inputMat, Size kernelSize){
|
||||||
|
Mat dilatedMat = new Mat();
|
||||||
|
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, kernelSize);
|
||||||
|
Imgproc.dilate(inputMat, dilatedMat, kernel);
|
||||||
|
return dilatedMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int countNonZeroPixels(Mat inputImage) {
|
||||||
|
if (inputImage != null)
|
||||||
|
return Core.countNonZero(inputImage);
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void debugMat(Mat mat, ImageView imageView) {
|
||||||
|
if (imageView == null || mat == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
|
||||||
|
Utils.matToBitmap(mat, bitmap);
|
||||||
|
|
||||||
|
// Display the bitmap in an ImageView
|
||||||
|
imageView.setImageBitmap(bitmap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,61 +1,73 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.camera.core.ExperimentalGetImage;
|
||||||
|
import androidx.camera.view.PreviewView;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Switch;
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.ToggleButton;
|
||||||
|
|
||||||
|
import com.example.ueberwachungssystem.Detection.DetectionReport;
|
||||||
|
import com.example.ueberwachungssystem.Detection.Detector;
|
||||||
|
import com.example.ueberwachungssystem.Detection.VideoDetector;
|
||||||
import com.example.ueberwachungssystem.logger.Logger;
|
import com.example.ueberwachungssystem.logger.Logger;
|
||||||
import com.jjoe64.graphview.GraphView;
|
import com.jjoe64.graphview.GraphView;
|
||||||
|
|
||||||
|
@ExperimentalGetImage
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
Logger logger;
|
|
||||||
GraphView graph;
|
|
||||||
private TextView tv_log;
|
|
||||||
MicrophoneDetector Mic;
|
|
||||||
private Switch TglBtn_Mic;
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
tv_log = (TextView) findViewById(R.id.tv_log); //Set textview for showing logged content
|
|
||||||
logger = new Logger(this.getClass().getSimpleName(), tv_log, "");
|
|
||||||
logger.log(this.getClass().getSimpleName() + ".onCreate");
|
|
||||||
graph = (GraphView) findViewById(R.id.graph);
|
|
||||||
Mic = new MicrophoneDetector(this, logger, graph);
|
|
||||||
setupMic();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
ImageView inputImageView = findViewById(R.id.inputImageView);
|
||||||
public void onResume() {
|
ImageView outputImageView = findViewById(R.id.outputImageView);
|
||||||
super.onResume();
|
PreviewView previewView = findViewById(R.id.previewView);
|
||||||
logger.log(this.getClass().getSimpleName() + ".onResume");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
logger.log(this.getClass().getSimpleName() + ".onPause");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupMic() {
|
// Mic stuff
|
||||||
TglBtn_Mic = (Switch) findViewById(R.id.TglBtn_Mic);
|
TextView textView= (TextView) findViewById(R.id.textView); //Set textview for showing logged content
|
||||||
TglBtn_Mic.setOnClickListener(new View.OnClickListener() {
|
Logger logger = new Logger(this.getClass().getSimpleName(), textView, "");
|
||||||
|
GraphView graph = (GraphView) findViewById(R.id.graph);
|
||||||
|
MicrophoneDetector mic = new MicrophoneDetector(this, logger, graph);
|
||||||
|
mic.startDetection();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// video Stuff
|
||||||
|
VideoDetector vd = new VideoDetector(this);
|
||||||
|
//vd.setPreviewView(previewView);
|
||||||
|
vd.debugProcessing(inputImageView, outputImageView);
|
||||||
|
vd.setOnDetectionListener(new Detector.OnDetectionListener() {
|
||||||
|
@Override
|
||||||
|
public void onDetection(@NonNull DetectionReport detectionReport) {
|
||||||
|
Log.d("onDetection", detectionReport.toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
vd.startDetection();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ToggleButton toggleButton = findViewById(R.id.toggleButton);
|
||||||
|
toggleButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (v == TglBtn_Mic) {
|
if (toggleButton.isChecked())
|
||||||
logger.log("onClick toggleButtonThread " + TglBtn_Mic.isChecked());
|
{
|
||||||
if (TglBtn_Mic.isChecked()) {
|
//vd.startDetection();
|
||||||
Mic.startDetection();
|
vd.startRecording();
|
||||||
} else {
|
}
|
||||||
Mic.stopDetection();
|
else {
|
||||||
logger.clearLog();
|
//vd.stopDetection();
|
||||||
}
|
vd.stopRecording();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,26 +1,44 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:gravity="top"
|
||||||
|
android:orientation="vertical"
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/tv_log"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="Log"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toBottomOf="@+id/TglBtn_Mic" />
|
|
||||||
|
|
||||||
<Switch
|
<TextView
|
||||||
android:id="@+id/TglBtn_Mic"
|
android:id="@+id/textView"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="100dp"
|
||||||
|
android:text="TextView" />
|
||||||
|
|
||||||
|
<androidx.camera.view.PreviewView
|
||||||
|
android:id="@+id/previewView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Mic"
|
android:backgroundTint="@android:color/black"/>
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
<ToggleButton
|
||||||
|
android:id="@+id/toggleButton"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="ToggleButton" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/inputImageView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
tools:srcCompat="@tools:sample/avatars" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/outputImageView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="1dp"
|
||||||
|
tools:srcCompat="@tools:sample/avatars" />
|
||||||
|
|
||||||
<com.jjoe64.graphview.GraphView
|
<com.jjoe64.graphview.GraphView
|
||||||
android:id="@+id/graph"
|
android:id="@+id/graph"
|
||||||
@@ -34,4 +52,5 @@
|
|||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
app:layout_constraintVertical_bias="0.499" />
|
app:layout_constraintVertical_bias="0.499" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
||||||
|
</LinearLayout>
|
||||||
Reference in new issue
Block a user