Compare commits
6
Commits
7dee124378
...
bk_video
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e01e23eccd | ||
|
|
a493bafb79 | ||
|
|
aed079377f | ||
|
|
bff6abbd29 | ||
|
|
5d31f0bb46 | ||
|
|
ea0e88ed89 |
No files matched your search
@@ -14,7 +14,6 @@ import java.io.File;
|
|||||||
|
|
||||||
@ExperimentalGetImage
|
@ExperimentalGetImage
|
||||||
public class DetectorService extends LifecycleService {
|
public class DetectorService extends LifecycleService {
|
||||||
|
|
||||||
public ServiceBinder serviceBinder = new ServiceBinder();
|
public ServiceBinder serviceBinder = new ServiceBinder();
|
||||||
private DetectorService.OnDetectionListener listener;
|
private DetectorService.OnDetectionListener listener;
|
||||||
private boolean isServiceRunning = false;
|
private boolean isServiceRunning = false;
|
||||||
@@ -27,6 +26,9 @@ public class DetectorService extends LifecycleService {
|
|||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
if (isServiceRunning)
|
if (isServiceRunning)
|
||||||
return START_NOT_STICKY;
|
return START_NOT_STICKY;
|
||||||
|
|
||||||
|
|
||||||
|
// Setup Service classes:
|
||||||
videoDetector = new VideoDetector(this);
|
videoDetector = new VideoDetector(this);
|
||||||
videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
|
videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
|
||||||
@Override
|
@Override
|
||||||
@@ -38,8 +40,8 @@ public class DetectorService extends LifecycleService {
|
|||||||
audioRecorder = new AudioRecorder(this);
|
audioRecorder = new AudioRecorder(this);
|
||||||
|
|
||||||
|
|
||||||
isServiceRunning = true;
|
|
||||||
|
|
||||||
|
isServiceRunning = true;
|
||||||
return super.onStartCommand(intent, flags, startId);
|
return super.onStartCommand(intent, flags, startId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package com.example.ueberwachungssystem.Detection;
|
|||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.ImageFormat;
|
import android.graphics.ImageFormat;
|
||||||
@@ -53,9 +52,6 @@ public class VideoDetector extends Detector {
|
|||||||
// Calling Activity
|
// Calling Activity
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
|
||||||
// Permission handling
|
|
||||||
private static final int PERMISSION_REQUEST_CODE = 3691;
|
|
||||||
|
|
||||||
// Camera Provider
|
// Camera Provider
|
||||||
private ProcessCameraProvider cameraProvider;
|
private ProcessCameraProvider cameraProvider;
|
||||||
private ImageAnalysis imageAnalysis;
|
private ImageAnalysis imageAnalysis;
|
||||||
@@ -79,9 +75,11 @@ public class VideoDetector extends Detector {
|
|||||||
|
|
||||||
|
|
||||||
// Parameters
|
// 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 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
|
// Check States
|
||||||
if (isDetecting)
|
if (isDetecting)
|
||||||
return;
|
return;
|
||||||
// Return On Request Permissions
|
|
||||||
if (!hasPermissions()) {
|
|
||||||
getPermissions();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Configure Image Analysis
|
// Configure Image Analysis
|
||||||
imageAnalysis = setupImageAnalysis();
|
imageAnalysis = setupImageAnalysis();
|
||||||
// Open CV startup check
|
// Open CV startup check
|
||||||
@@ -141,12 +134,6 @@ public class VideoDetector extends Detector {
|
|||||||
public void startRecording() {
|
public void startRecording() {
|
||||||
// Check States
|
// Check States
|
||||||
if (isRecording){
|
if (isRecording){
|
||||||
extendViolation();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Return On Request Permissions
|
|
||||||
if (!hasPermissions()) {
|
|
||||||
getPermissions();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +224,7 @@ public class VideoDetector extends Detector {
|
|||||||
// Violation Condition
|
// Violation Condition
|
||||||
if (percentChanged * 100 > ALARM_THRESHOLD) {
|
if (percentChanged * 100 > ALARM_THRESHOLD) {
|
||||||
if (allowReportViolation)
|
if (allowReportViolation)
|
||||||
reportViolation("Video", n);
|
reportViolation("Video", percentChanged);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imageProxy.close();
|
imageProxy.close();
|
||||||
@@ -274,9 +261,11 @@ public class VideoDetector extends Detector {
|
|||||||
// Process Image
|
// Process Image
|
||||||
Mat processed = preprocessed.clone();
|
Mat processed = preprocessed.clone();
|
||||||
processed = OpenCVHelper.thresholdPixels(processed, previousImage, 25);
|
processed = OpenCVHelper.thresholdPixels(processed, previousImage, 25);
|
||||||
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
|
||||||
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
for(int i = 0; i < DILATE_ITERATIONS; i++)
|
||||||
processed = OpenCVHelper.thresholdContourArea(processed, 500);
|
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
||||||
|
|
||||||
|
processed = OpenCVHelper.thresholdContourArea(processed, AREA_THRESHOLD);
|
||||||
// Output
|
// Output
|
||||||
previousImage = preprocessed.clone();
|
previousImage = preprocessed.clone();
|
||||||
// Show Output Image
|
// Show Output Image
|
||||||
@@ -292,7 +281,6 @@ public class VideoDetector extends Detector {
|
|||||||
this.outputImageView = outputImageView;
|
this.outputImageView = outputImageView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
private void setPreviewView(@NonNull PreviewView previewView) {
|
private void setPreviewView(@NonNull PreviewView previewView) {
|
||||||
// Create Preview
|
// Create Preview
|
||||||
@@ -302,7 +290,6 @@ public class VideoDetector extends Detector {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Generate File Name */
|
/** Generate File Name */
|
||||||
private String generateFileName(){
|
private String generateFileName(){
|
||||||
// Get the current timestamp
|
// Get the current timestamp
|
||||||
@@ -321,7 +308,6 @@ public class VideoDetector extends Detector {
|
|||||||
return display.getRotation();
|
return display.getRotation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Start delay until Violation Report is allowed */
|
/** Start delay until Violation Report is allowed */
|
||||||
private void startViolationTimer(float setupTime) {
|
private void startViolationTimer(float setupTime) {
|
||||||
new CountDownTimer((long) (START_DELAY), 100) {
|
new CountDownTimer((long) (START_DELAY), 100) {
|
||||||
@@ -335,16 +321,6 @@ public class VideoDetector extends Detector {
|
|||||||
}.start();
|
}.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) {
|
public void setOutputDir(File outputDir) {
|
||||||
this.outputDir = outputDir;
|
this.outputDir = outputDir;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,39 +30,50 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
PreviewView previewView = findViewById(R.id.previewView);
|
PreviewView previewView = findViewById(R.id.previewView);
|
||||||
|
|
||||||
|
|
||||||
VideoDetector vd = new VideoDetector(this);
|
PermissionHandler permissionHandler = new PermissionHandler(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();
|
|
||||||
|
|
||||||
|
|
||||||
AudioRecorder audioRecorder = new AudioRecorder(this);
|
permissionHandler.getPermissions();
|
||||||
|
|
||||||
|
if (permissionHandler.hasPermissions()) {
|
||||||
|
AudioRecorder audioRecorder = new AudioRecorder(this);
|
||||||
ToggleButton toggleButton = findViewById(R.id.toggleButton);
|
VideoDetector vd = new VideoDetector(this);
|
||||||
toggleButton.setOnClickListener(new View.OnClickListener() {
|
//vd.setPreviewView(previewView);
|
||||||
@Override
|
vd.debugProcessing(inputImageView, outputImageView);
|
||||||
public void onClick(View v) {
|
vd.setOnDetectionListener(new Detector.OnDetectionListener() {
|
||||||
if (toggleButton.isChecked())
|
@Override
|
||||||
{
|
public void onDetection(@NonNull DetectionReport detectionReport) {
|
||||||
//vd.startDetection();
|
Log.d("onDetection", detectionReport.toString());
|
||||||
//vd.stopDetection();
|
|
||||||
vd.startRecording();
|
|
||||||
audioRecorder.startRecording();
|
|
||||||
}
|
}
|
||||||
else {
|
});
|
||||||
//vd.stopDetection();
|
vd.startDetection();
|
||||||
vd.stopRecording();
|
|
||||||
audioRecorder.stopRecording();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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