|
|
|
|
|
|
|
|
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 { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 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) |
|
|
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Stops Video Detection */ |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Stops the Video Detection |
|
|
|
|
|
* */ |
|
|
@Override |
|
|
@Override |
|
|
public void stopDetection() { |
|
|
public void stopDetection() { |
|
|
if (!isDetectionRunning) |
|
|
if (!isDetectionRunning) |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 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)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 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(); |