From 9fce3be6a80f285e77099caa56cb2f168871340c Mon Sep 17 00:00:00 2001 From: Bastian Kohler Date: Sun, 28 May 2023 22:19:30 +0200 Subject: [PATCH 1/3] Refactored some stuff in Main --- .../example/ueberwachungssystem/MainActivity.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index 2c99287..63f6df6 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -21,11 +21,6 @@ import com.example.ueberwachungssystem.VideoDetection.VideoDetector; public class MainActivity extends AppCompatActivity { private static final int CAMERA_PERMISSION_REQUEST_CODE = 101; - private PreviewView previewView; - private TextView textView; - private ToggleButton toggleButton; - private Boolean cameraPreviewIsRunning = false; - private Boolean isPressed = false; @Override @@ -33,16 +28,15 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - textView = findViewById(R.id.textView); - previewView = findViewById(R.id.previewView); - toggleButton = findViewById(R.id.previewButton); + TextView textView = findViewById(R.id.textView); + PreviewView previewView = findViewById(R.id.previewView); VideoDetector vd = new VideoDetector(this); vd.setPreviewView(previewView); vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() { @Override - public void onDetection(DetectionReport detectionReport) { + public void onDetection(@NonNull DetectionReport detectionReport) { detectionReport.log("OnDetection"); textView.setText(detectionReport.toString()); } @@ -50,6 +44,7 @@ public class MainActivity extends AppCompatActivity { //vd.startDetection(); + ToggleButton toggleButton = findViewById(R.id.previewButton); toggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { From ba6765b1fbeab8aad57d08bdd7f68d7a2e1e16c5 Mon Sep 17 00:00:00 2001 From: Bastian Kohler Date: Sun, 28 May 2023 22:20:56 +0200 Subject: [PATCH 2/3] Changed VideoDetector.java to detect based on changed pixel count instead of average luminosity --- .../VideoDetection/VideoDetector.java | 101 +++++++++++++----- 1 file changed, 75 insertions(+), 26 deletions(-) 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 ad80e28..d7d9a16 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java @@ -10,7 +10,6 @@ import androidx.annotation.NonNull; 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.lifecycle.ProcessCameraProvider; import androidx.camera.view.PreviewView; @@ -37,10 +36,15 @@ public class VideoDetector extends Detector { private final float DELTA_LUMINOSITY_THRESHOLD = 1.5f; private Float previousLuminosity = null; + private static final int PIXEL_THRESHOLD = 50; // Luminosity (brightness channel of YUV_420_888) + private static final int ALARM_THRESHOLD = 1; // Percent of pixels changed + private ByteBuffer previousBuffer = null; + /** Constructor */ public VideoDetector(Context context) { + super(); this.context = context; } @@ -54,16 +58,13 @@ public class VideoDetector extends Detector { // Request Camera Provider final ListenableFuture cameraProviderFuture = ProcessCameraProvider.getInstance(context); //Check for Camera availability - cameraProviderFuture.addListener(new Runnable() { - @Override - public void run() { - try { - cameraProvider = cameraProviderFuture.get(); - bindLuminosityAnalysis(cameraProvider); - isDetectionRunning = true; - } catch (ExecutionException | InterruptedException e) { - // No errors need to be handled for this Future. This should never be reached. - } + cameraProviderFuture.addListener(() -> { + try { + cameraProvider = cameraProviderFuture.get(); + bindLuminosityAnalysis(cameraProvider); + isDetectionRunning = true; + } catch (ExecutionException | InterruptedException e) { + // No errors need to be handled for this Future. This should never be reached. } },ContextCompat.getMainExecutor(context)); } @@ -91,25 +92,34 @@ public class VideoDetector extends Detector { ImageAnalysis.Builder builder = new ImageAnalysis.Builder(); builder.setTargetResolution(new Size(640, 480)); 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), new ImageAnalysis.Analyzer() { - @Override - public void analyze(@NonNull ImageProxy imageProxy) { - if (imageProxy.getFormat() == ImageFormat.YUV_420_888) { - Image image = imageProxy.getImage(); - assert image != null; + imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> { + if (imageProxy.getFormat() == ImageFormat.YUV_420_888) { + Image image = imageProxy.getImage(); + assert image != null; + + + // Luminosity Detection + //float luminosity = calculateLuminosity(image); + //if (previousLuminosity != null) + // checkForViolation(luminosity, previousLuminosity); + //previousLuminosity = luminosity; + + + // Changed Pixel Detection + int pixelChanged = getChangedPixelCount(image); + int width = image.getWidth(); + int height = image.getHeight(); + + float percentPixelChanged = (float) 100f * pixelChanged / (width * height); + if (percentPixelChanged > ALARM_THRESHOLD) + reportViolation("0", "Video", percentPixelChanged); - // Analyze frame - float luminosity = calculateLuminosity(image); - Log.d("Video Detector", String.valueOf(luminosity)); - if (previousLuminosity != null) - checkForViolation(luminosity, previousLuminosity); - previousLuminosity = luminosity; - } - imageProxy.close(); } + imageProxy.close(); }); // Create Preview Preview preview = new Preview.Builder().build(); @@ -149,11 +159,50 @@ public class VideoDetector extends Detector { } + /** 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(); + previousBuffer.put(buffer); + previousBuffer.rewind(); + } + + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; x++) { + int index = (y * yRowStride) + (x * yPixelStride); + int luminosity = (buffer.get(index) & 0xff); + int previousLuminosity = (previousBuffer.get(index) & 0xff); + int diff = Math.abs(luminosity - previousLuminosity); + if (diff > PIXEL_THRESHOLD) + changedPixelCount++; + } + } + // Reset and copy Byte Buffer + buffer.rewind(); + previousBuffer.rewind(); + previousBuffer.put(buffer); + + return changedPixelCount; + } + + /** Check if delta Luminosity exceeds threshold */ private void checkForViolation(float luminosity, float previousLuminosity) { float deltaLuminosity = Math.abs(luminosity - previousLuminosity); if (deltaLuminosity > DELTA_LUMINOSITY_THRESHOLD) { - reportViolation("1232", "Video",luminosity); + reportViolation("0", "Video",luminosity); Log.d("Violation", "Violation"); } Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity))); From 74a291bdd6a83c153dd2829da9dfc1480d41c872 Mon Sep 17 00:00:00 2001 From: Bastian Kohler Date: Sun, 28 May 2023 22:28:23 +0200 Subject: [PATCH 3/3] Removed olf Luminosity detection --- .../VideoDetection/VideoDetector.java | 57 ++----------------- 1 file changed, 5 insertions(+), 52 deletions(-) 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 d7d9a16..01baf7a 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java @@ -3,7 +3,6 @@ package com.example.ueberwachungssystem.VideoDetection; import android.content.Context; import android.graphics.ImageFormat; import android.media.Image; -import android.util.Log; import android.util.Size; import androidx.annotation.NonNull; @@ -32,12 +31,9 @@ public class VideoDetector extends Detector { private Boolean isDetectionRunning = false; // Preview Camera Image private PreviewView previewView = null; - // Check Violation - private final float DELTA_LUMINOSITY_THRESHOLD = 1.5f; - private Float previousLuminosity = null; - - private static final int PIXEL_THRESHOLD = 50; // Luminosity (brightness channel of YUV_420_888) - private static final int ALARM_THRESHOLD = 1; // Percent of pixels changed + // 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 ByteBuffer previousBuffer = null; @@ -101,23 +97,15 @@ public class VideoDetector extends Detector { Image image = imageProxy.getImage(); assert image != null; - - // Luminosity Detection - //float luminosity = calculateLuminosity(image); - //if (previousLuminosity != null) - // checkForViolation(luminosity, previousLuminosity); - //previousLuminosity = luminosity; - - // Changed Pixel Detection int pixelChanged = getChangedPixelCount(image); int width = image.getWidth(); int height = image.getHeight(); float percentPixelChanged = (float) 100f * pixelChanged / (width * height); + if (percentPixelChanged > ALARM_THRESHOLD) reportViolation("0", "Video", percentPixelChanged); - } imageProxy.close(); }); @@ -134,31 +122,6 @@ public class VideoDetector extends Detector { } - /** Return Luminosity from Image */ - private float calculateLuminosity (Image image) { - int width = image.getWidth(); - int height = image.getHeight(); - - Image.Plane[] planes = image.getPlanes(); - ByteBuffer luminosityBuffer = planes[0].getBuffer(); - - int yRowStride = image.getPlanes()[0].getRowStride(); - int yPixelStride = image.getPlanes()[0].getPixelStride(); - - int luminosity; - float sum = 0; - - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; x++) { - int index = (y * yRowStride) + (x * yPixelStride); - luminosity = (luminosityBuffer.get(index) & 0xff); - sum += luminosity; - } - } - return sum / (width * height); - } - - /** Calculate Amount of Pixels changed */ private int getChangedPixelCount(Image image) { int width = image.getWidth(); @@ -177,6 +140,7 @@ public class VideoDetector extends Detector { buffer.rewind(); previousBuffer.put(buffer); previousBuffer.rewind(); + return 0; } for (int y = 0; y < height; ++y) { @@ -196,15 +160,4 @@ public class VideoDetector extends Detector { return changedPixelCount; } - - - /** Check if delta Luminosity exceeds threshold */ - private void checkForViolation(float luminosity, float previousLuminosity) { - float deltaLuminosity = Math.abs(luminosity - previousLuminosity); - if (deltaLuminosity > DELTA_LUMINOSITY_THRESHOLD) { - reportViolation("0", "Video",luminosity); - Log.d("Violation", "Violation"); - } - Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity))); - } }