|
|
@@ -13,6 +13,7 @@ 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;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
|
|
|
|
|
@@ -23,20 +24,31 @@ import java.util.concurrent.ExecutionException; |
|
|
|
|
|
|
|
@ExperimentalGetImage
|
|
|
|
public class VideoDetector {
|
|
|
|
|
|
|
|
// Calling Activity
|
|
|
|
private final Context context;
|
|
|
|
// Camera Provider
|
|
|
|
private ProcessCameraProvider cameraProvider;
|
|
|
|
private Boolean isDetectionRunning = false;
|
|
|
|
//private float currentLuminosity;
|
|
|
|
// Preview Camera Image
|
|
|
|
private PreviewView previewView = null;
|
|
|
|
// Check Violation
|
|
|
|
private final float DELTA_LUMINOSITY_THRESHOLD = 10f;
|
|
|
|
private float lastLuminosity = 0f;
|
|
|
|
private Boolean isViolated = false;
|
|
|
|
|
|
|
|
|
|
|
|
/** Constructor */
|
|
|
|
public VideoDetector(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
public VideoDetector(Context context) { this.context = context; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Return State of Video Detector */
|
|
|
|
public Boolean isRunning() {
|
|
|
|
return isDetectionRunning;
|
|
|
|
public Boolean isRunning() { return isDetectionRunning; }
|
|
|
|
|
|
|
|
/** Return the status of Violation */
|
|
|
|
public boolean getViolationStatus()
|
|
|
|
{
|
|
|
|
return isViolated;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Starts Video Detection */
|
|
|
@@ -64,6 +76,11 @@ public class VideoDetector { |
|
|
|
isDetectionRunning = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set PreviewView to show Image */
|
|
|
|
public void setPreviewView(PreviewView previewView) {
|
|
|
|
this.previewView = previewView;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
|
|
|
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
|
|
|
// Configure and create Image Analysis
|
|
|
@@ -83,6 +100,9 @@ public class VideoDetector { |
|
|
|
// Analyze frame
|
|
|
|
float luminosity = calculateLuminosity(image);
|
|
|
|
Log.d("Video Detector", String.valueOf(luminosity));
|
|
|
|
if (luminosity != 0)
|
|
|
|
checkForViolation(luminosity, lastLuminosity);
|
|
|
|
lastLuminosity = luminosity;
|
|
|
|
}
|
|
|
|
imageProxy.close();
|
|
|
|
}
|
|
|
@@ -91,7 +111,10 @@ public class VideoDetector { |
|
|
|
Preview preview = new Preview.Builder().build();
|
|
|
|
// Specify which Camera to use
|
|
|
|
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
|
|
|
// Connect Preview to PreviewView
|
|
|
|
// Update PreviewView if set
|
|
|
|
if (previewView != null)
|
|
|
|
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
|
|
|
|
|
|
|
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
|
|
|
}
|
|
|
|
|
|
|
@@ -118,4 +141,14 @@ public class VideoDetector { |
|
|
|
}
|
|
|
|
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) {
|
|
|
|
isViolated = true;
|
|
|
|
Log.d("Violation", "Violation");
|
|
|
|
}
|
|
|
|
Log.d("Delta", String.valueOf(Math.abs(previousLuminosity - luminosity)));
|
|
|
|
}
|
|
|
|
}
|