Browse Source

Refactor

pull/4/head
Bastian Kohler 1 year ago
parent
commit
19d1bfe1e3

+ 1
- 0
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

} }
else { else {
vd.stopDetection(); vd.stopDetection();
textView.setText("");
} }
} }
}); });

+ 14
- 12
app/src/main/java/com/example/ueberwachungssystem/VideoDetection/VideoDetector.java View File

// Preview Camera Image // Preview Camera Image
private PreviewView previewView = null; private PreviewView previewView = null;
// Detect Violation // 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 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; private ByteBuffer previousBuffer = null;






/** Constructor */ /** Constructor */
public VideoDetector(Context context) { public VideoDetector(Context context) {

super(); super();
this.context = context; this.context = context;
} }
cameraProvider = cameraProviderFuture.get(); cameraProvider = cameraProviderFuture.get();
bindLuminosityAnalysis(cameraProvider); bindLuminosityAnalysis(cameraProvider);
isDetectionRunning = true; isDetectionRunning = true;
previousBuffer = null;
} catch (ExecutionException | InterruptedException e) { } catch (ExecutionException | InterruptedException e) {
// No errors need to be handled for this Future. This should never be reached. // No errors need to be handled for this Future. This should never be reached.
} }
preview.setSurfaceProvider(previewView.getSurfaceProvider()); preview.setSurfaceProvider(previewView.getSurfaceProvider());


cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview); cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
stopDetection();
} }





/** Calculate Amount of Pixels changed */ /** Calculate Amount of Pixels changed */
private int getChangedPixelCount(Image image) { private int getChangedPixelCount(Image image) {
int width = image.getWidth();
int height = image.getHeight();

Image.Plane[] planes = image.getPlanes(); Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer(); ByteBuffer buffer = planes[0].getBuffer();


int yRowStride = image.getPlanes()[0].getRowStride();
int yPixelStride = image.getPlanes()[0].getPixelStride();

int changedPixelCount = 0;

if (previousBuffer == null) { if (previousBuffer == null) {
previousBuffer = ByteBuffer.allocate(buffer.capacity()); previousBuffer = ByteBuffer.allocate(buffer.capacity());
buffer.rewind(); buffer.rewind();
return 0; 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 y = 0; y < height; ++y) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
int index = (y * yRowStride) + (x * yPixelStride); int index = (y * yRowStride) + (x * yPixelStride);
changedPixelCount++; changedPixelCount++;
} }
} }

// Reset and copy Byte Buffer // Reset and copy Byte Buffer
buffer.rewind(); buffer.rewind();
previousBuffer.rewind(); previousBuffer.rewind();

Loading…
Cancel
Save