Compare commits

...

5 Commits

3 changed files with 59 additions and 83 deletions

View File

@ -1,6 +1,5 @@
package com.example.ueberwachungssystem;
import android.content.Context;
import androidx.annotation.NonNull;
@ -8,7 +7,7 @@ abstract public class Detector {
private OnDetectionListener listener;
/** Constructor - takes context of current activity */
public Detector(Context context) {};
public Detector() {}
/** On Detection Listener - runs when violation is reported */

View File

@ -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) {

View File

@ -3,28 +3,26 @@ 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;
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;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;
import com.example.ueberwachungssystem.DetectionReport;
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 {
public class VideoDetector extends Detector {
// Calling Activity
private final Context context;
@ -33,64 +31,43 @@ public class VideoDetector {
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;
// On Detection Listener
private OnDetectionListener listener;
// 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;
/** Constructor */
public VideoDetector(Context context) {
super();
this.context = context;
}
/** On Detection Listener - runs when a violation is reported */
public interface OnDetectionListener {
void onDetection(DetectionReport detectionReport);
}
public void setOnDetectionListener(OnDetectionListener listener) {
this.listener = listener;
}
private void reportViolation(float amplitude) {
if (listener != null) {
DetectionReport detectionReport = new DetectionReport("123", "Video", amplitude);
listener.onDetection(detectionReport);
}
}
/** Return State of Video Detector */
public Boolean isRunning() {
return isDetectionRunning;
}
/** 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(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));
}
/** Stops Video Detection */
@Override
public void stopDetection() {
if (!isDetectionRunning)
return;
@ -111,25 +88,26 @@ public class VideoDetector {
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;
// Analyze frame
float luminosity = calculateLuminosity(image);
Log.d("Video Detector", String.valueOf(luminosity));
if (previousLuminosity != null)
checkForViolation(luminosity, previousLuminosity);
previousLuminosity = luminosity;
}
imageProxy.close();
// 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();
@ -144,38 +122,42 @@ public class VideoDetector {
}
/** Return Luminosity from Image */
private float calculateLuminosity (Image image) {
/** Calculate Amount of Pixels changed */
private int getChangedPixelCount(Image image) {
int width = image.getWidth();
int height = image.getHeight();
Image.Plane[] planes = image.getPlanes();
ByteBuffer luminosityBuffer = planes[0].getBuffer();
ByteBuffer buffer = planes[0].getBuffer();
int yRowStride = image.getPlanes()[0].getRowStride();
int yPixelStride = image.getPlanes()[0].getPixelStride();
int luminosity;
float sum = 0;
int changedPixelCount = 0;
if (previousBuffer == null) {
previousBuffer = ByteBuffer.allocate(buffer.capacity());
buffer.rewind();
previousBuffer.put(buffer);
previousBuffer.rewind();
return 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;
int luminosity = (buffer.get(index) & 0xff);
int previousLuminosity = (previousBuffer.get(index) & 0xff);
int diff = Math.abs(luminosity - previousLuminosity);
if (diff > PIXEL_THRESHOLD)
changedPixelCount++;
}
}
return sum / (width * height);
}
// Reset and copy Byte Buffer
buffer.rewind();
previousBuffer.rewind();
previousBuffer.put(buffer);
/** 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(luminosity);
Log.d("Violation", "Violation");
}
Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity)));
return changedPixelCount;
}
}