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.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;
|
||||
@ -37,10 +36,15 @@ public class VideoDetector extends Detector {
|
||||
private final float DELTA_LUMINOSITY_THRESHOLD = 1.5f;
|
||||
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 */
|
||||
public VideoDetector(Context context) {
|
||||
|
||||
super();
|
||||
this.context = context;
|
||||
}
|
||||
@ -54,9 +58,7 @@ public class VideoDetector extends Detector {
|
||||
// Request Camera Provider
|
||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||
//Check for Camera availability
|
||||
cameraProviderFuture.addListener(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cameraProviderFuture.addListener(() -> {
|
||||
try {
|
||||
cameraProvider = cameraProviderFuture.get();
|
||||
bindLuminosityAnalysis(cameraProvider);
|
||||
@ -64,7 +66,6 @@ public class VideoDetector extends Detector {
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
// No errors need to be handled for this Future. This should never be reached.
|
||||
}
|
||||
}
|
||||
},ContextCompat.getMainExecutor(context));
|
||||
}
|
||||
|
||||
@ -91,25 +92,34 @@ public class VideoDetector extends Detector {
|
||||
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) {
|
||||
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;
|
||||
|
||||
// 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);
|
||||
|
||||
}
|
||||
imageProxy.close();
|
||||
}
|
||||
});
|
||||
// Create Preview
|
||||
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 */
|
||||
private void checkForViolation(float luminosity, float previousLuminosity) {
|
||||
float deltaLuminosity = Math.abs(luminosity - previousLuminosity);
|
||||
if (deltaLuminosity > DELTA_LUMINOSITY_THRESHOLD) {
|
||||
reportViolation("1232", "Video",luminosity);
|
||||
reportViolation("0", "Video",luminosity);
|
||||
Log.d("Violation", "Violation");
|
||||
}
|
||||
Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity)));
|
||||
|
Loading…
x
Reference in New Issue
Block a user