Compare commits
3 Commits
1576719d10
...
3d799ae461
Author | SHA1 | Date | |
---|---|---|---|
3d799ae461 | |||
2501662737 | |||
d04d166e14 |
@ -2,31 +2,22 @@ package com.example.ueberwachungssystem;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.camera.core.Camera;
|
|
||||||
import androidx.camera.core.CameraSelector;
|
|
||||||
import androidx.camera.core.Preview;
|
|
||||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
|
||||||
import androidx.camera.view.PreviewView;
|
import androidx.camera.view.PreviewView;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.camera.core.ExperimentalGetImage;
|
import androidx.camera.core.ExperimentalGetImage;
|
||||||
import androidx.lifecycle.LifecycleOwner;
|
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.graphics.Color;
|
||||||
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.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
import android.widget.ToggleButton;
|
||||||
|
|
||||||
import com.example.ueberwachungssystem.VideoDetection.DetectionReport;
|
import com.example.ueberwachungssystem.VideoDetection.DetectionReport;
|
||||||
import com.example.ueberwachungssystem.VideoDetection.VideoDetector;
|
import com.example.ueberwachungssystem.VideoDetection.VideoDetector;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
|
||||||
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
@ExperimentalGetImage
|
@ExperimentalGetImage
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
@ -34,7 +25,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
|
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
|
||||||
private PreviewView previewView;
|
private PreviewView previewView;
|
||||||
private TextView textView;
|
private TextView textView;
|
||||||
private Button previewButton;
|
private ToggleButton toggleButton;
|
||||||
private Boolean cameraPreviewIsRunning = false;
|
private Boolean cameraPreviewIsRunning = false;
|
||||||
private Boolean isPressed = false;
|
private Boolean isPressed = false;
|
||||||
|
|
||||||
@ -46,7 +37,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
textView = findViewById(R.id.textView);
|
textView = findViewById(R.id.textView);
|
||||||
previewView = findViewById(R.id.previewView);
|
previewView = findViewById(R.id.previewView);
|
||||||
previewButton = findViewById(R.id.previewButton);
|
toggleButton = findViewById(R.id.previewButton);
|
||||||
|
|
||||||
|
|
||||||
VideoDetector vd = new VideoDetector(this);
|
VideoDetector vd = new VideoDetector(this);
|
||||||
@ -54,24 +45,23 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() {
|
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onDetection(DetectionReport detectionReport) {
|
public void onDetection(DetectionReport detectionReport) {
|
||||||
Log.d("Listener", detectionReport.detectionType);
|
detectionReport.log("OnDetection");
|
||||||
|
textView.setText(detectionReport.toString());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
vd.startDetection();
|
//vd.startDetection();
|
||||||
|
|
||||||
|
|
||||||
previewButton.setOnClickListener(new View.OnClickListener() {
|
toggleButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
getCameraAccess();
|
getCameraAccess();
|
||||||
if (isCameraAccessAllowed() && !isPressed)
|
if (isCameraAccessAllowed() && toggleButton.isChecked())
|
||||||
{
|
{
|
||||||
vd.stopDetection();
|
|
||||||
isPressed = true;
|
|
||||||
}
|
|
||||||
else if (isCameraAccessAllowed() && isPressed) {
|
|
||||||
vd.startDetection();
|
vd.startDetection();
|
||||||
isPressed = false;
|
}
|
||||||
|
else {
|
||||||
|
vd.stopDetection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -99,39 +89,4 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void previewCamera() {
|
|
||||||
// Request Camera Access
|
|
||||||
getCameraAccess();
|
|
||||||
// Return when Camera Access not allowed or Camera Preview is running
|
|
||||||
if (!isCameraAccessAllowed() || cameraPreviewIsRunning)
|
|
||||||
return;
|
|
||||||
// Camera Preview is running
|
|
||||||
cameraPreviewIsRunning = true;
|
|
||||||
// Request Camera Provider
|
|
||||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
|
|
||||||
//Check for Camera availability
|
|
||||||
cameraProviderFuture.addListener(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
|
|
||||||
bindPreview(cameraProvider);
|
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
|
||||||
// No errors need to be handled for this Future. This should never be reached.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},ContextCompat.getMainExecutor(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
|
|
||||||
// Create Preview
|
|
||||||
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
|
|
||||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
|
||||||
Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,19 +1,36 @@
|
|||||||
package com.example.ueberwachungssystem.VideoDetection;
|
package com.example.ueberwachungssystem.VideoDetection;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
|
||||||
/** Detection Report Class */
|
/** Detection Report Class */
|
||||||
|
|
||||||
public class DetectionReport {
|
public class DetectionReport {
|
||||||
public String timeStamp;
|
public String timeStamp;
|
||||||
public String detectionType;
|
public String detectionType;
|
||||||
public float detectedAmplitude;
|
public float detectedValue;
|
||||||
public String detectorID;
|
public String detectorID;
|
||||||
|
|
||||||
public DetectionReport(String detectorID, String detectionType, float detectedAmplitude) {
|
public DetectionReport(String detectorID, String detectionType, float detectedAmplitude) {
|
||||||
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
|
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
|
||||||
this.detectionType = detectionType;
|
this.detectionType = detectionType;
|
||||||
this.detectedAmplitude = detectedAmplitude;
|
this.detectedValue = detectedAmplitude;
|
||||||
this.detectorID = detectorID;
|
this.detectorID = detectorID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Get Detection Report in String format */
|
||||||
|
public String toString() {
|
||||||
|
String time = "Time: " + "[" + this.timeStamp + "]";
|
||||||
|
String type = "Type: " + "[" + this.detectionType + "]";
|
||||||
|
String value = "Value: " + "[" + this.detectedValue + "]";
|
||||||
|
String id = "ID: " + "[" + this.detectorID + "]";
|
||||||
|
|
||||||
|
return String.join("\t", time, type, value, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Debug Report */
|
||||||
|
public void log(String tag) {
|
||||||
|
Log.d(tag, this.toString());
|
||||||
|
}
|
||||||
}
|
}
|
@ -25,6 +25,7 @@ import java.util.concurrent.ExecutionException;
|
|||||||
|
|
||||||
@ExperimentalGetImage
|
@ExperimentalGetImage
|
||||||
public class VideoDetector {
|
public class VideoDetector {
|
||||||
|
|
||||||
// Calling Activity
|
// Calling Activity
|
||||||
private final Context context;
|
private final Context context;
|
||||||
// Camera Provider
|
// Camera Provider
|
||||||
@ -33,29 +34,32 @@ public class VideoDetector {
|
|||||||
// Preview Camera Image
|
// Preview Camera Image
|
||||||
private PreviewView previewView = null;
|
private PreviewView previewView = null;
|
||||||
// Check Violation
|
// Check Violation
|
||||||
private final float DELTA_LUMINOSITY_THRESHOLD = 0.5f;
|
private final float DELTA_LUMINOSITY_THRESHOLD = 0.3f;
|
||||||
private float previousLuminosity = 0f;
|
private Float previousLuminosity = null;
|
||||||
// On Detection Listener
|
// On Detection Listener
|
||||||
private OnDetectionListener listener;
|
private OnDetectionListener listener;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Constructor */
|
/** Constructor */
|
||||||
public VideoDetector(Context context) { this.context = context; }
|
public VideoDetector(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** On Detection Listener - runs when a violation is reported */
|
/** On Detection Listener - runs when a violation is reported */
|
||||||
public interface OnDetectionListener {
|
public interface OnDetectionListener {
|
||||||
public void onDetection(DetectionReport detectionReport);
|
void onDetection(DetectionReport detectionReport);
|
||||||
}
|
}
|
||||||
public void setOnDetectionListener(OnDetectionListener listener) {
|
public void setOnDetectionListener(OnDetectionListener listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
public void reportViolation(float amplitude) {
|
public void reportViolation(float amplitude) {
|
||||||
|
if (listener != null) {
|
||||||
DetectionReport detectionReport = new DetectionReport("123", "Video", amplitude);
|
DetectionReport detectionReport = new DetectionReport("123", "Video", amplitude);
|
||||||
if (listener != null)
|
|
||||||
listener.onDetection(detectionReport);
|
listener.onDetection(detectionReport);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Return State of Video Detector */
|
/** Return State of Video Detector */
|
||||||
@ -66,7 +70,8 @@ public class VideoDetector {
|
|||||||
|
|
||||||
/** Starts Video Detection */
|
/** Starts Video Detection */
|
||||||
public void startDetection() {
|
public void startDetection() {
|
||||||
isDetectionRunning = true;
|
if (isDetectionRunning)
|
||||||
|
return;
|
||||||
// Request Camera Provider
|
// Request Camera Provider
|
||||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||||
//Check for Camera availability
|
//Check for Camera availability
|
||||||
@ -76,6 +81,7 @@ public class VideoDetector {
|
|||||||
try {
|
try {
|
||||||
cameraProvider = cameraProviderFuture.get();
|
cameraProvider = cameraProviderFuture.get();
|
||||||
bindLuminosityAnalysis(cameraProvider);
|
bindLuminosityAnalysis(cameraProvider);
|
||||||
|
isDetectionRunning = true;
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
// No errors need to be handled for this Future. This should never be reached.
|
// No errors need to be handled for this Future. This should never be reached.
|
||||||
}
|
}
|
||||||
@ -86,6 +92,8 @@ public class VideoDetector {
|
|||||||
|
|
||||||
/** Stops Video Detection */
|
/** Stops Video Detection */
|
||||||
public void stopDetection() {
|
public void stopDetection() {
|
||||||
|
if (!isDetectionRunning)
|
||||||
|
return;
|
||||||
cameraProvider.unbindAll();
|
cameraProvider.unbindAll();
|
||||||
isDetectionRunning = false;
|
isDetectionRunning = false;
|
||||||
}
|
}
|
||||||
@ -116,6 +124,7 @@ 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 (previousLuminosity != null)
|
||||||
checkForViolation(luminosity, previousLuminosity);
|
checkForViolation(luminosity, previousLuminosity);
|
||||||
previousLuminosity = luminosity;
|
previousLuminosity = luminosity;
|
||||||
}
|
}
|
||||||
@ -131,6 +140,7 @@ public class VideoDetector {
|
|||||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
||||||
|
|
||||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
||||||
|
stopDetection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,23 +13,24 @@
|
|||||||
android:id="@+id/textView"
|
android:id="@+id/textView"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Hello World!"
|
android:text=""
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
<Button
|
|
||||||
|
<ToggleButton
|
||||||
android:id="@+id/previewButton"
|
android:id="@+id/previewButton"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Button"
|
android:text="ToggleButton" />
|
||||||
tools:layout_editor_absoluteX="156dp"
|
|
||||||
tools:layout_editor_absoluteY="189dp" />
|
|
||||||
|
|
||||||
<androidx.camera.view.PreviewView
|
<androidx.camera.view.PreviewView
|
||||||
android:id="@+id/previewView"
|
android:id="@+id/previewView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent" />
|
android:layout_height="match_parent"
|
||||||
|
android:backgroundTint="@android:color/black"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
Loading…
x
Reference in New Issue
Block a user