Added functions for checking luminosity violation
This commit is contained in:
parent
80ebf77eb6
commit
bef5d03494
@ -46,6 +46,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
|
|
||||||
VideoDetector vd = new VideoDetector(this);
|
VideoDetector vd = new VideoDetector(this);
|
||||||
|
vd.setPreviewView(previewView);
|
||||||
vd.startDetection();
|
vd.startDetection();
|
||||||
|
|
||||||
previewButton.setOnClickListener(new View.OnClickListener() {
|
previewButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@ -13,6 +13,7 @@ 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.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.lifecycle.LifecycleOwner;
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
|
|
||||||
@ -23,20 +24,31 @@ import java.util.concurrent.ExecutionException;
|
|||||||
|
|
||||||
@ExperimentalGetImage
|
@ExperimentalGetImage
|
||||||
public class VideoDetector {
|
public class VideoDetector {
|
||||||
|
// Calling Activity
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
// Camera Provider
|
||||||
private ProcessCameraProvider cameraProvider;
|
private ProcessCameraProvider cameraProvider;
|
||||||
private Boolean isDetectionRunning = false;
|
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 */
|
/** Constructor */
|
||||||
public VideoDetector(Context context) {
|
public VideoDetector(Context context) { this.context = context; }
|
||||||
this.context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Return State of Video Detector */
|
/** Return State of Video Detector */
|
||||||
public Boolean isRunning() {
|
public Boolean isRunning() { return isDetectionRunning; }
|
||||||
return isDetectionRunning;
|
|
||||||
|
/** Return the status of Violation */
|
||||||
|
public boolean getViolationStatus()
|
||||||
|
{
|
||||||
|
return isViolated;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Starts Video Detection */
|
/** Starts Video Detection */
|
||||||
@ -64,6 +76,11 @@ public class VideoDetector {
|
|||||||
isDetectionRunning = false;
|
isDetectionRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Set PreviewView to show Image */
|
||||||
|
public void setPreviewView(PreviewView previewView) {
|
||||||
|
this.previewView = previewView;
|
||||||
|
}
|
||||||
|
|
||||||
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
||||||
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
private void bindLuminosityAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
||||||
// Configure and create Image Analysis
|
// Configure and create Image Analysis
|
||||||
@ -83,6 +100,9 @@ public class VideoDetector {
|
|||||||
// Analyze frame
|
// Analyze frame
|
||||||
float luminosity = calculateLuminosity(image);
|
float luminosity = calculateLuminosity(image);
|
||||||
Log.d("Video Detector", String.valueOf(luminosity));
|
Log.d("Video Detector", String.valueOf(luminosity));
|
||||||
|
if (luminosity != 0)
|
||||||
|
checkForViolation(luminosity, lastLuminosity);
|
||||||
|
lastLuminosity = luminosity;
|
||||||
}
|
}
|
||||||
imageProxy.close();
|
imageProxy.close();
|
||||||
}
|
}
|
||||||
@ -91,7 +111,10 @@ public class VideoDetector {
|
|||||||
Preview preview = new Preview.Builder().build();
|
Preview preview = new Preview.Builder().build();
|
||||||
// Specify which Camera to use
|
// Specify which Camera to use
|
||||||
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
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);
|
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,4 +141,14 @@ public class VideoDetector {
|
|||||||
}
|
}
|
||||||
return sum / (width * height);
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user