Compare commits
2
Commits
19d1bfe1e3
..
ms
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35583a4cdd | ||
|
|
d22952d83b |
No files matched your search
Whitespace-only changes.
@@ -36,13 +36,4 @@ 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'
|
||||||
|
|
||||||
// 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}"
|
|
||||||
}
|
}
|
||||||
@@ -2,9 +2,6 @@
|
|||||||
<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-feature android:name="android.hardware.camera"/>
|
|
||||||
<uses-permission android:name="android.permission.CAMERA"/>
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem;
|
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.util.Calendar;
|
|
||||||
|
|
||||||
/** Detection Report Class */
|
|
||||||
public class DetectionReport {
|
|
||||||
public String timeStamp;
|
|
||||||
public String detectionType;
|
|
||||||
public float detectedValue;
|
|
||||||
public String detectorID;
|
|
||||||
|
|
||||||
public DetectionReport(String detectorID, String detectionType, float detectedAmplitude) {
|
|
||||||
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
|
|
||||||
this.detectionType = detectionType;
|
|
||||||
this.detectedValue = detectedAmplitude;
|
|
||||||
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) {
|
|
||||||
Log.d(tag, this.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
|
|
||||||
abstract public class Detector {
|
|
||||||
private OnDetectionListener listener;
|
|
||||||
|
|
||||||
/** 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 detectorID, String detectionType, float amplitude) {
|
|
||||||
if (listener != null) {
|
|
||||||
DetectionReport detectionReport = new DetectionReport(detectorID, detectionType, amplitude);
|
|
||||||
listener.onDetection(detectionReport);
|
|
||||||
} else {
|
|
||||||
throw new IllegalStateException("No listener set for violation reporting");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** 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();
|
|
||||||
}
|
|
||||||
@@ -1,86 +1,15 @@
|
|||||||
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.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.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 {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
|
|
||||||
|
|
||||||
|
|
||||||
@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);
|
||||||
|
//
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,165 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem.VideoDetection;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.ImageFormat;
|
|
||||||
import android.media.Image;
|
|
||||||
import android.util.Size;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.camera.core.CameraSelector;
|
|
||||||
import androidx.camera.core.ExperimentalGetImage;
|
|
||||||
import androidx.camera.core.ImageAnalysis;
|
|
||||||
import androidx.camera.core.Preview;
|
|
||||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
|
||||||
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;
|
|
||||||
|
|
||||||
@ExperimentalGetImage
|
|
||||||
public class VideoDetector extends Detector {
|
|
||||||
|
|
||||||
// Calling Activity
|
|
||||||
private final Context context;
|
|
||||||
// Camera Provider
|
|
||||||
private ProcessCameraProvider cameraProvider;
|
|
||||||
private Boolean isDetectionRunning = false;
|
|
||||||
// Preview Camera Image
|
|
||||||
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 ByteBuffer previousBuffer = null;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Constructor */
|
|
||||||
public VideoDetector(Context context) {
|
|
||||||
super();
|
|
||||||
this.context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Starts Video Detection */
|
|
||||||
@Override
|
|
||||||
public void startDetection() {
|
|
||||||
if (isDetectionRunning)
|
|
||||||
return;
|
|
||||||
// Request Camera Provider
|
|
||||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
|
||||||
//Check for Camera availability
|
|
||||||
cameraProviderFuture.addListener(() -> {
|
|
||||||
try {
|
|
||||||
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.
|
|
||||||
}
|
|
||||||
},ContextCompat.getMainExecutor(context));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Stops Video Detection */
|
|
||||||
@Override
|
|
||||||
public void stopDetection() {
|
|
||||||
if (!isDetectionRunning)
|
|
||||||
return;
|
|
||||||
cameraProvider.unbindAll();
|
|
||||||
isDetectionRunning = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Set PreviewView to show Image */
|
|
||||||
public void setPreviewView(PreviewView previewView) {
|
|
||||||
this.previewView = previewView;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
|
||||||
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
|
||||||
// Configure and create Image Analysis
|
|
||||||
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), imageProxy -> {
|
|
||||||
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
|
||||||
Image image = imageProxy.getImage();
|
|
||||||
assert image != null;
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
});
|
|
||||||
// Create Preview
|
|
||||||
Preview preview = new Preview.Builder().build();
|
|
||||||
// Specify which Camera to use
|
|
||||||
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
|
||||||
// Update PreviewView if set
|
|
||||||
if (previewView != null)
|
|
||||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
|
||||||
|
|
||||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Calculate Amount of Pixels changed */
|
|
||||||
private int getChangedPixelCount(Image image) {
|
|
||||||
Image.Plane[] planes = image.getPlanes();
|
|
||||||
ByteBuffer buffer = planes[0].getBuffer();
|
|
||||||
|
|
||||||
if (previousBuffer == null) {
|
|
||||||
previousBuffer = ByteBuffer.allocate(buffer.capacity());
|
|
||||||
buffer.rewind();
|
|
||||||
previousBuffer.put(buffer);
|
|
||||||
previousBuffer.rewind();
|
|
||||||
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);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +1,18 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?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: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="center"
|
|
||||||
android:orientation="vertical"
|
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/textView"
|
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text=""
|
android:text="Hello World!"
|
||||||
android:textSize="20dp"
|
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
<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>
|
|
||||||
Reference in new issue
Block a user