From 3d799ae4610e589cb4bd72b574e801a738d1e4dc Mon Sep 17 00:00:00 2001 From: Bastian Kohler Date: Thu, 18 May 2023 15:05:09 +0200 Subject: [PATCH] Rebuild Demo in Main Activity --- .../ueberwachungssystem/MainActivity.java | 32 +++++++------------ .../VideoDetection/DetectionReport.java | 19 +++++++---- .../VideoDetection/VideoDetector.java | 8 +++-- app/src/main/res/layout/activity_main.xml | 15 +++++---- 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index 4e1d659..9fbbb65 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -2,31 +2,22 @@ package com.example.ueberwachungssystem; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; -import androidx.camera.core.Camera; -import androidx.camera.core.CameraSelector; -import androidx.camera.core.Preview; -import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.camera.view.PreviewView; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.camera.core.ExperimentalGetImage; -import androidx.lifecycle.LifecycleOwner; import android.Manifest; import android.content.pm.PackageManager; +import android.graphics.Color; import android.os.Bundle; -import android.util.Log; import android.view.View; -import android.widget.Button; import android.widget.TextView; import android.widget.Toast; +import android.widget.ToggleButton; import com.example.ueberwachungssystem.VideoDetection.DetectionReport; import com.example.ueberwachungssystem.VideoDetection.VideoDetector; -import com.google.common.util.concurrent.ListenableFuture; - -import java.util.Calendar; -import java.util.concurrent.ExecutionException; @ExperimentalGetImage public class MainActivity extends AppCompatActivity { @@ -34,7 +25,7 @@ public class MainActivity extends AppCompatActivity { private static final int CAMERA_PERMISSION_REQUEST_CODE = 101; private PreviewView previewView; private TextView textView; - private Button previewButton; + private ToggleButton toggleButton; private Boolean cameraPreviewIsRunning = false; private Boolean isPressed = false; @@ -46,7 +37,7 @@ public class MainActivity extends AppCompatActivity { textView = findViewById(R.id.textView); previewView = findViewById(R.id.previewView); - previewButton = findViewById(R.id.previewButton); + toggleButton = findViewById(R.id.previewButton); VideoDetector vd = new VideoDetector(this); @@ -55,23 +46,22 @@ public class MainActivity extends AppCompatActivity { @Override public void onDetection(DetectionReport detectionReport) { detectionReport.log("OnDetection"); + textView.setText(detectionReport.toString()); } }); - vd.startDetection(); + //vd.startDetection(); - previewButton.setOnClickListener(new View.OnClickListener() { + toggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getCameraAccess(); - if (isCameraAccessAllowed() && !isPressed) + if (isCameraAccessAllowed() && toggleButton.isChecked()) { - vd.stopDetection(); - isPressed = true; - } - else if (isCameraAccessAllowed() && isPressed) { vd.startDetection(); - isPressed = false; + } + else { + vd.stopDetection(); } } }); diff --git a/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/DetectionReport.java b/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/DetectionReport.java index c73d91e..fff61dd 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/DetectionReport.java +++ b/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/DetectionReport.java @@ -18,14 +18,19 @@ public class DetectionReport { this.detectorID = detectorID; } + + /** Get Detection Report in String format */ + public String toString() { + String time = "Time: " + "[" + this.timeStamp + "]"; + String type = "Type: " + "[" + this.detectionType + "]"; + String value = "Value: " + "[" + this.detectedValue + "]"; + String id = "ID: " + "[" + this.detectorID + "]"; + + return String.join("\t", time, type, value, id); + } + /** Debug Report */ public void log(String tag) { - String time = "Time: [" + this.timeStamp + "]"; - String type = "Type: [" + this.detectionType + "]"; - String value = "Value: [" + this.detectedValue + "]"; - String id = "ID: [" + this.detectorID + "]"; - - String message = String.join("\t", time, type, value, id); - Log.d(tag, message); + Log.d(tag, this.toString()); } } \ No newline at end of file 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 c09c849..1d9e8d9 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java @@ -34,8 +34,8 @@ public class VideoDetector { // Preview Camera Image private PreviewView previewView = null; // Check Violation - private final float DELTA_LUMINOSITY_THRESHOLD = 0.5f; - private float previousLuminosity = 0f; + private final float DELTA_LUMINOSITY_THRESHOLD = 0.3f; + private Float previousLuminosity = null; // On Detection Listener private OnDetectionListener listener; @@ -124,7 +124,8 @@ public class VideoDetector { // Analyze frame float luminosity = calculateLuminosity(image); Log.d("Video Detector", String.valueOf(luminosity)); - checkForViolation(luminosity, previousLuminosity); + if (previousLuminosity != null) + checkForViolation(luminosity, previousLuminosity); previousLuminosity = luminosity; } imageProxy.close(); @@ -139,6 +140,7 @@ public class VideoDetector { preview.setSurfaceProvider(previewView.getSurfaceProvider()); cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview); + stopDetection(); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6cc655f..1ec07d4 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -13,23 +13,24 @@ android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Hello World!" + android:text="" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> -