|
|
@@ -20,6 +20,22 @@ import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
import java.nio.ByteBuffer; |
|
|
|
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 |
|
|
|
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) { |
|
|
|
super(); |
|
|
|
this.context = context; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** Starts Video Detection */ |
|
|
|
/** |
|
|
|
* Starts the Video Detection |
|
|
|
* */ |
|
|
|
@Override |
|
|
|
public void startDetection() { |
|
|
|
if (isDetectionRunning) |
|
|
@@ -55,7 +76,7 @@ public class VideoDetector extends Detector { |
|
|
|
cameraProviderFuture.addListener(() -> { |
|
|
|
try { |
|
|
|
cameraProvider = cameraProviderFuture.get(); |
|
|
|
bindLuminosityAnalysis(cameraProvider); |
|
|
|
bindAnalysis(cameraProvider); |
|
|
|
isDetectionRunning = true; |
|
|
|
previousBuffer = null; |
|
|
|
} catch (ExecutionException | InterruptedException e) { |
|
|
@@ -65,7 +86,9 @@ public class VideoDetector extends Detector { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** Stops Video Detection */ |
|
|
|
/** |
|
|
|
* Stops the Video Detection |
|
|
|
* */ |
|
|
|
@Override |
|
|
|
public void stopDetection() { |
|
|
|
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) { |
|
|
|
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 |
|
|
|
ImageAnalysis.Builder builder = new ImageAnalysis.Builder(); |
|
|
|
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) { |
|
|
|
Image.Plane[] planes = image.getPlanes(); |
|
|
|
ByteBuffer buffer = planes[0].getBuffer(); |