Compare commits
11
Commits
ms
...
1576719d10
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1576719d10 | ||
|
|
362af75b9a | ||
|
|
bef5d03494 | ||
|
|
80ebf77eb6 | ||
|
|
c4ed247a05 | ||
|
|
323786859e | ||
|
|
354bf937b2 | ||
|
|
39bb37f5c3 | ||
|
|
b198dc2e06 | ||
|
|
d83cd3dc32 | ||
|
|
065acefeb9 |
No files matched your search
Whitespace-only changes.
@@ -36,4 +36,13 @@ dependencies {
|
|||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||||
|
|
||||||
|
// Required for CameraX
|
||||||
|
def camerax_version = "1.2.2"
|
||||||
|
implementation "androidx.camera:camera-core:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-camera2:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-video:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-view:${camerax_version}"
|
||||||
|
implementation "androidx.camera:camera-extensions:${camerax_version}"
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,9 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<uses-feature android:name="android.hardware.camera"/>
|
||||||
|
<uses-permission android:name="android.permission.CAMERA"/>
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|||||||
@@ -1,14 +1,137 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem;
|
||||||
|
|
||||||
|
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.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
import androidx.camera.core.ExperimentalGetImage;
|
||||||
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.example.ueberwachungssystem.VideoDetection.DetectionReport;
|
||||||
|
import com.example.ueberwachungssystem.VideoDetection.VideoDetector;
|
||||||
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
@ExperimentalGetImage
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
|
||||||
|
private PreviewView previewView;
|
||||||
|
private TextView textView;
|
||||||
|
private Button previewButton;
|
||||||
|
private Boolean cameraPreviewIsRunning = false;
|
||||||
|
private Boolean isPressed = false;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
textView = findViewById(R.id.textView);
|
||||||
|
previewView = findViewById(R.id.previewView);
|
||||||
|
previewButton = findViewById(R.id.previewButton);
|
||||||
|
|
||||||
|
|
||||||
|
VideoDetector vd = new VideoDetector(this);
|
||||||
|
vd.setPreviewView(previewView);
|
||||||
|
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() {
|
||||||
|
@Override
|
||||||
|
public void onDetection(DetectionReport detectionReport) {
|
||||||
|
Log.d("Listener", detectionReport.detectionType);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
vd.startDetection();
|
||||||
|
|
||||||
|
|
||||||
|
previewButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
getCameraAccess();
|
||||||
|
if (isCameraAccessAllowed() && !isPressed)
|
||||||
|
{
|
||||||
|
vd.stopDetection();
|
||||||
|
isPressed = true;
|
||||||
|
}
|
||||||
|
else if (isCameraAccessAllowed() && isPressed) {
|
||||||
|
vd.startDetection();
|
||||||
|
isPressed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCameraAccessAllowed() {
|
||||||
|
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void getCameraAccess() {
|
||||||
|
if (!isCameraAccessAllowed())
|
||||||
|
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||||
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
|
|
||||||
|
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0) {
|
||||||
|
boolean cameraRights = grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
||||||
|
if (cameraRights) {
|
||||||
|
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.example.ueberwachungssystem.VideoDetection;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
/** Detection Report Class */
|
||||||
|
|
||||||
|
public class DetectionReport {
|
||||||
|
public String timeStamp;
|
||||||
|
public String detectionType;
|
||||||
|
public float detectedAmplitude;
|
||||||
|
public String detectorID;
|
||||||
|
|
||||||
|
public DetectionReport(String detectorID, String detectionType, float detectedAmplitude) {
|
||||||
|
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
|
||||||
|
this.detectionType = detectionType;
|
||||||
|
this.detectedAmplitude = detectedAmplitude;
|
||||||
|
this.detectorID = detectorID;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
package com.example.ueberwachungssystem.VideoDetection;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.ImageFormat;
|
||||||
|
import android.media.Image;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.util.Size;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.camera.core.CameraSelector;
|
||||||
|
import androidx.camera.core.ExperimentalGetImage;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
|
||||||
|
@ExperimentalGetImage
|
||||||
|
public class VideoDetector {
|
||||||
|
// Calling Activity
|
||||||
|
private final Context context;
|
||||||
|
// Camera Provider
|
||||||
|
private ProcessCameraProvider cameraProvider;
|
||||||
|
private Boolean isDetectionRunning = false;
|
||||||
|
// Preview Camera Image
|
||||||
|
private PreviewView previewView = null;
|
||||||
|
// Check Violation
|
||||||
|
private final float DELTA_LUMINOSITY_THRESHOLD = 0.5f;
|
||||||
|
private float previousLuminosity = 0f;
|
||||||
|
// On Detection Listener
|
||||||
|
private OnDetectionListener listener;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** Constructor */
|
||||||
|
public VideoDetector(Context context) { this.context = context; }
|
||||||
|
|
||||||
|
|
||||||
|
/** On Detection Listener - runs when a violation is reported */
|
||||||
|
public interface OnDetectionListener {
|
||||||
|
public void onDetection(DetectionReport detectionReport);
|
||||||
|
}
|
||||||
|
public void setOnDetectionListener(OnDetectionListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
public void reportViolation(float amplitude) {
|
||||||
|
DetectionReport detectionReport = new DetectionReport("123", "Video", amplitude);
|
||||||
|
if (listener != null)
|
||||||
|
listener.onDetection(detectionReport);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Return State of Video Detector */
|
||||||
|
public Boolean isRunning() {
|
||||||
|
return isDetectionRunning;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Starts Video Detection */
|
||||||
|
public void startDetection() {
|
||||||
|
isDetectionRunning = true;
|
||||||
|
// Request Camera Provider
|
||||||
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||||
|
//Check for Camera availability
|
||||||
|
cameraProviderFuture.addListener(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
cameraProvider = cameraProviderFuture.get();
|
||||||
|
bindLuminosityAnalysis(cameraProvider);
|
||||||
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
|
// No errors need to be handled for this Future. This should never be reached.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},ContextCompat.getMainExecutor(context));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Stops Video Detection */
|
||||||
|
public void stopDetection() {
|
||||||
|
cameraProvider.unbindAll();
|
||||||
|
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
|
||||||
|
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
||||||
|
builder.setTargetResolution(new Size(640, 480));
|
||||||
|
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
||||||
|
ImageAnalysis imageAnalysis = builder.build();
|
||||||
|
|
||||||
|
// Set Analyzer
|
||||||
|
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), new ImageAnalysis.Analyzer() {
|
||||||
|
@Override
|
||||||
|
public void analyze(@NonNull ImageProxy imageProxy) {
|
||||||
|
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
||||||
|
Image image = imageProxy.getImage();
|
||||||
|
assert image != null;
|
||||||
|
|
||||||
|
// Analyze frame
|
||||||
|
float luminosity = calculateLuminosity(image);
|
||||||
|
Log.d("Video Detector", String.valueOf(luminosity));
|
||||||
|
checkForViolation(luminosity, previousLuminosity);
|
||||||
|
previousLuminosity = luminosity;
|
||||||
|
}
|
||||||
|
imageProxy.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Create Preview
|
||||||
|
Preview preview = new Preview.Builder().build();
|
||||||
|
// Specify which Camera to use
|
||||||
|
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
||||||
|
// Update PreviewView if set
|
||||||
|
if (previewView != null)
|
||||||
|
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
||||||
|
|
||||||
|
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Return Luminosity from Image */
|
||||||
|
private float calculateLuminosity (Image image) {
|
||||||
|
int width = image.getWidth();
|
||||||
|
int height = image.getHeight();
|
||||||
|
|
||||||
|
Image.Plane[] planes = image.getPlanes();
|
||||||
|
ByteBuffer luminosityBuffer = planes[0].getBuffer();
|
||||||
|
|
||||||
|
int yRowStride = image.getPlanes()[0].getRowStride();
|
||||||
|
int yPixelStride = image.getPlanes()[0].getPixelStride();
|
||||||
|
|
||||||
|
int luminosity;
|
||||||
|
float sum = 0;
|
||||||
|
|
||||||
|
for (int y = 0; y < height; ++y) {
|
||||||
|
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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:gravity="center"
|
||||||
|
android:orientation="vertical"
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<TextView
|
<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="Hello World!"
|
||||||
@@ -15,4 +19,17 @@
|
|||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
<Button
|
||||||
|
android:id="@+id/previewButton"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Button"
|
||||||
|
tools:layout_editor_absoluteX="156dp"
|
||||||
|
tools:layout_editor_absoluteY="189dp" />
|
||||||
|
|
||||||
|
<androidx.camera.view.PreviewView
|
||||||
|
android:id="@+id/previewView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
Reference in new issue
Block a user