Added comments to VideoDetector class

This commit is contained in:
Bastian Kohler 2023-06-02 17:48:41 +02:00
parent 95919a6602
commit 047d45f1cb

View File

@ -20,6 +20,22 @@ 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;
/**
* Video Detector inherits some methods from abstract Detector class (more info there)
*
*
* USE FROM MAIN ACTIVITY:
*
* VideoDetector vd = new VideoDetector(this);
* vd.setPreview(previewView); //THIS IS OPTIONAL!
* vd.setOnDetectionListener(new Detector.OnDetectionListener{...});
* vd.startDetection();
* vd.stopDetection
*
* */
@ExperimentalGetImage @ExperimentalGetImage
public class VideoDetector extends Detector { public class VideoDetector extends Detector {
@ -37,14 +53,19 @@ public class VideoDetector extends Detector {
/** Constructor */ /**
* Constructor
* @param context: the context of calling activity (usually "this")
* */
public VideoDetector(Context context) { public VideoDetector(Context context) {
super(); super();
this.context = context; this.context = context;
} }
/** Starts Video Detection */ /**
* Starts the Video Detection
* */
@Override @Override
public void startDetection() { public void startDetection() {
if (isDetectionRunning) if (isDetectionRunning)
@ -55,7 +76,7 @@ public class VideoDetector extends Detector {
cameraProviderFuture.addListener(() -> { cameraProviderFuture.addListener(() -> {
try { try {
cameraProvider = cameraProviderFuture.get(); cameraProvider = cameraProviderFuture.get();
bindLuminosityAnalysis(cameraProvider); bindAnalysis(cameraProvider);
isDetectionRunning = true; isDetectionRunning = true;
previousBuffer = null; previousBuffer = null;
} catch (ExecutionException | InterruptedException e) { } catch (ExecutionException | InterruptedException e) {
@ -65,7 +86,9 @@ public class VideoDetector extends Detector {
} }
/** Stops Video Detection */ /**
* Stops the Video Detection
* */
@Override @Override
public void stopDetection() { public void stopDetection() {
if (!isDetectionRunning) if (!isDetectionRunning)
@ -75,14 +98,20 @@ public class VideoDetector extends Detector {
} }
/** Set PreviewView to show Image */ /**
* Set PreviewView to show video feed while detecting
* this is optional and does not need to be called
* */
public void setPreviewView(PreviewView previewView) { public void setPreviewView(PreviewView previewView) {
this.previewView = previewView; this.previewView = previewView;
} }
/** Binds the Luminosity Analyzer (configure and run Analysis) */ /**
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) { * Binds the Luminosity Analyzer (configure and run Analysis)
* @param cameraProvider: CameraProvider of Context passed by Constructor
* */
private void bindAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
// Configure and create Image Analysis // Configure and create Image Analysis
ImageAnalysis.Builder builder = new ImageAnalysis.Builder(); ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
builder.setTargetResolution(new Size(640, 480)); builder.setTargetResolution(new Size(640, 480));
@ -121,7 +150,10 @@ public class VideoDetector extends Detector {
/** Calculate Amount of Pixels changed */ /**
* Calculate Amount of Pixels changed using a threshold
* @param image
* */
private int getChangedPixelCount(Image image) { private int getChangedPixelCount(Image image) {
Image.Plane[] planes = image.getPlanes(); Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer(); ByteBuffer buffer = planes[0].getBuffer();