Compare commits

...
3 Commits
5 changed files with 47 additions and 107 deletions

No files matched your search

@@ -1,4 +1,4 @@
package com.example.ueberwachungssystem;
package com.example.ueberwachungssystem.Detection;
import android.util.Log;
@@ -1,4 +1,4 @@
package com.example.ueberwachungssystem;
package com.example.ueberwachungssystem.Detection;
import androidx.annotation.NonNull;
@@ -1,4 +1,4 @@
package com.example.ueberwachungssystem.VideoDetection;
package com.example.ueberwachungssystem.Detection;
import android.content.Context;
import android.graphics.ImageFormat;
@@ -15,12 +15,27 @@ import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;
import com.example.ueberwachungssystem.Detector;
import com.google.common.util.concurrent.ListenableFuture;
import java.nio.ByteBuffer;
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);
* vd.setPreview(previewView); //THIS IS OPTIONAL!
* vd.setOnDetectionListener(new Detector.OnDetectionListener{...});
* vd.startDetection();
* vd.stopDetection
*
* */
@ExperimentalGetImage
public class VideoDetector extends Detector {
@@ -33,19 +48,24 @@ public class VideoDetector extends Detector {
private PreviewView previewView = null;
// Detect Violation
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 static final float ALARM_THRESHOLD = 0.2f; // Percent of pixels changed
private ByteBuffer previousBuffer = null;
/** Constructor */
/**
* Constructor
* @param context: the context of calling activity (usually "this")
* */
public VideoDetector(Context context) {
super();
this.context = context;
}
/** Starts Video Detection */
/**
* Starts the Video Detection
* */
@Override
public void startDetection() {
if (isDetectionRunning)
@@ -56,7 +76,7 @@ public class VideoDetector extends Detector {
cameraProviderFuture.addListener(() -> {
try {
cameraProvider = cameraProviderFuture.get();
bindLuminosityAnalysis(cameraProvider);
bindAnalysis(cameraProvider);
isDetectionRunning = true;
previousBuffer = null;
} catch (ExecutionException | InterruptedException e) {
@@ -66,7 +86,9 @@ public class VideoDetector extends Detector {
}
/** Stops Video Detection */
/**
* Stops the Video Detection
* */
@Override
public void stopDetection() {
if (!isDetectionRunning)
@@ -76,14 +98,20 @@ public class VideoDetector extends Detector {
}
/** Set PreviewView to show Image */
/**
* Set PreviewView to show video feed while detecting
* this is optional and does not need to be called
* */
public void setPreviewView(PreviewView previewView) {
this.previewView = previewView;
}
/** Binds the Luminosity Analyzer (configure and run Analysis) */
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
/**
* Binds the Luminosity Analyzer (configure and run Analysis)
* @param cameraProvider: CameraProvider of Context passed by Constructor
* */
private void bindAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
// Configure and create Image Analysis
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
builder.setTargetResolution(new Size(640, 480));
@@ -122,7 +150,10 @@ public class VideoDetector extends Detector {
/** Calculate Amount of Pixels changed */
/**
* Calculate Amount of Pixels changed using a threshold
* @param image
* */
private int getChangedPixelCount(Image image) {
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
@@ -1,86 +1,14 @@
package com.example.ueberwachungssystem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.camera.core.ExperimentalGetImage;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.example.ueberwachungssystem.VideoDetection.VideoDetector;
@ExperimentalGetImage
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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(@NonNull DetectionReport detectionReport) {
detectionReport.log("OnDetection");
textView.setText(detectionReport.toString());
}
});
//vd.startDetection();
ToggleButton toggleButton = findViewById(R.id.previewButton);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getCameraAccess();
if (isCameraAccessAllowed() && toggleButton.isChecked())
{
vd.startDetection();
}
else {
vd.stopDetection();
textView.setText("");
}
}
});
}
private boolean isCameraAccessAllowed() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}
private void getCameraAccess() {
if (!isCameraAccessAllowed())
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0) {
boolean cameraRights = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (cameraRights) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
}
+3 -22
View File
@@ -1,37 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="@+id/previewButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@android:color/black"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>