Changed VideoDetector.java to detect based on changed pixel count instead of average luminosity
This commit is contained in:
parent
9fce3be6a8
commit
ba6765b1fb
@ -10,7 +10,6 @@ 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;
|
||||||
@ -37,10 +36,15 @@ public class VideoDetector extends Detector {
|
|||||||
private final float DELTA_LUMINOSITY_THRESHOLD = 1.5f;
|
private final float DELTA_LUMINOSITY_THRESHOLD = 1.5f;
|
||||||
private Float previousLuminosity = null;
|
private Float previousLuminosity = null;
|
||||||
|
|
||||||
|
private static final int PIXEL_THRESHOLD = 50; // Luminosity (brightness channel of YUV_420_888)
|
||||||
|
private static final int ALARM_THRESHOLD = 1; // Percent of pixels changed
|
||||||
|
private ByteBuffer previousBuffer = null;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Constructor */
|
/** Constructor */
|
||||||
public VideoDetector(Context context) {
|
public VideoDetector(Context context) {
|
||||||
|
|
||||||
super();
|
super();
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
@ -54,16 +58,13 @@ public class VideoDetector extends Detector {
|
|||||||
// 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
|
try {
|
||||||
public void run() {
|
cameraProvider = cameraProviderFuture.get();
|
||||||
try {
|
bindLuminosityAnalysis(cameraProvider);
|
||||||
cameraProvider = cameraProviderFuture.get();
|
isDetectionRunning = true;
|
||||||
bindLuminosityAnalysis(cameraProvider);
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
isDetectionRunning = true;
|
// No errors need to be handled for this Future. This should never be reached.
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
|
||||||
// No errors need to be handled for this Future. This should never be reached.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},ContextCompat.getMainExecutor(context));
|
},ContextCompat.getMainExecutor(context));
|
||||||
}
|
}
|
||||||
@ -91,25 +92,34 @@ 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), new ImageAnalysis.Analyzer() {
|
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> {
|
||||||
@Override
|
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
||||||
public void analyze(@NonNull ImageProxy imageProxy) {
|
Image image = imageProxy.getImage();
|
||||||
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
assert image != null;
|
||||||
Image image = imageProxy.getImage();
|
|
||||||
assert image != null;
|
|
||||||
|
// Luminosity Detection
|
||||||
|
//float luminosity = calculateLuminosity(image);
|
||||||
|
//if (previousLuminosity != null)
|
||||||
|
// checkForViolation(luminosity, previousLuminosity);
|
||||||
|
//previousLuminosity = luminosity;
|
||||||
|
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
// Analyze frame
|
|
||||||
float luminosity = calculateLuminosity(image);
|
|
||||||
Log.d("Video Detector", String.valueOf(luminosity));
|
|
||||||
if (previousLuminosity != null)
|
|
||||||
checkForViolation(luminosity, previousLuminosity);
|
|
||||||
previousLuminosity = luminosity;
|
|
||||||
}
|
|
||||||
imageProxy.close();
|
|
||||||
}
|
}
|
||||||
|
imageProxy.close();
|
||||||
});
|
});
|
||||||
// Create Preview
|
// Create Preview
|
||||||
Preview preview = new Preview.Builder().build();
|
Preview preview = new Preview.Builder().build();
|
||||||
@ -149,11 +159,50 @@ public class VideoDetector extends Detector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Calculate Amount of Pixels changed */
|
||||||
|
private int getChangedPixelCount(Image image) {
|
||||||
|
int width = image.getWidth();
|
||||||
|
int height = image.getHeight();
|
||||||
|
|
||||||
|
Image.Plane[] planes = image.getPlanes();
|
||||||
|
ByteBuffer buffer = planes[0].getBuffer();
|
||||||
|
|
||||||
|
int yRowStride = image.getPlanes()[0].getRowStride();
|
||||||
|
int yPixelStride = image.getPlanes()[0].getPixelStride();
|
||||||
|
|
||||||
|
int changedPixelCount = 0;
|
||||||
|
|
||||||
|
if (previousBuffer == null) {
|
||||||
|
previousBuffer = ByteBuffer.allocate(buffer.capacity());
|
||||||
|
buffer.rewind();
|
||||||
|
previousBuffer.put(buffer);
|
||||||
|
previousBuffer.rewind();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Check if delta Luminosity exceeds threshold */
|
/** Check if delta Luminosity exceeds threshold */
|
||||||
private void checkForViolation(float luminosity, float previousLuminosity) {
|
private void checkForViolation(float luminosity, float previousLuminosity) {
|
||||||
float deltaLuminosity = Math.abs(luminosity - previousLuminosity);
|
float deltaLuminosity = Math.abs(luminosity - previousLuminosity);
|
||||||
if (deltaLuminosity > DELTA_LUMINOSITY_THRESHOLD) {
|
if (deltaLuminosity > DELTA_LUMINOSITY_THRESHOLD) {
|
||||||
reportViolation("1232", "Video",luminosity);
|
reportViolation("0", "Video",luminosity);
|
||||||
Log.d("Violation", "Violation");
|
Log.d("Violation", "Violation");
|
||||||
}
|
}
|
||||||
Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity)));
|
Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity)));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user