Compare commits
5 Commits
37617dbd54
...
74a291bdd6
Author | SHA1 | Date | |
---|---|---|---|
74a291bdd6 | |||
ba6765b1fb | |||
9fce3be6a8 | |||
9892fc35bc | |||
62a528d33b |
@ -1,6 +1,5 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
|
||||||
@ -8,7 +7,7 @@ abstract public class Detector {
|
|||||||
private OnDetectionListener listener;
|
private OnDetectionListener listener;
|
||||||
|
|
||||||
/** Constructor - takes context of current activity */
|
/** Constructor - takes context of current activity */
|
||||||
public Detector(Context context) {};
|
public Detector() {}
|
||||||
|
|
||||||
|
|
||||||
/** On Detection Listener - runs when violation is reported */
|
/** On Detection Listener - runs when violation is reported */
|
||||||
|
@ -21,11 +21,6 @@ import com.example.ueberwachungssystem.VideoDetection.VideoDetector;
|
|||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
|
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
|
@Override
|
||||||
@ -33,16 +28,15 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
textView = findViewById(R.id.textView);
|
TextView textView = findViewById(R.id.textView);
|
||||||
previewView = findViewById(R.id.previewView);
|
PreviewView previewView = findViewById(R.id.previewView);
|
||||||
toggleButton = findViewById(R.id.previewButton);
|
|
||||||
|
|
||||||
|
|
||||||
VideoDetector vd = new VideoDetector(this);
|
VideoDetector vd = new VideoDetector(this);
|
||||||
vd.setPreviewView(previewView);
|
vd.setPreviewView(previewView);
|
||||||
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() {
|
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onDetection(DetectionReport detectionReport) {
|
public void onDetection(@NonNull DetectionReport detectionReport) {
|
||||||
detectionReport.log("OnDetection");
|
detectionReport.log("OnDetection");
|
||||||
textView.setText(detectionReport.toString());
|
textView.setText(detectionReport.toString());
|
||||||
}
|
}
|
||||||
@ -50,6 +44,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
//vd.startDetection();
|
//vd.startDetection();
|
||||||
|
|
||||||
|
|
||||||
|
ToggleButton toggleButton = findViewById(R.id.previewButton);
|
||||||
toggleButton.setOnClickListener(new View.OnClickListener() {
|
toggleButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
@ -3,28 +3,26 @@ package com.example.ueberwachungssystem.VideoDetection;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.ImageFormat;
|
import android.graphics.ImageFormat;
|
||||||
import android.media.Image;
|
import android.media.Image;
|
||||||
import android.util.Log;
|
|
||||||
import android.util.Size;
|
import android.util.Size;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.camera.core.CameraSelector;
|
import androidx.camera.core.CameraSelector;
|
||||||
import androidx.camera.core.ExperimentalGetImage;
|
import androidx.camera.core.ExperimentalGetImage;
|
||||||
import androidx.camera.core.ImageAnalysis;
|
import androidx.camera.core.ImageAnalysis;
|
||||||
import androidx.camera.core.ImageProxy;
|
|
||||||
import androidx.camera.core.Preview;
|
import androidx.camera.core.Preview;
|
||||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||||
import androidx.camera.view.PreviewView;
|
import androidx.camera.view.PreviewView;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.lifecycle.LifecycleOwner;
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
|
|
||||||
import com.example.ueberwachungssystem.DetectionReport;
|
import com.example.ueberwachungssystem.Detector;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
@ExperimentalGetImage
|
@ExperimentalGetImage
|
||||||
public class VideoDetector {
|
public class VideoDetector extends Detector {
|
||||||
|
|
||||||
// Calling Activity
|
// Calling Activity
|
||||||
private final Context context;
|
private final Context context;
|
||||||
@ -33,51 +31,30 @@ public class VideoDetector {
|
|||||||
private Boolean isDetectionRunning = false;
|
private Boolean isDetectionRunning = false;
|
||||||
// Preview Camera Image
|
// Preview Camera Image
|
||||||
private PreviewView previewView = null;
|
private PreviewView previewView = null;
|
||||||
// Check Violation
|
// Detect Violation
|
||||||
private final float DELTA_LUMINOSITY_THRESHOLD = 1.5f;
|
private static final int PIXEL_THRESHOLD = 60; // Luminosity (brightness channel of YUV_420_888)
|
||||||
private Float previousLuminosity = null;
|
private static final float ALARM_THRESHOLD = 0.5f; // Percent of pixels changed
|
||||||
// On Detection Listener
|
private ByteBuffer previousBuffer = null;
|
||||||
private OnDetectionListener listener;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Constructor */
|
/** Constructor */
|
||||||
public VideoDetector(Context context) {
|
public VideoDetector(Context context) {
|
||||||
|
|
||||||
|
super();
|
||||||
this.context = context;
|
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 */
|
/** Starts Video Detection */
|
||||||
|
@Override
|
||||||
public void startDetection() {
|
public void startDetection() {
|
||||||
if (isDetectionRunning)
|
if (isDetectionRunning)
|
||||||
return;
|
return;
|
||||||
// Request Camera Provider
|
// Request Camera Provider
|
||||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||||
//Check for Camera availability
|
//Check for Camera availability
|
||||||
cameraProviderFuture.addListener(new Runnable() {
|
cameraProviderFuture.addListener(() -> {
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
try {
|
||||||
cameraProvider = cameraProviderFuture.get();
|
cameraProvider = cameraProviderFuture.get();
|
||||||
bindLuminosityAnalysis(cameraProvider);
|
bindLuminosityAnalysis(cameraProvider);
|
||||||
@ -85,12 +62,12 @@ public class VideoDetector {
|
|||||||
} 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.
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},ContextCompat.getMainExecutor(context));
|
},ContextCompat.getMainExecutor(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Stops Video Detection */
|
/** Stops Video Detection */
|
||||||
|
@Override
|
||||||
public void stopDetection() {
|
public void stopDetection() {
|
||||||
if (!isDetectionRunning)
|
if (!isDetectionRunning)
|
||||||
return;
|
return;
|
||||||
@ -111,25 +88,26 @@ public class VideoDetector {
|
|||||||
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
||||||
builder.setTargetResolution(new Size(640, 480));
|
builder.setTargetResolution(new Size(640, 480));
|
||||||
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
||||||
|
builder.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888);
|
||||||
ImageAnalysis imageAnalysis = builder.build();
|
ImageAnalysis imageAnalysis = builder.build();
|
||||||
|
|
||||||
// Set Analyzer
|
// Set Analyzer
|
||||||
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), new ImageAnalysis.Analyzer() {
|
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> {
|
||||||
@Override
|
|
||||||
public void analyze(@NonNull ImageProxy imageProxy) {
|
|
||||||
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
||||||
Image image = imageProxy.getImage();
|
Image image = imageProxy.getImage();
|
||||||
assert image != null;
|
assert image != null;
|
||||||
|
|
||||||
// Analyze frame
|
// Changed Pixel Detection
|
||||||
float luminosity = calculateLuminosity(image);
|
int pixelChanged = getChangedPixelCount(image);
|
||||||
Log.d("Video Detector", String.valueOf(luminosity));
|
int width = image.getWidth();
|
||||||
if (previousLuminosity != null)
|
int height = image.getHeight();
|
||||||
checkForViolation(luminosity, previousLuminosity);
|
|
||||||
previousLuminosity = luminosity;
|
float percentPixelChanged = (float) 100f * pixelChanged / (width * height);
|
||||||
|
|
||||||
|
if (percentPixelChanged > ALARM_THRESHOLD)
|
||||||
|
reportViolation("0", "Video", percentPixelChanged);
|
||||||
}
|
}
|
||||||
imageProxy.close();
|
imageProxy.close();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
// Create Preview
|
// Create Preview
|
||||||
Preview preview = new Preview.Builder().build();
|
Preview preview = new Preview.Builder().build();
|
||||||
@ -144,38 +122,42 @@ public class VideoDetector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Return Luminosity from Image */
|
/** Calculate Amount of Pixels changed */
|
||||||
private float calculateLuminosity (Image image) {
|
private int getChangedPixelCount(Image image) {
|
||||||
int width = image.getWidth();
|
int width = image.getWidth();
|
||||||
int height = image.getHeight();
|
int height = image.getHeight();
|
||||||
|
|
||||||
Image.Plane[] planes = image.getPlanes();
|
Image.Plane[] planes = image.getPlanes();
|
||||||
ByteBuffer luminosityBuffer = planes[0].getBuffer();
|
ByteBuffer buffer = planes[0].getBuffer();
|
||||||
|
|
||||||
int yRowStride = image.getPlanes()[0].getRowStride();
|
int yRowStride = image.getPlanes()[0].getRowStride();
|
||||||
int yPixelStride = image.getPlanes()[0].getPixelStride();
|
int yPixelStride = image.getPlanes()[0].getPixelStride();
|
||||||
|
|
||||||
int luminosity;
|
int changedPixelCount = 0;
|
||||||
float sum = 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 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);
|
||||||
luminosity = (luminosityBuffer.get(index) & 0xff);
|
int luminosity = (buffer.get(index) & 0xff);
|
||||||
sum += luminosity;
|
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);
|
||||||
|
|
||||||
|
return changedPixelCount;
|
||||||
/** 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)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user