Compare commits
11
Commits
bc5227ba4f
...
bk_video
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e01e23eccd | ||
|
|
a493bafb79 | ||
|
|
aed079377f | ||
|
|
bff6abbd29 | ||
|
|
5d31f0bb46 | ||
|
|
ea0e88ed89 | ||
|
|
7dee124378 | ||
|
|
0ee1446cbc | ||
|
|
4185cb5200 | ||
|
|
9b2d57ad37 | ||
|
|
847e90f5ac |
No files matched your search
@@ -3,6 +3,7 @@ package com.example.ueberwachungssystem.Detection;
|
||||
import android.content.Context;
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.MediaRecorder;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -50,6 +51,7 @@ public class AudioRecorder {
|
||||
mediaRecorder.release();
|
||||
mediaRecorder = null;
|
||||
isRecording = false;
|
||||
Toast.makeText(context, "audio recording saved", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +1,20 @@
|
||||
package com.example.ueberwachungssystem.Detection;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.camera.core.ExperimentalGetImage;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.lifecycle.LifecycleService;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@ExperimentalGetImage
|
||||
public class DetectorService extends LifecycleService {
|
||||
|
||||
public TestBinder testBinder = new TestBinder();
|
||||
public ServiceBinder serviceBinder = new ServiceBinder();
|
||||
private DetectorService.OnDetectionListener listener;
|
||||
private boolean isServiceRunning = false;
|
||||
|
||||
@@ -33,6 +26,9 @@ public class DetectorService extends LifecycleService {
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (isServiceRunning)
|
||||
return START_NOT_STICKY;
|
||||
|
||||
|
||||
// Setup Service classes:
|
||||
videoDetector = new VideoDetector(this);
|
||||
videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
|
||||
@Override
|
||||
@@ -44,8 +40,8 @@ public class DetectorService extends LifecycleService {
|
||||
audioRecorder = new AudioRecorder(this);
|
||||
|
||||
|
||||
isServiceRunning = true;
|
||||
|
||||
isServiceRunning = true;
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
@@ -56,7 +52,7 @@ public class DetectorService extends LifecycleService {
|
||||
}
|
||||
|
||||
/** Service methods */
|
||||
public class TestBinder extends Binder {
|
||||
public class ServiceBinder extends Binder {
|
||||
public DetectorService getBoundService() {
|
||||
// Return an instance of the TestService
|
||||
return DetectorService.this;
|
||||
@@ -66,7 +62,7 @@ public class DetectorService extends LifecycleService {
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
super.onBind(intent);
|
||||
return testBinder;
|
||||
return serviceBinder;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.example.ueberwachungssystem.Detection;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.ImageFormat;
|
||||
@@ -53,9 +52,6 @@ public class VideoDetector extends Detector {
|
||||
// Calling Activity
|
||||
private final Context context;
|
||||
|
||||
// Permission handling
|
||||
private static final int PERMISSION_REQUEST_CODE = 3691;
|
||||
|
||||
// Camera Provider
|
||||
private ProcessCameraProvider cameraProvider;
|
||||
private ImageAnalysis imageAnalysis;
|
||||
@@ -79,9 +75,11 @@ public class VideoDetector extends Detector {
|
||||
|
||||
|
||||
// Parameters
|
||||
private static final float ALARM_THRESHOLD = 0.5f; // Percent of pixels changed
|
||||
private static final float ALARM_THRESHOLD = 0f; // Percent of pixels changed
|
||||
private static final float AREA_THRESHOLD = 10f;
|
||||
private static final int DILATE_ITERATIONS = 2;
|
||||
private static final float START_DELAY = 20000; // milliseconds
|
||||
private static final android.util.Size IMAGE_RES = new android.util.Size(480, 360);
|
||||
private static final android.util.Size IMAGE_RES = new android.util.Size(640, 480);
|
||||
|
||||
|
||||
|
||||
@@ -110,11 +108,6 @@ public class VideoDetector extends Detector {
|
||||
// Check States
|
||||
if (isDetecting)
|
||||
return;
|
||||
// Return On Request Permissions
|
||||
if (!hasPermissions()) {
|
||||
getPermissions();
|
||||
return;
|
||||
}
|
||||
// Configure Image Analysis
|
||||
imageAnalysis = setupImageAnalysis();
|
||||
// Open CV startup check
|
||||
@@ -141,12 +134,6 @@ public class VideoDetector extends Detector {
|
||||
public void startRecording() {
|
||||
// Check States
|
||||
if (isRecording){
|
||||
extendViolation();
|
||||
return;
|
||||
}
|
||||
// Return On Request Permissions
|
||||
if (!hasPermissions()) {
|
||||
getPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -170,12 +157,12 @@ public class VideoDetector extends Detector {
|
||||
@Override
|
||||
public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) {
|
||||
isRecording = false;
|
||||
Toast.makeText(context, "recording saved", Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(context, "video recording saved", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
@Override
|
||||
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
|
||||
isRecording = false;
|
||||
Toast.makeText(context, "recording failed", Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(context, "video recording failed", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -196,6 +183,9 @@ public class VideoDetector extends Detector {
|
||||
/** Stops the Recording */
|
||||
@SuppressLint("RestrictedApi")
|
||||
public void stopRecording(){
|
||||
if(!isRecording)
|
||||
return;
|
||||
|
||||
videoCapture.stopRecording();
|
||||
cameraProvider.unbind(videoCapture);
|
||||
isRecording = false;
|
||||
@@ -234,7 +224,7 @@ public class VideoDetector extends Detector {
|
||||
// Violation Condition
|
||||
if (percentChanged * 100 > ALARM_THRESHOLD) {
|
||||
if (allowReportViolation)
|
||||
reportViolation("Video", n);
|
||||
reportViolation("Video", percentChanged);
|
||||
}
|
||||
}
|
||||
imageProxy.close();
|
||||
@@ -271,9 +261,11 @@ public class VideoDetector extends Detector {
|
||||
// Process Image
|
||||
Mat processed = preprocessed.clone();
|
||||
processed = OpenCVHelper.thresholdPixels(processed, previousImage, 25);
|
||||
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
||||
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
||||
processed = OpenCVHelper.thresholdContourArea(processed, 500);
|
||||
|
||||
for(int i = 0; i < DILATE_ITERATIONS; i++)
|
||||
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
||||
|
||||
processed = OpenCVHelper.thresholdContourArea(processed, AREA_THRESHOLD);
|
||||
// Output
|
||||
previousImage = preprocessed.clone();
|
||||
// Show Output Image
|
||||
@@ -289,7 +281,6 @@ public class VideoDetector extends Detector {
|
||||
this.outputImageView = outputImageView;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
private void setPreviewView(@NonNull PreviewView previewView) {
|
||||
// Create Preview
|
||||
@@ -299,7 +290,6 @@ public class VideoDetector extends Detector {
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/** Generate File Name */
|
||||
private String generateFileName(){
|
||||
// Get the current timestamp
|
||||
@@ -318,7 +308,6 @@ public class VideoDetector extends Detector {
|
||||
return display.getRotation();
|
||||
}
|
||||
|
||||
|
||||
/** Start delay until Violation Report is allowed */
|
||||
private void startViolationTimer(float setupTime) {
|
||||
new CountDownTimer((long) (START_DELAY), 100) {
|
||||
@@ -332,16 +321,6 @@ public class VideoDetector extends Detector {
|
||||
}.start();
|
||||
}
|
||||
|
||||
/** Permission handling */
|
||||
private boolean hasPermissions() {
|
||||
return ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
|
||||
ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
private void getPermissions() {
|
||||
if (!hasPermissions())
|
||||
ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
|
||||
public void setOutputDir(File outputDir) {
|
||||
this.outputDir = outputDir;
|
||||
}
|
||||
|
||||
@@ -4,95 +4,76 @@ import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.camera.core.ExperimentalGetImage;
|
||||
import androidx.camera.view.PreviewView;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ToggleButton;
|
||||
|
||||
import com.example.ueberwachungssystem.Detection.AudioRecorder;
|
||||
import com.example.ueberwachungssystem.Detection.DetectionReport;
|
||||
import com.example.ueberwachungssystem.Detection.DetectorService;
|
||||
import com.example.ueberwachungssystem.Detection.Detector;
|
||||
import com.example.ueberwachungssystem.Detection.VideoDetector;
|
||||
|
||||
@ExperimentalGetImage
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private DetectorService detectorService = new DetectorService();
|
||||
private ImageView inputImageView;
|
||||
private ImageView outputImageView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
|
||||
inputImageView = findViewById(R.id.inputImageView);
|
||||
outputImageView = findViewById(R.id.outputImageView);
|
||||
ImageView inputImageView = findViewById(R.id.inputImageView);
|
||||
ImageView outputImageView = findViewById(R.id.outputImageView);
|
||||
PreviewView previewView = findViewById(R.id.previewView);
|
||||
|
||||
|
||||
|
||||
Intent serviceIntent = new Intent(this, DetectorService.class);
|
||||
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
|
||||
startService(serviceIntent);
|
||||
PermissionHandler permissionHandler = new PermissionHandler(this);
|
||||
|
||||
|
||||
permissionHandler.getPermissions();
|
||||
|
||||
|
||||
ToggleButton toggleButton = findViewById(R.id.toggleButton);
|
||||
toggleButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (toggleButton.isChecked())
|
||||
{
|
||||
detectorService.startVideoRecording();
|
||||
//detectorService.startAudioRecording();
|
||||
}
|
||||
else {
|
||||
//detectorService.stopAudioRecording();
|
||||
detectorService.stopVideoRecording();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ServiceConnection serviceConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
DetectorService.TestBinder binder = (DetectorService.TestBinder) service;
|
||||
detectorService = binder.getBoundService();
|
||||
|
||||
detectorService.startVideoDetection();
|
||||
detectorService.debugVideoProcessing(inputImageView, outputImageView);
|
||||
detectorService.setOnDetectionListener(new DetectorService.OnDetectionListener() {
|
||||
if (permissionHandler.hasPermissions()) {
|
||||
AudioRecorder audioRecorder = new AudioRecorder(this);
|
||||
VideoDetector vd = new VideoDetector(this);
|
||||
//vd.setPreviewView(previewView);
|
||||
vd.debugProcessing(inputImageView, outputImageView);
|
||||
vd.setOnDetectionListener(new Detector.OnDetectionListener() {
|
||||
@Override
|
||||
public void onDetection(@NonNull DetectionReport detectionReport) {
|
||||
Log.d("onDetection", detectionReport.toString());
|
||||
}
|
||||
});
|
||||
vd.startDetection();
|
||||
|
||||
ToggleButton toggleButton = findViewById(R.id.toggleButton);
|
||||
toggleButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (toggleButton.isChecked())
|
||||
{
|
||||
//vd.startDetection();
|
||||
//vd.stopDetection();
|
||||
vd.startRecording();
|
||||
audioRecorder.startRecording();
|
||||
}
|
||||
else {
|
||||
//vd.stopDetection();
|
||||
vd.stopRecording();
|
||||
audioRecorder.stopRecording();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {}
|
||||
};
|
||||
|
||||
|
||||
private boolean hasPermissions() {
|
||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
|
||||
ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
private void getPermissions() {
|
||||
if (!hasPermissions())
|
||||
ActivityCompat.requestPermissions((Activity) this, new String[]{android.Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, 12345);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.example.ueberwachungssystem;
|
||||
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
public class PermissionHandler {
|
||||
private final Context context;
|
||||
private static final int PERMISSION_REQUEST_CODE = 23409;
|
||||
private static final String[] permissions = new String[]{
|
||||
android.Manifest.permission.CAMERA,
|
||||
android.Manifest.permission.RECORD_AUDIO
|
||||
};
|
||||
|
||||
public PermissionHandler(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public boolean hasPermissions() {
|
||||
boolean permissionState = true;
|
||||
for (String permission: permissions) {
|
||||
permissionState = permissionState && ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
return permissionState;
|
||||
}
|
||||
|
||||
public void getPermissions() {
|
||||
if (!hasPermissions())
|
||||
ActivityCompat.requestPermissions((Activity) context, permissions, PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
|
||||
public void showPermissionToast() {
|
||||
if (hasPermissions())
|
||||
Toast.makeText(context, "permissions available", Toast.LENGTH_SHORT).show();
|
||||
else
|
||||
Toast.makeText(context, "permissions missing", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user