From 19d1bfe1e313bbe2656db93ab8a8a6a75ee0ebe7 Mon Sep 17 00:00:00 2001 From: Bastian Kohler Date: Mon, 29 May 2023 10:43:51 +0200 Subject: [PATCH] Refactor --- .../ueberwachungssystem/MainActivity.java | 1 + .../VideoDetection/VideoDetector.java | 26 ++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index 63f6df6..1173b49 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -55,6 +55,7 @@ public class MainActivity extends AppCompatActivity { } else { vd.stopDetection(); + textView.setText(""); } } }); diff --git a/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java b/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java index 01baf7a..babb677 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java @@ -32,15 +32,14 @@ public class VideoDetector extends Detector { // Preview Camera Image private PreviewView previewView = null; // Detect Violation - private static final int PIXEL_THRESHOLD = 60; // Luminosity (brightness channel of YUV_420_888) - private static final float ALARM_THRESHOLD = 0.5f; // Percent of pixels changed + private static final float PIXEL_THRESHOLD = 30f; // Luminosity (brightness channel of YUV_420_888) + private static final float ALARM_THRESHOLD = 1f; // Percent of pixels changed private ByteBuffer previousBuffer = null; /** Constructor */ public VideoDetector(Context context) { - super(); this.context = context; } @@ -59,6 +58,7 @@ public class VideoDetector extends Detector { cameraProvider = cameraProviderFuture.get(); bindLuminosityAnalysis(cameraProvider); isDetectionRunning = true; + previousBuffer = null; } catch (ExecutionException | InterruptedException e) { // No errors need to be handled for this Future. This should never be reached. } @@ -118,23 +118,15 @@ public class VideoDetector extends Detector { preview.setSurfaceProvider(previewView.getSurfaceProvider()); cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview); - stopDetection(); } + /** Calculate Amount of Pixels changed */ private int getChangedPixelCount(Image image) { - int width = image.getWidth(); - int height = image.getHeight(); - Image.Plane[] planes = image.getPlanes(); ByteBuffer buffer = planes[0].getBuffer(); - int yRowStride = image.getPlanes()[0].getRowStride(); - int yPixelStride = image.getPlanes()[0].getPixelStride(); - - int changedPixelCount = 0; - if (previousBuffer == null) { previousBuffer = ByteBuffer.allocate(buffer.capacity()); buffer.rewind(); @@ -143,6 +135,15 @@ public class VideoDetector extends Detector { return 0; } + int width = image.getWidth(); + int height = image.getHeight(); + + int yRowStride = image.getPlanes()[0].getRowStride(); + int yPixelStride = image.getPlanes()[0].getPixelStride(); + + int changedPixelCount = 0; + + // Count changed pixels for (int y = 0; y < height; ++y) { for (int x = 0; x < width; x++) { int index = (y * yRowStride) + (x * yPixelStride); @@ -153,6 +154,7 @@ public class VideoDetector extends Detector { changedPixelCount++; } } + // Reset and copy Byte Buffer buffer.rewind(); previousBuffer.rewind();