Compare commits
No commits in common. "74a291bdd6a83c153dd2829da9dfc1480d41c872" and "37617dbd545a82d8e6477dab5fba0c31119ee85d" have entirely different histories.
74a291bdd6
...
37617dbd54
@ -1,5 +1,6 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
|
||||||
@ -7,7 +8,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() {}
|
public Detector(Context context) {};
|
||||||
|
|
||||||
|
|
||||||
/** On Detection Listener - runs when violation is reported */
|
/** On Detection Listener - runs when violation is reported */
|
||||||
|
@ -21,6 +21,11 @@ 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
|
||||||
@ -28,15 +33,16 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
TextView textView = findViewById(R.id.textView);
|
textView = findViewById(R.id.textView);
|
||||||
PreviewView previewView = findViewById(R.id.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(@NonNull DetectionReport detectionReport) {
|
public void onDetection(DetectionReport detectionReport) {
|
||||||
detectionReport.log("OnDetection");
|
detectionReport.log("OnDetection");
|
||||||
textView.setText(detectionReport.toString());
|
textView.setText(detectionReport.toString());
|
||||||
}
|
}
|
||||||
@ -44,7 +50,6 @@ 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,26 +3,28 @@ 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.Detector;
|
import com.example.ueberwachungssystem.DetectionReport;
|
||||||
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 extends Detector {
|
public class VideoDetector {
|
||||||
|
|
||||||
// Calling Activity
|
// Calling Activity
|
||||||
private final Context context;
|
private final Context context;
|
||||||
@ -31,43 +33,64 @@ public class VideoDetector extends Detector {
|
|||||||
private Boolean isDetectionRunning = false;
|
private Boolean isDetectionRunning = false;
|
||||||
// Preview Camera Image
|
// Preview Camera Image
|
||||||
private PreviewView previewView = null;
|
private PreviewView previewView = null;
|
||||||
// Detect Violation
|
// Check Violation
|
||||||
private static final int PIXEL_THRESHOLD = 60; // Luminosity (brightness channel of YUV_420_888)
|
private final float DELTA_LUMINOSITY_THRESHOLD = 1.5f;
|
||||||
private static final float ALARM_THRESHOLD = 0.5f; // Percent of pixels changed
|
private Float previousLuminosity = null;
|
||||||
private ByteBuffer previousBuffer = null;
|
// On Detection Listener
|
||||||
|
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(() -> {
|
cameraProviderFuture.addListener(new Runnable() {
|
||||||
try {
|
@Override
|
||||||
cameraProvider = cameraProviderFuture.get();
|
public void run() {
|
||||||
bindLuminosityAnalysis(cameraProvider);
|
try {
|
||||||
isDetectionRunning = true;
|
cameraProvider = cameraProviderFuture.get();
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
bindLuminosityAnalysis(cameraProvider);
|
||||||
// No errors need to be handled for this Future. This should never be reached.
|
isDetectionRunning = true;
|
||||||
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
|
// 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;
|
||||||
@ -88,26 +111,25 @@ public class VideoDetector extends Detector {
|
|||||||
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), imageProxy -> {
|
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), new ImageAnalysis.Analyzer() {
|
||||||
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
@Override
|
||||||
Image image = imageProxy.getImage();
|
public void analyze(@NonNull ImageProxy imageProxy) {
|
||||||
assert image != null;
|
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
||||||
|
Image image = imageProxy.getImage();
|
||||||
|
assert image != null;
|
||||||
|
|
||||||
// Changed Pixel Detection
|
// Analyze frame
|
||||||
int pixelChanged = getChangedPixelCount(image);
|
float luminosity = calculateLuminosity(image);
|
||||||
int width = image.getWidth();
|
Log.d("Video Detector", String.valueOf(luminosity));
|
||||||
int height = image.getHeight();
|
if (previousLuminosity != null)
|
||||||
|
checkForViolation(luminosity, previousLuminosity);
|
||||||
float percentPixelChanged = (float) 100f * pixelChanged / (width * height);
|
previousLuminosity = luminosity;
|
||||||
|
}
|
||||||
if (percentPixelChanged > ALARM_THRESHOLD)
|
imageProxy.close();
|
||||||
reportViolation("0", "Video", percentPixelChanged);
|
|
||||||
}
|
}
|
||||||
imageProxy.close();
|
|
||||||
});
|
});
|
||||||
// Create Preview
|
// Create Preview
|
||||||
Preview preview = new Preview.Builder().build();
|
Preview preview = new Preview.Builder().build();
|
||||||
@ -122,42 +144,38 @@ public class VideoDetector extends Detector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Calculate Amount of Pixels changed */
|
/** Return Luminosity from Image */
|
||||||
private int getChangedPixelCount(Image image) {
|
private float calculateLuminosity (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 buffer = planes[0].getBuffer();
|
ByteBuffer luminosityBuffer = 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 changedPixelCount = 0;
|
int luminosity;
|
||||||
|
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);
|
||||||
int luminosity = (buffer.get(index) & 0xff);
|
luminosity = (luminosityBuffer.get(index) & 0xff);
|
||||||
int previousLuminosity = (previousBuffer.get(index) & 0xff);
|
sum += luminosity;
|
||||||
int diff = Math.abs(luminosity - previousLuminosity);
|
|
||||||
if (diff > PIXEL_THRESHOLD)
|
|
||||||
changedPixelCount++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Reset and copy Byte Buffer
|
return sum / (width * height);
|
||||||
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