Added onDetectionListener to VideoDecoder
This commit is contained in:
parent
362af75b9a
commit
1576719d10
@ -15,13 +15,17 @@ import androidx.lifecycle.LifecycleOwner;
|
|||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.example.ueberwachungssystem.VideoDetection.DetectionReport;
|
||||||
|
import com.example.ueberwachungssystem.VideoDetection.VideoDetector;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
@ExperimentalGetImage
|
@ExperimentalGetImage
|
||||||
@ -47,8 +51,15 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
VideoDetector vd = new VideoDetector(this);
|
VideoDetector vd = new VideoDetector(this);
|
||||||
vd.setPreviewView(previewView);
|
vd.setPreviewView(previewView);
|
||||||
|
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() {
|
||||||
|
@Override
|
||||||
|
public void onDetection(DetectionReport detectionReport) {
|
||||||
|
Log.d("Listener", detectionReport.detectionType);
|
||||||
|
}
|
||||||
|
});
|
||||||
vd.startDetection();
|
vd.startDetection();
|
||||||
|
|
||||||
|
|
||||||
previewButton.setOnClickListener(new View.OnClickListener() {
|
previewButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
@ -1,154 +1,171 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem.VideoDetection;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.ImageFormat;
|
import android.graphics.ImageFormat;
|
||||||
import android.media.Image;
|
import android.media.Image;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.Size;
|
import android.util.Size;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
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.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;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.lifecycle.LifecycleOwner;
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
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;
|
||||||
|
|
||||||
@ExperimentalGetImage
|
|
||||||
public class VideoDetector {
|
@ExperimentalGetImage
|
||||||
// Calling Activity
|
public class VideoDetector {
|
||||||
private final Context context;
|
// Calling Activity
|
||||||
// Camera Provider
|
private final Context context;
|
||||||
private ProcessCameraProvider cameraProvider;
|
// Camera Provider
|
||||||
private Boolean isDetectionRunning = false;
|
private ProcessCameraProvider cameraProvider;
|
||||||
// Preview Camera Image
|
private Boolean isDetectionRunning = false;
|
||||||
private PreviewView previewView = null;
|
// Preview Camera Image
|
||||||
// Check Violation
|
private PreviewView previewView = null;
|
||||||
private final float DELTA_LUMINOSITY_THRESHOLD = 10f;
|
// Check Violation
|
||||||
private float lastLuminosity = 0f;
|
private final float DELTA_LUMINOSITY_THRESHOLD = 0.5f;
|
||||||
private Boolean isViolated = false;
|
private float previousLuminosity = 0f;
|
||||||
|
// On Detection Listener
|
||||||
|
private OnDetectionListener listener;
|
||||||
/** Constructor */
|
|
||||||
public VideoDetector(Context context) { this.context = context; }
|
|
||||||
|
|
||||||
|
/** Constructor */
|
||||||
|
public VideoDetector(Context context) { this.context = context; }
|
||||||
/** Return State of Video Detector */
|
|
||||||
public Boolean isRunning() { return isDetectionRunning; }
|
|
||||||
|
/** On Detection Listener - runs when a violation is reported */
|
||||||
/** Return the status of Violation */
|
public interface OnDetectionListener {
|
||||||
public boolean getViolationStatus()
|
public void onDetection(DetectionReport detectionReport);
|
||||||
{
|
}
|
||||||
return isViolated;
|
public void setOnDetectionListener(OnDetectionListener listener) {
|
||||||
}
|
this.listener = listener;
|
||||||
|
}
|
||||||
/** Starts Video Detection */
|
public void reportViolation(float amplitude) {
|
||||||
public void startDetection() {
|
DetectionReport detectionReport = new DetectionReport("123", "Video", amplitude);
|
||||||
isDetectionRunning = true;
|
if (listener != null)
|
||||||
// Request Camera Provider
|
listener.onDetection(detectionReport);
|
||||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
}
|
||||||
//Check for Camera availability
|
|
||||||
cameraProviderFuture.addListener(new Runnable() {
|
|
||||||
@Override
|
/** Return State of Video Detector */
|
||||||
public void run() {
|
public Boolean isRunning() {
|
||||||
try {
|
return isDetectionRunning;
|
||||||
cameraProvider = cameraProviderFuture.get();
|
}
|
||||||
bindLuminosityAnalysis(cameraProvider);
|
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
|
||||||
// No errors need to be handled for this Future. This should never be reached.
|
/** Starts Video Detection */
|
||||||
}
|
public void startDetection() {
|
||||||
}
|
isDetectionRunning = true;
|
||||||
},ContextCompat.getMainExecutor(context));
|
// Request Camera Provider
|
||||||
}
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||||
|
//Check for Camera availability
|
||||||
/** Stops Video Detection */
|
cameraProviderFuture.addListener(new Runnable() {
|
||||||
public void stopDetection() {
|
@Override
|
||||||
cameraProvider.unbindAll();
|
public void run() {
|
||||||
isDetectionRunning = false;
|
try {
|
||||||
}
|
cameraProvider = cameraProviderFuture.get();
|
||||||
|
bindLuminosityAnalysis(cameraProvider);
|
||||||
/** Set PreviewView to show Image */
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
public void setPreviewView(PreviewView previewView) {
|
// No errors need to be handled for this Future. This should never be reached.
|
||||||
this.previewView = previewView;
|
}
|
||||||
}
|
}
|
||||||
|
},ContextCompat.getMainExecutor(context));
|
||||||
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
}
|
||||||
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
|
||||||
// Configure and create Image Analysis
|
|
||||||
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
/** Stops Video Detection */
|
||||||
builder.setTargetResolution(new Size(640, 480));
|
public void stopDetection() {
|
||||||
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
cameraProvider.unbindAll();
|
||||||
ImageAnalysis imageAnalysis = builder.build();
|
isDetectionRunning = false;
|
||||||
|
}
|
||||||
// Set Analyzer
|
|
||||||
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), new ImageAnalysis.Analyzer() {
|
|
||||||
@Override
|
/** Set PreviewView to show Image */
|
||||||
public void analyze(@NonNull ImageProxy imageProxy) {
|
public void setPreviewView(PreviewView previewView) {
|
||||||
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
this.previewView = previewView;
|
||||||
Image image = imageProxy.getImage();
|
}
|
||||||
assert image != null;
|
|
||||||
|
|
||||||
// Analyze frame
|
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
||||||
float luminosity = calculateLuminosity(image);
|
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
||||||
Log.d("Video Detector", String.valueOf(luminosity));
|
// Configure and create Image Analysis
|
||||||
if (luminosity != 0)
|
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
||||||
checkForViolation(luminosity, lastLuminosity);
|
builder.setTargetResolution(new Size(640, 480));
|
||||||
lastLuminosity = luminosity;
|
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
||||||
}
|
ImageAnalysis imageAnalysis = builder.build();
|
||||||
imageProxy.close();
|
|
||||||
}
|
// Set Analyzer
|
||||||
});
|
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), new ImageAnalysis.Analyzer() {
|
||||||
// Create Preview
|
@Override
|
||||||
Preview preview = new Preview.Builder().build();
|
public void analyze(@NonNull ImageProxy imageProxy) {
|
||||||
// Specify which Camera to use
|
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
||||||
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
Image image = imageProxy.getImage();
|
||||||
// Update PreviewView if set
|
assert image != null;
|
||||||
if (previewView != null)
|
|
||||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
// Analyze frame
|
||||||
|
float luminosity = calculateLuminosity(image);
|
||||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
Log.d("Video Detector", String.valueOf(luminosity));
|
||||||
}
|
checkForViolation(luminosity, previousLuminosity);
|
||||||
|
previousLuminosity = luminosity;
|
||||||
/** Return Luminosity from Image */
|
}
|
||||||
private float calculateLuminosity (Image image) {
|
imageProxy.close();
|
||||||
int width = image.getWidth();
|
}
|
||||||
int height = image.getHeight();
|
});
|
||||||
|
// Create Preview
|
||||||
Image.Plane[] planes = image.getPlanes();
|
Preview preview = new Preview.Builder().build();
|
||||||
ByteBuffer luminosityBuffer = planes[0].getBuffer();
|
// Specify which Camera to use
|
||||||
|
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
||||||
int yRowStride = image.getPlanes()[0].getRowStride();
|
// Update PreviewView if set
|
||||||
int yPixelStride = image.getPlanes()[0].getPixelStride();
|
if (previewView != null)
|
||||||
|
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
||||||
int luminosity;
|
|
||||||
float sum = 0;
|
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
||||||
|
}
|
||||||
for (int y = 0; y < height; ++y) {
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
int index = (y * yRowStride) + (x * yPixelStride);
|
/** Return Luminosity from Image */
|
||||||
luminosity = (luminosityBuffer.get(index) & 0xff);
|
private float calculateLuminosity (Image image) {
|
||||||
sum += luminosity;
|
int width = image.getWidth();
|
||||||
}
|
int height = image.getHeight();
|
||||||
}
|
|
||||||
return sum / (width * height);
|
Image.Plane[] planes = image.getPlanes();
|
||||||
}
|
ByteBuffer luminosityBuffer = planes[0].getBuffer();
|
||||||
|
|
||||||
/** Check if delta Luminosity exceeds threshold */
|
int yRowStride = image.getPlanes()[0].getRowStride();
|
||||||
private void checkForViolation(float luminosity, float previousLuminosity) {
|
int yPixelStride = image.getPlanes()[0].getPixelStride();
|
||||||
float deltaLuminosity = Math.abs(luminosity - previousLuminosity);
|
|
||||||
if (deltaLuminosity > DELTA_LUMINOSITY_THRESHOLD) {
|
int luminosity;
|
||||||
isViolated = true;
|
float sum = 0;
|
||||||
Log.d("Violation", "Violation");
|
|
||||||
}
|
for (int y = 0; y < height; ++y) {
|
||||||
Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity)));
|
for (int x = 0; x < width; x++) {
|
||||||
}
|
int index = (y * yRowStride) + (x * yPixelStride);
|
||||||
}
|
luminosity = (luminosityBuffer.get(index) & 0xff);
|
||||||
|
sum += luminosity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum / (width * height);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** 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(deltaLuminosity);
|
||||||
|
Log.d("Violation", "Violation");
|
||||||
|
}
|
||||||
|
Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity)));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user